wpf Image 转 90 度

在WPF中旋转Image控件90度,可通过以下两种主流方法实现:


1. ‌使用BitmapImage.Rotation属性‌(静态旋转)

直接在XAML或代码中设置图像的旋转属性,适用于加载时固定角度的旋转。

XAML实现示例‌:

复制代码
<Image Width="150">
    <Image.Source>
        <BitmapImage UriSource="/sample.jpg" Rotation="Rotate90" />
    </Image.Source>
</Image>

C#代码实现‌:

复制代码
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("pack://application:,,/sample.jpg");
bi.Rotation = Rotation.Rotate90;  // 支持Rotate90/180/270
bi.EndInit();
imageControl.Source = bi;

特点‌:

  • 仅支持90度的倍数旋转(90/180/270)。
  • 旋转在图像解码阶段完成,性能更优。

2. ‌使用RotateTransform‌(动态旋转)

通过变换(Transform)实现灵活旋转,支持任意角度和运行时调整。

XAML实现‌:

复制代码
<Image Source="sample.jpg">
    <Image.RenderTransform>
        <RotateTransform Angle="90" CenterX="0.5" CenterY="0.5" />
    </Image.RenderTransform>
</Image>

C#代码动态旋转‌:

复制代码
RotateTransform rotateTransform = new RotateTransform(90);
rotateTransform.CenterX = imageControl.ActualWidth / 2;  // 设置旋转中心为图像中点
rotateTransform.CenterY = imageControl.ActualHeight / 2;
imageControl.RenderTransform = rotateTransform;

特点‌:

  • 支持任意角度(如45度)和动画效果。
  • 需手动设置CenterX/Y控制旋转中心(默认左上角)。

选择建议:

  • 静态旋转 ‌:优先使用BitmapImage.Rotation,性能更佳。
  • 动态效果 ‌:选择RotateTransform,可结合Storyboard实现动画。
相关推荐
晓纪同学1 天前
WPF-03 第一个WPF程序
大数据·hadoop·wpf
光电大美美-见合八方中国芯1 天前
用于无色波分复用光网络的 10.7 Gb/s 反射式电吸收调制器与半导体光放大器单片集成
网络·后端·ai·云计算·wpf·信息与通信·模块测试
晓纪同学1 天前
WPF-02体系结构
wpf
晓纪同学1 天前
WPF-01概述
wpf
海盗12341 天前
OxyPlot 在 WPF 中的使用
.net·wpf
晓纪同学1 天前
WPF-04 XAML概述
wpf
△曉風殘月〆2 天前
如何在WPF中捕获窗口外的事件
wpf
爱吃烤鸡翅的酸菜鱼3 天前
Java 事件发布-订阅机制全解析:从原生实现到主流中间件
java·中间件·wpf·事件·发布订阅
武藤一雄4 天前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
CSharp精选营4 天前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform