Edit image pixels in WPF

Source: Internet
Author: User

[Note: For your convenience, the hyperlinks in this article are connected to the relevant content in msdn .]

The previous day I started to study WPF. It starts from the display, editing, and storage of images.

In my opinion, the changes in image processing between WPF and previous forms are too great. They use different classes to represent images. In the from clauseImageIn WPF,ImageIt is used as a control to display images.Imagesource,Bitmapsource,Bitmapimage,Writeablebitmap. It is not clear about the features and specific applications of these classes.

But the problem encountered a few days ago (http://student.csdn.net/space.php? Uid = 127131 & Do = thread & id = 10182.

Now let's summarize:

1. First, you must understand the image format.

The source images I use are in JPG or BMP format. In these two formats, the image is generally represented by three elements: R, G, and B. Therefore, each pixel in the program is represented by three byte variables.

It should also be said here. When pixel reading is performed on an image, the width and height of the image should be obtained using the pixelwidth and pixelheight attributes. In WPF, the width and height attributes are of the double type. They are used to indicate the display size of the image, rather than the actual size.

2. Read and use the image control to display images.

In WPF, many file paths need to be convertedUriClass objects can be easily used. To read a piece, use openfiledialog to first find the image path filename (of course, string type ). Create a new URI fileuri = new uri (filename );. Then, I useBitmapdecoderClass decodes the image and sends the decoded image to the image control for display. As for the last two parameters in create, I don't quite understand either. I only know that this is correct.

Code:
  1. Uri fileuri = new uri (filename );
  2. Bitmapdecoder decoder = bitmapdecoder. Create (fileuri, bitmapcreateoptions. preservepixelformat, bitmapcacheoption. Default );
  3. Image1.source = decoder. Frames [0];


3. Find a method to read the pixel of the image and write the modified pixel back.

Bitmapsource, bitmapimage, and writeablebitmap all haveCopypixelsMethod. This method can be used to read the pixels in the image. Its prototype is public virtual void copypixels (array pixels, int stride, int offset). [This is only a reload method ].

Array pixels: After this function is executed, the value in this variable is the image pixel data to be obtained. Before calling this function, you can declare a byte array (for example, byte [] imgpixels = new byte [number of pixels × number of elements contained in each pixel]);

Int stride: original meaning of stride, stride. After my analysis, I feel that it indicates the number of bytes required for a row of the image. That is, [number of pixels per line × number of elements per pixel ];

Int offset: This is a better understanding, that is, where to copy pixels (positions expressed in pixels.

4. Operate on pixel data (imgpixels array.

I will not talk about it here. You can do it at will!

5. Use the modified data to generate a new image.

As mentioned above, bitmapsource, bitmapimage, and writeablebitmap can be used to obtain pixels, but only writeablebitmap can be used to generate images using pixels. Because only it hasWritepixelsMethod. The prototype is public void writepixels (int32rect sourcerect, array pixels, int stride, int offset). [This is only an overloaded method ].

Int32rect sourcerect: the region to be updated, that is, the region to be changed using pixel data. If you create a new image, it is new int32rect (, pixelwidth, pixelheight;

Array pixels and INT stride will not be explained much. The meaning and usage are the same as above;

Int offset: it is worth mentioning. Here, it refers to the offset of the pixel data, that is, the offset of the pixels array, rather than the offset of the pixel.

In this case, writeablebitmap images can be easily converted to other image types.

6. Save the generated image.

Because I operate on pixels and enable the image to be opened again to know what operations I have performed on the object level, I save the image as BMP. The focus here is to use the bitmapencoder class to encode the image. Of course, different image formats have different classes inherited fromBitmapencoderClass. Here we use the bitmapencoder class subclass.BMP bitmapencoder. The Code is as follows:

Code:
  1. Static public void saveimage (string file, bitmapsource IMG)
  2. {
  3. BMP bitmapencoder encoder = new BMP bitmapencoder ();
  4. Encoder. frames. Add (bitmapframe. Create (IMG ));
  5. Filestream bitmap = new filestream (file, filemode. Create, fileaccess. Write );
  6. Encoder. Save (Bitmap );
  7. Bitmap. Close ();
  8. }

In this way, you can try it if you are interested.

In addition, I am a beginner in WPF. What is wrong? Please help me with criticism and guidance!

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.