C # three methods for processing image pixels

Source: Internet
Author: User

In C #, you can use the getpixel method, the memory copy method, and the pointer method (unsafe) to obtain and process the image pixels.

The following uses the grayscale image as an example to describe the comparison of the specific processing method and speed (1 GB memory, P4 processor testing ).

1. getpixel Method

Getpixel (I, j) and setpixel (I, j, color) can directly obtain the color structure of an image pixel, but the processing speed is relatively slow, processing a pair of 180*180 images takes about 100.48 Ms.

 Private   Void Pixel_click ( Object Sender, eventargs e ){ If (Curbitmap! = Null ) {Mytimer. cleartimer (); mytimer. Start (); color curcolor; Int RET; For ( Int I = 0; I <curbitmap. width; I ++ ){ For ( Int J = 0; j <curbitmap. height; j ++) {curcolor = curbitmap. getpixel (I, j); ret = ( Int ) (Curcolor. R * 0.299 + curcolor. g * 0.587 + curcolor. B * 0.114); curbitmap. setpixel (I, j, color. fromargb (Ret, RET, RET);} mytimer. stop (); timebox. TEXT = mytimer. duration. tostring (" ####.## ") +" Millisecond "; Invalidate ();}}
2. memory copy method
 
The memory copy method uses system. runtime. interopservices. marshal. COPY Copies the image data to the array and then processes it. You do not need to directly operate the pointer without using unsafe. The processing speed is not much different from the pointer processing speed,
Processing a pair of 180*180 images takes about 1.32 Ms.
 Private   Void Memory_click ( Object Sender, eventargs e ){ If (Curbitmap! = Null ) {Mytimer. cleartimer (); mytimer. Start (); rectangle rect = New Rectangle (0, 0, curbitmap. width, curbitmap. height); system. drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat); intptr = BMP data. scan0; Int Bytes = curbitmap. Width * curbitmap. Height * 3;Byte [] Rgbvalues = New   Byte [Bytes]; system. runtime. interopservices. Marshal. Copy (PTR, rgbvalues, 0, bytes ); Double Colortemp = 0; For ( Int I = 0; I <rgbvalues. length; I + = 3) {colortemp = rgbvalues [I + 2] * 0.299 + rgbvalues [I + 1] * 0.587 + rgbvalues [I] * 0.114; rgbvalues [I] = rgbvalues [I + 1] = rgbvalues [I + 2] = ( Byte ) Colortemp;} system. runtime. interopservices. marshal. copy (rgbvalues, 0, PTR, bytes); curbitmap. unlockbits (BMP data); mytimer. stop (); timebox. TEXT = mytimer. duration. tostring (" ####.## ") +" Millisecond "; Invalidate ();}}
3. pointer Method
 
The pointer is an unsafe operation in C # And must be enclosed in unsafe mode for processing. The fastest speed is. processing a 180*180 image takes about 1.16 Ms.
 
UseByte* PTR = (Byte*) (BMP data. scan0); obtain the pointer to the root position of the image data, and then use BMP data. scan0 to obtain the scanning width of the image. Then you can perform the pointer operation.
 
 
 Private   Void Pointer_click ( Object Sender, eventargs e ){If (Curbitmap! = Null ) {Mytimer. cleartimer (); mytimer. Start (); rectangle rect = New Rectangle (0, 0, curbitmap. width, curbitmap. height); system. drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat ); Byte Temp = 0; Unsafe { Byte * PTR = ( Byte *) (BMP data. scan0 ); For ( Int I = 0; I <BMP data. height; I ++ ){ For ( Int J = 0; j <BMP data. width; j ++) {temp = ( Byte ) (0.299 * PTR [2] + 0.587 * PTR [1] + 0.114 * PTR [0]); PTR [0] = PTR [1] = PTR [2] = temp; PTR + = 3;} PTR + = BMP data. stride-BMP data. width * 3 ;}} curbitmap. unlockbits (BMP data); mytimer. stop (); timebox. TEXT = mytimer. duration. tostring (" ####.## ") +" Millisecond "; Invalidate ();}}

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.