Introduction to image processing in WPF

Source: Internet
Author: User
Tags new set

Original: Introduction to image processing in WPF

Compared to GDI + in WinForm, WPF provides a new set of APIs for displaying and editing images. The new API features the following:

    1. Extensibility model for new or dedicated image formats.
    2. Enhanced native image formats including bitmap (BMP), joint Image Expert Group (JPEG), Portable Network Graphics (PNG), Tagged Image File format (TIFF), Microsoft Windows Media photo, Graphics Interchange Format (GIF), and icon (. ico) Energy and safety.
    3. High-depth image data is retained up to 32 bits per channel.
    4. Non-destructive image scaling, cropping, and rotation.
    5. Simplified Color Management
    6. Supports private metadata within a file.
    7. Managed components leverage an unmanaged infrastructure to provide seamless integration of images with other WPF features, such as user interface (UI), animations, and graphics. Managed components can also benefit from the Windows Presentation Foundation (WPF) Image processing codec extensibility model, which enables the automatic recognition of new image formats in WPF.

Most of the hosted WPF image processing APIs reside in the System.Windows.Media.Imaging namespace, but several important types, such as ImageBrush and imagedrawing, reside in the System.Windows.Media namespace, Image resides in the System.Windows.Controls namespace.

Let me show you how to use the new API using a simple example:

Image Encoding Format conversion:

var imagestreamsource = File. OpenRead (@ "r:\1\24.bmp");
var decoder = Bitmapdecoder. Create (Imagestreamsource, bitmapcreateoptions. Preservepixelformat, bitmapcacheoption. Default);
var bitmapframe = decoder. Frames[0];

// display pictures on the interface
Image1. Source = Bitmapframe;

var encoder = newjpegbitmapencoder();
Encoder. Frames.add (Bitmapframe);
Encoder. Save (File. Create (@ "r:\1\3.jpg"));

This function is very simple, is to convert a BMP format picture to a JPG format. This example also shows the basic way to image processing for WPF:

    1. Obtaining image information from the decoder (xxxdecoder)
      Once the decoder is created, the image information is saved in the frames (although most images (jpg,bmp,png, etc.) have a single frame, but Gif,ico images have multiple frames).
    2. Maintain image information with encoder (xxxencoder)
      Accordingly, as long as the encoder is created and the corresponding frame is set.

Image processing:

Commonly used image processing includes scaling, cropping, and rotation, as shown below is an example of rotating an image 90 degrees.

var imagestreamsource = File. OpenRead (@ "r:\1\24.bmp");
var decoder = Bitmapdecoder. Create (Imagestreamsource, bitmapcreateoptions. Preservepixelformat, bitmapcacheoption. Default);
var bitmapframe = decoder. Frames[0];??

transformedbitmap myrotatedbitmapsource = newtransformedbitmap();
Myrotatedbitmapsource.begininit ();
Myrotatedbitmapsource.source = Bitmapframe;??

// rotation degree
Myrotatedbitmapsource.transform = newRotateTransform(90);
Myrotatedbitmapsource.endinit ();?

// Rotate

var rotate = newRotateTransform(90);
var rotatedbitmap = newtransformedbitmap(bitmapframe, rotate);

Image1. Source = Rotatedbitmap;

//// cropping
Croppedbitmap Chainedbitmap = new Croppedbitmap (bitmapframe,new int32rect (0, (int) bitmapframe.width-100, (int) Bitmapframe.height));

//// Zoom
var scare = new ScaleTransform (1.5, 2);
var scaredbitmap = new Transformedbitmap (bitmapframe, scare);

var encoder = newjpegbitmapencoder();
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"));

Compared to the above example, here is a transformedbitmap transformation, in fact, it is the same as the transformation in XAML.

<ImageWidth= "Max"Margin= "5"Grid.column= "0"Grid.Row = "1" >
<   Image.source >
<     TransformedbitmapSource= "/sampleimages/watermelon.jpg" >
<       Transformedbitmap.transform >
<         RotateTransformAngle = "/>"
</       Transformedbitmap.transform >
</     Transformedbitmap >
</    image.source>
</Image>

Other transformations can also be referred to in XAML for processing, not too much.

???

Introduction to image processing in WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.