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实现动画。
相关推荐
听麟12 小时前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
笨蛋不要掉眼泪20 小时前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf
LcVong2 天前
WPF MediaPlayer获取网络视频流当前帧并展示图片完整范例
网络·wpf
bugcome_com2 天前
WPF数据绑定入门:从传统事件到5种绑定模式
wpf
LateFrames2 天前
我用 WPF 做了一个 “苍蝇飞舞” 的屏保
ui·wpf
wuty0072 天前
完善基于WPF开发的标尺控件(含实例代码)
wpf·wpf标尺·支持横向竖向标尺·ruler
浩浩测试一下3 天前
洪水猛兽攻击 Ddos Dos cc Drdos floods区别
安全·web安全·网络安全·系统安全·wpf·可信计算技术·安全架构
无心水3 天前
分布式环境下定时任务与SELECT FOR UPDATE的陷阱与解决方案
分布式·后端·wpf·xxl-job·quartz·定时任务·selectforupdate
xdpcxq10293 天前
Spring AOP + Guava RateLimiter 用注解实现优雅限流
spring·wpf·guava
Aevget4 天前
界面控件DevExpress WPF v25.2新版亮点:模板工具包全新升级
wpf·界面控件·devexpress·ui开发·.net 10