網站首頁 編程語言 正文
和Winform中的GDI+相比,WPF提供了一組新的API用于顯示和編輯圖像。新API特點如下:
適用于新的或專用圖像格式的擴展性模型。
對包括位圖 (BMP)、聯合圖像專家組 (JPEG)、可移植網絡圖形 (PNG)、標記圖像文件格式 (TIFF)、Microsoft Windows Media 照片、圖形交換格式 (GIF) 和圖標 (.ico) 在內的本機圖像格式增強了性能和安全性。
高位深圖像數據的保留最多 32 位/通道。
非破壞性圖像縮放、裁切和旋轉。
簡化的顏色管理
支持文件內的專用元數據。
托管組件利用非托管基礎結構提供圖像與其他 WPF 功能(如用戶界面 (UI)、動畫和圖形)的無縫集成。托管組件還可以從 Windows Presentation Foundation (WPF) 圖像處理編解碼器擴展性模型獲益,利用該模型可以實現自動識別 WPF 中的新圖像格式。
大部分托管的 WPF 圖像處理 API 駐留在 System.Windows.Media.Imaging 命名空間中,不過,幾個重要的類型(如 ImageBrush 和 ImageDrawing)都駐留在 System.Windows.Media 命名空間,Image 駐留在 System.Windows.Controls 命名空間。
下面我通過一個簡單的示例演示一下新的API的使用方法:
圖像編碼格式轉換:
var imageStreamSource = File.OpenRead(@"r:\1\24.bmp");
var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
var bitmapFrame = decoder.Frames[0];
//在界面上顯示圖片
//image1.Source = bitmapFrame;
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(bitmapFrame);
encoder.Save(File.Create(@"r:\1\3.jpg"));
這個功能非常簡單,就是把一個bmp格式的圖片轉換為了一個jpg格式的圖片。這個示例也顯示了WPF的圖像處理的基本方式:
從解碼器(xxxDecoder)中獲取圖像信息
創建解碼器后,圖像信息就保存在Frames(雖然大部分圖像(jpg,bmp,png等)只有一幀,但GIF,ico等圖像有多幀)屬性中了。用編碼器(xxxEncoder)保持圖像信息
相應的,編碼時只要創建編碼器,并設置相應的幀即可。
圖像處理:
常用的圖像處理包括縮放、裁切和旋轉等,如下是一個將圖像旋轉90度的例子。
var imageStreamSource = File.OpenRead(@"r:\1\24.bmp");
var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
var bitmapFrame = decoder.Frames[0];
TransformedBitmap myRotatedBitmapSource = new TransformedBitmap();
myRotatedBitmapSource.BeginInit();
myRotatedBitmapSource.Source = bitmapFrame;
// 旋轉90度
myRotatedBitmapSource.Transform = new RotateTransform(90);
myRotatedBitmapSource.EndInit();
//旋轉
var rotate = new RotateTransform(90);
var rotatedBitMap = new TransformedBitmap(bitmapFrame, rotate);
image1.Source = rotatedBitMap;
////裁剪
//CroppedBitmap chainedBitMap = new CroppedBitmap(bitmapFrame,new Int32Rect(100, 0, (int)bitmapFrame.Width - 100, (int)bitmapFrame.Height));
////縮放
//var scare = new ScaleTransform(1.5, 2);
//var scaredBitMap = new TransformedBitmap(bitmapFrame, scare);
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rotatedBitMap));
//encoder.Frames.Add(BitmapFrame.Create(scaredBitMap));
//encoder.Frames.Add(BitmapFrame.Create(chainedBitMap));
encoder.Save(File.Create(@"r:\1\3.jpg"));
和上面的例子相比,這里就是多了一個TransformedBitmap變換,其實這和xaml中的變換時一樣的。
<Image Width="150" Margin="5" Grid.Column="0" Grid.Row="1">
<Image.Source>
<TransformedBitmap Source="/sampleImages/watermelon.jpg" >
<TransformedBitmap.Transform>
<RotateTransform Angle="90"/>
</TransformedBitmap.Transform>
</TransformedBitmap>
</Image.Source>
</Image>
其它變換也都可以參照xaml中處理方式進行,這里就不過多介紹了。
原文鏈接:https://www.cnblogs.com/TianFang/archive/2009/10/18/1585450.html
相關推薦
- 2022-08-30 Mac上出現多個版本選擇打開edge瀏覽器
- 2022-07-29 pytest?fixtures函數及測試函數的參數化解讀_python
- 2023-12-19 CentOS和Ubuntu中防火墻相關命令
- 2022-07-24 Python使用apscheduler模塊設置定時任務的實現_python
- 2022-09-12 Python中.py程序在CMD控制臺以指定虛擬環境運行_python
- 2022-03-03 【css】page-break-after 頁面打印分頁屬性
- 2022-10-03 C++深入探究重載重寫覆蓋的區別_C 語言
- 2022-03-14 關于跨域 Response to preflight request doesn‘t pass ac
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支