我将图像拖放到画布上。我已修复边界,以便我的图像不会拖出画布,但是当我放大并缩小图像时,它的边界会发生变化,并且不会拖动整个画布。我已经尝试过这个。直到现在。 p>
public void Btnedit_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
button.IsEnabled = false;
CompositeTransform CT = new CompositeTransform();
ImgeracOpen.ManipulationMode = ManipulationModes.All;
//ImgeracOpen.ManipulationDelta += Drag_ManipulationDelta;
ImgeracOpen.ManipulationDelta += Composite_ManipulationDelta;
ImgeracOpen.RenderTransform = CT;
}
void Composite_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// scale the image
FrameworkElement Elem = sender as FrameworkElement;
CompositeTransform CT = Elem.RenderTransform as CompositeTransform;
if (CT != null)
{
CT.ScaleX *= e.Delta.Scale;
CT.ScaleY *= e.Delta.Scale;
CT.CenterX = Elem.ActualWidth / 2;
CT.CenterY = Elem.ActualHeight / 2;
if (CT.ScaleX < 0.25) CT.ScaleX = 0.25;
if (CT.ScaleY < 0.25) CT.ScaleY = 0.25;
if (CT.ScaleX > 1.15) CT.ScaleX = 1.15;
if (CT.ScaleY > 1.15) CT.ScaleY = 1.15;
}
double Left = Canvas.GetLeft(Elem);
double Top = Canvas.GetTop(Elem);
Left += e.Delta.Translation.X;//Get x cordinate
Top += e.Delta.Translation.Y;//Get y cordinate
//check for bounds
if (Left < 0)
{
Left = 0;
}
else if (Left > (my_canvas.ActualWidth - Elem.ActualWidth))
{
Left = my_canvas.ActualWidth - Elem.ActualWidth;
}
if (Top < 0)
{
Top = 0;
}
else if (Top > (my_canvas.ActualHeight - Elem.ActualHeight))
{
Top = my_canvas.ActualHeight - Elem.ActualHeight;
}
Canvas.SetLeft(Elem, Left);
Canvas.SetTop(Elem, Top);
提前致谢
答案 0 :(得分:1)
当我放大并缩小图像时,它的边界会发生变化,并且不会拖动整个画布。
这是因为当您缩放图片时,图片的ActualWidth
和ActualHeight
不会发生变化,Left
和{{1}也会发生变化你来自Top
和Canvas.GetLeft
。
要解决此问题,您需要在图片缩放时调整Canvas.GetTop
和Left
值:
如图所示。当图像缩放时,Top
值应该是Left
- Canvas.Width
- Image.ActualWidth
当图像到达右边界时。当它到达左边界时xOffset
应该等于Left
。
通过这个逻辑,xOffset
中的代码应该修改如下:
Composite_ManipulationDelta