Compared to GDI+ in Winform, WPF provides a new set of APIs for displaying and editing images. The new API features are as follows:
Extensibility model suitable for new or dedicated image formats.
Enhance performance and security for native image formats including Bitmap (BMP), Joint Image Expert Group (JPEG), Portable Network Graphics (PNG), Marked Image File Format (TIFF), Microsoft Windows Media Photos, Graphic Exchange Format (GIF), and Icons (.ico).
High-bit-depth image data is retained up to 32 bits/channel.
Non-destructive image scaling, cropping, and rotation.
Simplified color management
Supports dedicated metadata in files.
Managed components utilize unmanaged infrastructure to provide seamless integration of images with other WPF features such as user interfaces (UI), animations, and graphics. The managed components can also benefit from the Windows Presentation Foundation (WPF) image processing codec extensibility model, which enables automatic identification of new image formats in WPF.
Most of the managed WPF image processing APIs reside in the namespace, however, several important types (such as ImageBrush and ImageDrawing) reside in the namespace, and Image resides in the namespace.
Below I will demonstrate how to use the new API through a simple example:
Image encoding format conversion:
var imageStreamSource = (@"r:\1\"); var decoder = (imageStreamSource, , ); var bitmapFrame = [0]; //Show pictures on the interface// = bitmapFrame; var encoder = new JpegBitmapEncoder(); (bitmapFrame); ((@"r:\1\"));
This function is very simple, which is to convert a BMP format image into a JPG format image. This example also shows the basic way of WPF image processing:
Get image information from the decoder (xxxDecoder)
After creating the decoder, the image information is saved in the Frames (although most images (jpg, bmp, png, etc.) have only one frame, but GIF, IICO and other images have multiple frames) attributes.Use an encoder (xxxEncoder) to maintain image information
Correspondingly, when encoding, just create an encoder and set the corresponding frame.
Image processing:
Commonly used image processing includes scaling, cropping, and rotation. The following is an example of rotating an image 90 degrees.
var imageStreamSource = (@"r:\1\"); var decoder = (imageStreamSource, , ); var bitmapFrame = [0]; TransformedBitmap myRotatedBitmapSource = new TransformedBitmap(); (); = bitmapFrame; // Rotate 90 degrees = new RotateTransform(90); (); //Rotation var rotate = new RotateTransform(90); var rotatedBitMap = new TransformedBitmap(bitmapFrame, rotate); = rotatedBitMap; ////Crop//CroppedBitmap chainedBitMap = new CroppedBitmap(bitmapFrame,new Int32Rect(100, 0, (int) - 100, (int))); //// Zoom//var scare = new ScaleTransform(1.5, 2); //var scaredBitMap = new TransformedBitmap(bitmapFrame, scare); var encoder = new JpegBitmapEncoder(); ((rotatedBitMap)); //((scaredBitMap)); //((chainedBitMap)); ((@"r:\1\"));
Compared with the above example, there is an additional TransformedBitmap transformation here, which is actually the same as the transformation in xaml.
<Image Width="150" Margin="5" ="0" ="1"> <> <TransformedBitmap Source="/sampleImages/" > <> <RotateTransform Angle="90"/> </> </TransformedBitmap> </> </Image>
Other transformations can also be processed in xaml, so I will not introduce them here.
This is all about this article about WPF image processing. I hope it will be helpful to everyone's learning and I hope everyone will support me more.