按钮上的旋转图像单击 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16300984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotating image on button click WPF
提问by HHH
I have included images on a canvas on my WPF user control:
我在 WPF 用户控件的画布上包含了图像:
<Canvas>
<Thumb Canvas.Left="19" Canvas.Top="-7" Canvas.ZIndex="99" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Name="Image1" Source="/Images/Image1.png" Height="120" Width="242" Margin="63,180,696,393" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="52" Canvas.Top="6" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Name="Image2" Source="/Images/Image2.png" Height="120" Width="205" Margin="760,184,74,397" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="21" DragDelta="Thumb_DragDelta" Canvas.Top="-18">
<Thumb.Template>
<ControlTemplate>
<Image Name="Image3" Source="/Images/Image3.png" Height="124" Width="260" Margin="82,426,697,151" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb Canvas.Left="30" Canvas.Top="1" DragDelta="Thumb_DragDelta">
<Thumb.Template>
<ControlTemplate>
<Image Name="Image4" Source="/Images/Image4.png" Height="124" Width="255" Margin="744,341,39,236" />
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Canvas>
I have then included the following in the code behind, so that these thumb images can be moved around on screen, which works fine.
然后我在后面的代码中包含了以下内容,以便这些拇指图像可以在屏幕上移动,这很好用。
// Move images on canvas
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
UIElement thumb = e.Source as UIElement;
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
}
Does anyone know a quick add where I can add to this code with the thumbs on the canvas so that I have the option to rotate these images on a button click in different directions and different angles?
有谁知道一个快速添加的地方,我可以用画布上的拇指添加到此代码中,以便我可以选择在不同方向和不同角度单击按钮时旋转这些图像?
Thanks
谢谢
回答by Clemens
You may assign a RotateTransform to the RenderTransformproperty of your Thumbs and modify the Angleof the RotateTransform in an event handler:
您可以将 RotateTransform 分配给 Thumbs 的RenderTransform属性,并Angle在事件处理程序中修改RotateTransform 的属性:
<Thumb Canvas.Left="52" Canvas.Top="7" RenderTransformOrigin="0.5,0.5"
DragDelta="Thumb_DragDelta" MouseDoubleClick="Thumb_MouseDoubleClick">
<Thumb.RenderTransform>
<RotateTransform/>
</Thumb.RenderTransform>
<Thumb.Template>
...
</Thumb.Template>
</Thumb>
The event handler may look like this:
事件处理程序可能如下所示:
private void Thumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var thumb = e.Source as UIElement;
var transform = thumb.RenderTransform as RotateTransform;
transform.Angle += 90;
}
You should however change the large Marginvalues on your Thumbs, as they affect the rotation center.
但是,您应该更改Margin拇指上的大值,因为它们会影响旋转中心。
回答by Shafqat Masood
Canvas container = new Canvas();
container.Children.Add(image);
container.Arrange(new Rect(0, 0, source.PixelWidth, source.PixelHeight));
// render the result to a new bitmap.
RenderTargetBitmap target = new RenderTargetBitmap(targetWidth, targetHeight, sourceDpiX, sourceDpiY, PixelFormats.Default);
target.Render(container);
RenderTargetBitmap can be use it in place of a BitmapSource in any control.
RenderTargetBitmap 可以用来代替任何控件中的 BitmapSource。

