我有一种行为,它将图像添加为画布的子项:
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = d as ImageCanvasBehavior;
if (behavior == null)
return;
// The AssoicatedObjec is a Canvas.
Canvas canvas = behavior.AssociatedObject as Canvas;
if (canvas == null)
return;
canvas.Children.Clear();
CroppedBitmap b = (CroppedBitmap)e.NewValue;
Image img = new Image();
img.Source = b;
img.Width = canvas.ActualWidth;
img.Stretch = Stretch.Uniform;
canvas.Children.Add(img);
}
img是在画布的水平和垂直中间添加的。它们如何定位在画布的左上角?
TIA
编辑#2我认为这是错误的...只应显示图像,而不是图像上方和下方的所有黑色空间,图像不应被剪切掉。
答案 0 :(得分:1)
使用附加属性:
Image img = new Image();
img.Source = b;
img.Width = canvas.ActualWidth;
img.Stretch = Stretch.Uniform;
Canvas.SetTop(img, 0);
Canvas.SetLeft(img, 0);
答案 1 :(得分:0)
解决了这个问题。 croppedbitmap来自位于网格内的墨迹上的图像。左上方的墨迹扫描的参考点被视为:
<div id="wrapper">
<img id="element" src="https://pbs.twimg.com/profile_images/604644048/sign051.gif">
<img id="element2" src="http://smallbusinessbc.ca/wp-content/themes/sbbcmain/images/circle-icons/icon-education.svg">
</div>
通过以下方式获得CORRECT参考点:
//translate point to get the position of the media element
var p = ic.PointToScreen(new Point(0, 0));
修复后,其他所有内容都会正确恢复。