我知道这是以前问过的。但是我无法使标记的解决方案有效。
我正在使用Wpf和MVVM模式。我认为绑定将起作用。但事实并非如此。
我现在正在执行的程序是在图像中绘制灰色矩形。所有图形均已放置到位。除非矩形已旋转。
这是我的项控件的结构。
<ItemsControl ItemsSource="{Binding RectItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="canvas" Background="Transparent" Height="{Binding ElementName=img, Path=ActualHeight}" Width="{Binding ElementName=img, Path=ActualWidth}"
>
<Canvas.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleX="{Binding zoomValue}" ScaleY="{Binding zoomValue}"/>
</TransformGroup>
</Canvas.LayoutTransform>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="{Binding Width}"
Height="{Binding Height}"
Fill="Transparent"
Stroke="Blue"
StrokeThickness="1" RenderTransformOrigin=".5,.5" >
<Rectangle.LayoutTransform>
<RotateTransform Angle="{Binding Rotation}"/>
</Rectangle.LayoutTransform>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是示例绘制的图像
旋转的是绘制的矩形
但是旋转时我无法获得更新的坐标。
我什至尝试读取画布孩子的坐标。
就像下面这样:
class CanvasMouseRightButtonDown : Behavior<Canvas>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseRightButtonDown += AssociatedObject_MouseRightButtonDown;
}
private void AssociatedObject_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
foreach (var item in AssociatedObject.Children)
{
double X = Canvas.GetLeft((UIElement)item);
Debug.WriteLine("x: " + X);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseRightButtonDown -= AssociatedObject_MouseRightButtonDown;
}
}
我什至使用此类
class Maths
{
public static Point UpdatePoint(Point p, Point origin, double radian)
{
double dx = p.X - origin.X;
double dy = p.Y - origin.Y;
return new Point(
(dx * Math.Cos(radian) - dy * Math.Sin(radian)) + origin.X,
(dx * Math.Sin(radian) + dy * Math.Cos(radian)) + origin.Y
);
}
public static double Min(params double[] values)
{
if (values.Length == 0) return 0;
if (values.Length == 1) return values[0];
double val = values[0];
for (int i = 1; i < values.Length; i++)
{
val = (values[i] < val) ? values[i] : val;
}
return val;
}
public static double Max(params double[] values)
{
if (values.Length == 0) return 0;
if (values.Length == 1) return values[0];
double val = values[0];
for (int i = 1; i < values.Length; i++)
{
val = (values[i] > val) ? values[i] : val;
}
return val;
}
public static Rect GetBounds(Point p1, Point p2, Point p3, Point p4)
{
double l = Min(p1.X, p2.X, p3.X, p4.X);
double t = Min(p1.Y, p2.Y, p3.Y, p4.Y);
double r = Max(p1.X, p2.X, p3.X, p4.X);
double b = Max(p1.Y, p2.Y, p3.Y, p4.Y);
return new Rect(l, t, r - l, b - t);
}
public static Rect GetBounds(double width, double height, double angle)
{
double centerX = width / 2;
double centerY = height / 2;
var center = new Point(centerX, centerY);
var p1 = new Point(0, 0);
var p2 = new Point(centerX * 2, 0);
var p3 = new Point(centerX * 2, centerY * 2);
var p4 = new Point(0, centerY * 2);
double radian = angle * Math.PI / 180;
p1 = Maths.UpdatePoint(p1, center, radian);
p2 = Maths.UpdatePoint(p2, center, radian);
p3 = Maths.UpdatePoint(p3, center, radian);
p4 = Maths.UpdatePoint(p4, center, radian);
return GetBounds(p1, p2, p3, p4);
}
}
这就是我使用上面的类的方式
Maths.GetBounds(r.Width, r.Height, r.rotation)
然后我将getbounds的结果传递到
ImageMagick.MagickImage
但是绘制的矩形位置不正确,仅适用于旋转的矩形。
关于, 拉蒙