WPF中圖像處理的方法介紹
和Winform中的GDI+相比,WPF提供了一組新的API用于顯示和編輯圖像。新API特點(diǎn)如下:
適用于新的或?qū)S脠D像格式的擴(kuò)展性模型。
對(duì)包括位圖 (BMP)、聯(lián)合圖像專家組 (JPEG)、可移植網(wǎng)絡(luò)圖形 (PNG)、標(biāo)記圖像文件格式 (TIFF)、Microsoft Windows Media 照片、圖形交換格式 (GIF) 和圖標(biāo) (.ico) 在內(nèi)的本機(jī)圖像格式增強(qiáng)了性能和安全性。
高位深圖像數(shù)據(jù)的保留最多 32 位/通道。
非破壞性圖像縮放、裁切和旋轉(zhuǎn)。
簡(jiǎn)化的顏色管理
支持文件內(nèi)的專用元數(shù)據(jù)。
托管組件利用非托管基礎(chǔ)結(jié)構(gòu)提供圖像與其他 WPF 功能(如用戶界面 (UI)、動(dòng)畫和圖形)的無(wú)縫集成。托管組件還可以從 Windows Presentation Foundation (WPF) 圖像處理編解碼器擴(kuò)展性模型獲益,利用該模型可以實(shí)現(xiàn)自動(dòng)識(shí)別 WPF 中的新圖像格式。
大部分托管的 WPF 圖像處理 API 駐留在 System.Windows.Media.Imaging 命名空間中,不過(guò),幾個(gè)重要的類型(如 ImageBrush 和 ImageDrawing)都駐留在 System.Windows.Media 命名空間,Image 駐留在 System.Windows.Controls 命名空間。
下面我通過(guò)一個(gè)簡(jiǎn)單的示例演示一下新的API的使用方法:
圖像編碼格式轉(zhuǎn)換:
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"));
這個(gè)功能非常簡(jiǎn)單,就是把一個(gè)bmp格式的圖片轉(zhuǎn)換為了一個(gè)jpg格式的圖片。這個(gè)示例也顯示了WPF的圖像處理的基本方式:
從解碼器(xxxDecoder)中獲取圖像信息
創(chuàng)建解碼器后,圖像信息就保存在Frames(雖然大部分圖像(jpg,bmp,png等)只有一幀,但GIF,ico等圖像有多幀)屬性中了。用編碼器(xxxEncoder)保持圖像信息
相應(yīng)的,編碼時(shí)只要?jiǎng)?chuàng)建編碼器,并設(shè)置相應(yīng)的幀即可。
圖像處理:
常用的圖像處理包括縮放、裁切和旋轉(zhuǎn)等,如下是一個(gè)將圖像旋轉(zhuǎn)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; // 旋轉(zhuǎn)90度 myRotatedBitmapSource.Transform = new RotateTransform(90); myRotatedBitmapSource.EndInit(); //旋轉(zhuǎn) 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"));
和上面的例子相比,這里就是多了一個(gè)TransformedBitmap變換,其實(shí)這和xaml中的變換時(shí)一樣的。
<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中處理方式進(jìn)行,這里就不過(guò)多介紹了。
到此這篇關(guān)于WPF圖像處理的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#自定義序列化ISerializable的實(shí)現(xiàn)方法
這篇文章主要介紹了C#自定義序列化ISerializable的實(shí)現(xiàn)方法,涉及C#序列化的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04通過(guò)容器擴(kuò)展屬性IExtenderProvider實(shí)現(xiàn)WinForm通用數(shù)據(jù)驗(yàn)證組件
這篇文章介紹了通過(guò)容器擴(kuò)展屬性IExtenderProvider實(shí)現(xiàn)WinForm通用數(shù)據(jù)驗(yàn)證組件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12WPF實(shí)現(xiàn)頁(yè)面的切換的示例代碼
本文主要介紹了WPF實(shí)現(xiàn)頁(yè)面的切換的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01C#啟動(dòng)windows服務(wù)方法的相關(guān)問(wèn)題分析
C#啟動(dòng)windows服務(wù)的方法都是什么呢?C#啟動(dòng)服務(wù)類型為Disabled的windows服務(wù)會(huì)遇到什么樣的問(wèn)題呢?那么本文就向你介紹C#啟動(dòng)windows服務(wù)的方法的相關(guān)內(nèi)容2012-12-12C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探
這篇文章主要為大家詳細(xì)介紹了C#利用SharpPcap實(shí)現(xiàn)網(wǎng)絡(luò)包捕獲嗅探,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解
這篇文章主要介紹了C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要朋友們參考下。2019-08-08C# 靜態(tài)變量與靜態(tài)方法實(shí)例研究
寫了一個(gè)翻譯英漢單詞辭典的小程序,發(fā)現(xiàn)在調(diào)用幾千次的時(shí)候速度很慢2011-11-11