c#.net中的WPF图像旋转 [英] WPF image rotation in c#.net
问题描述
嗨
i在wpf中创建了一个图像我希望在我的winform应用程序中旋转该WPF图像,以更改0dgree到180dgree旋转的数值更新计数器的值..?
请记住,这是一个猜测,但我想说最简单的方法可能是应用RotateTransform
到图像
的RenderTransform
属性。RenderTransform
是一个DependencyProperty
所以你应该能够将它绑定到Value
NumericUpDown
的属性。然而,你需要一个IValueConverter
,它将NumericUpDown
中的值转换为RotateTransform
。IValueConverter
的代码如下所示:
public class RotationsTransformConverter
:IValueConverter
{
public object 转换( object value ,输入targetType, object 参数,CultureInfo文化)
{
double 角度;
尝试
{
angle =( double )值跨度>;
}
catch
{
angle = 0 。 0 ;
}
RotateTransform transformation = new RotateTransform(angle);
return 转换;
}
public object ConvertBack( object value ,输入targetType, object 参数,CultureInfo文化)
{
throw new NotImplementedException();
}
}
一旦你有了你的转换器,你将不得不在某处实例化它。我建议将其设置为资源。
< < span class =code-leadattribute> local:RotationsTransformConverter x:key = MyConverter / >
一旦转换器启动并准备就绪,你就可以从图像
的RenderTransform
属性为NumericUpDown
的价值
属性。在Binding
的转换器
属性中,您将引用转换为StaticResource
。该绑定看起来像这样:
< 图像 RenderTransform = {Binding Path =,Mode = OneWay,UpdateSourceTrigger = PropertyChanged,Converter = {StaticResource MyConverter}} />
如果仔细观察绑定,你会发现我把路径留空了。我不知道<$ c $的路径是什么c>图像到NumericUpDown
是,所以我打算让你填写那部分。
< blockquote>您可以将RotateTransform指定给Thumbs的RenderTransform属性,并在事件处理程序中修改RotateTransform的Angle:
< thumb canvas.left = 52canvas.top =7rendertransformorigin =0.5,0.5> DragDelta =Thumb_DragDelta鼠标DoubleClick =Thumb_MouseDoubleClick>
< thumb.rendertransform> < rotatetransform>
< thumb.template> ...
事件处理程序可能如下所示:
private void Thumb_MouseDoubleClick(object sender,MouseButtonEventArgs e)
{
var thumb = e.Source as UIElement;
var transform = thumb.RenderTransform as RotateTransform;
transform.Angle + = 90;
}
然而,您应该更改拇指上的大边距值,因为它们会影响旋转中心。
hi
i have creted an image in wpf i want to rotate that WPF image in my winform application on changing the vlaue of numerical updown counter with 0dgree to 180dgree rotation ..?
Keep in mind that this is a guess, but I would say that the easiest way to do this might be applying aRotateTransform
to theImage
'sRenderTransform
property.RenderTransform
is aDependencyProperty
so you should be able to bind it to theValue
property of theNumericUpDown
. Yet, you are going to need anIValueConverter
that transforms the value from theNumericUpDown
into aRotateTransform
. The code for theIValueConverter
will look something like this:
public class RotationsTransformConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double angle; try { angle = (double) value; } catch { angle = 0.0; } RotateTransform transformation = new RotateTransform(angle); return transformation; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Once you have your converter, you are going to have to instantiate it somewhere. I would advise setting it as a resource.
<local:RotationsTransformConverter x:key="MyConverter"/>
Once the converter is up and ready to go, you create a binding from theImage
'sRenderTransform
property to theNumericUpDown
'sValue
property. In theBinding
'sConverter
property, you will reference the converter as aStaticResource
. That binding will look something like this:
<Image RenderTransform="{Binding Path=, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource MyConverter}}/>
If you look closely at the binding, you will notice that I left the path blank. I don't know what the path from theImage
to theNumericUpDown
is, so I'm going to let you fill that part out.
You may assign a RotateTransform to the RenderTransform property of your Thumbs and modify the Angle of the RotateTransform in an event handler:
<thumb canvas.left="52" canvas.top="7" rendertransformorigin="0.5,0.5"> DragDelta="Thumb_DragDelta" MouseDoubleClick="Thumb_MouseDoubleClick">
<thumb.rendertransform> <rotatetransform>
<thumb.template> ...
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 Margin values on your Thumbs, as they affect the rotation center.
这篇关于c#.net中的WPF图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!