幾種常用WPF圖像處理方法介紹
作者:佚名
WPF圖像處理的方法有很多種,主要包括:縮放、裁切和旋轉(zhuǎn)等等。我們以一段代碼示例,來(lái)方便大家劣跡這些處理方法。
WPF圖像處理在試駕開(kāi)發(fā)中是非常有用的一個(gè)工具。開(kāi)發(fā)人員可以通過(guò)WPF圖像處理簡(jiǎn)單的實(shí)現(xiàn)精美的圖形界面顯示功能。這里就為大家簡(jiǎn)單介紹一下。#t#
常用的WPF圖像處理包括縮放、裁切和旋轉(zhuǎn)等,如下是一個(gè)將圖像旋轉(zhuǎn)90度的例子。
- var imageStreamSource =
File.OpenRead(@"r:\1\24.bmp"); - var decoder = BitmapDecoder.Create
(imageStreamSource, BitmapCreate
Options.PreservePixelFormat,
BitmapCacheOption.Default); - var bitmapFrame = decoder.Frames[0];
- TransformedBitmap myRotated
BitmapSource = 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 Trans
formedBitmap(bitmapFrame, rotate); - image1.Source = rotatedBitMap;
- ////裁剪
- //CroppedBitmap chainedBitMap =
new CroppedBitmap(bitmapFrame,
new Int32Rect(100, 0, (int)bitmap
Frame.Width - 100, (int)bitmap
Frame.Height)); - ////縮放
- //var scare = new ScaleTransform
(1.5, 2); - //var scaredBitMap = new Trans
formedBitmap(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"));
和上面的WPF圖像處理例子相比,這里就是多了一個(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中WPF圖像處理方式進(jìn)行,這里就不過(guò)多介紹了。
責(zé)任編輯:曹凱
來(lái)源:
博客園