wpf 和旋转转换出现问题
我正在尝试旋转画布上的元素并将其旋转(不是原始)位置保存到文件中。我实现了一个自定义 UIElement 控件来显示自定义图形,但是当图形在屏幕上旋转时,它会正确旋转(没有问题),但是当我使用 GetValue(Canvas.LeftProperty) 和 GetValue(Canvas) 获取元素的位置时.TopProperty),元素的X、Y坐标和角度是旋转前原始图像的位置。
我正在学习 WPF 来完成学校的一个项目,因此我对技术的了解并不像我想要的那么广泛,但如果有人可以帮助我,我将非常感激,谢谢。
这是我的代码的实现:
CustomObject m;
List<CustomObject> co = new List<CustomObject>();
foreach (var child in canvas1.Children){
m = child as CustomObject;
if (m != null && m.IsEnabled && m.IsVisible){
SaveStructure m1 = new SaveStructure();
<b>m1.Angle = Convert.ToSingle(ToRadians(m.Angle));</b>
<b>m1.X = Convert.ToInt32(m.GetValue(Canvas.LeftProperty));</b>
<b>m1.Y = Convert.ToInt32(m.GetValue(Canvas.TopProperty));</b>
co.Add(m1);
}
}
注意:我想知道的是如何获取画布上旋转元素的位置,因为我不断获取原始(未旋转)位置。
i am trying to rotate elements on a canvas and the save their rotated (not original) positions to a file. I implemented a custom UIElement control to display a custom graphic, however when the graphic is rotated on the screen it is rotated correctly (no problem there) however when i obtain the position of the element using GetValue(Canvas.LeftProperty) and GetValue(Canvas.TopProperty), the X, Y coordinates and the angle of the element is of position of the original image before rotation.
I am learning WPF to finish a project for school and thus my knowledge of the technology is not as vast as i would like but if anyone can help me i would greatly appreciate it, thank you.
this is the implementation of the code that i have:
CustomObject m;
List<CustomObject> co = new List<CustomObject>();
foreach (var child in canvas1.Children){
m = child as CustomObject;
if (m != null && m.IsEnabled && m.IsVisible){
SaveStructure m1 = new SaveStructure();
<b>m1.Angle = Convert.ToSingle(ToRadians(m.Angle));</b>
<b>m1.X = Convert.ToInt32(m.GetValue(Canvas.LeftProperty));</b>
<b>m1.Y = Convert.ToInt32(m.GetValue(Canvas.TopProperty));</b>
co.Add(m1);
}
}
Note: All i want to know is how to get the position of the rotated element on the canvas, because i keep obtaining the original (unrotated) position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您获得的位置仍然相同,因为对象没有移动,只是旋转了,如果您想获得旋转对象的边界,这与获取其位置不同。您可以通过获取元素的角点坐标来做到这一点(Canvas.GetLeft(m)、Canvas.GetTop(m)、Canvas.GetLeft(m) + m.Width、Canvas.GetTop(m) + m .Height)并使用
RotateTransform
的Transform(Point p)
方法旋转它们,然后从这些旋转点中提取边界。The position you get is still the same because the object was not moved, it was just rotated, if you want to get the bounds of the rotated object that is something different than getting its position. You could do that by getting the corner point coordinates of the elements (
Canvas.GetLeft(m), Canvas.GetTop(m), Canvas.GetLeft(m) + m.Width, Canvas.GetTop(m) + m.Height
) and rotate them using theRotateTransform
'sTransform(Point p)
method, then extract the bounds from those rotated points.