WPF圖像格式如何轉(zhuǎn)換
WPF圖像格式可以通過(guò)我們的需求進(jìn)行各種轉(zhuǎn)換。那么在編碼轉(zhuǎn)換中,我們要如何正確的處理呢?對(duì)于初學(xué)者來(lái)說(shuō)可能不太好理解。#t#
- var imageStreamSource =
File.OpenRead(@"r:\1\24.bmp"); - var decoder = BitmapDecoder.
Create(imageStreamSource, Bitmap
CreateOptions.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圖像格式處理的基本方式:
1、從解碼器(xxxDecoder)中獲取圖像信息
創(chuàng)建解碼器后,圖像信息就保存在Frames(雖然大部分圖像(jpg,bmp,png等)只有一幀,但GIF,ico等圖像有多幀)屬性中了。
2、用編碼器(xxxEncoder)保持圖像信息
相應(yīng)的,WPF圖像格式編碼時(shí)只要?jiǎng)?chuàng)建編碼器,并設(shè)置相應(yīng)的幀即可。