[C # Drawing] Bitmap locked into system memory

Source: Internet
Author: User

The implementation of the Bitmap.lockbits method is to say that the bitmap is locked into the system memory.

Using the LockBits method, you can lock an existing bitmap in system memory for programmatic modification. Although a large-scale change with lockbits can achieve better performance, you can still use the SetPixel method to change the color of the image.

The type of the return value of the function is BitmapData, which contains the attributes of the selected bitmap area.

Private voidlockunlockbitsexample (PaintEventArgs e) {//Create a new bitmap.Bitmap BMP =NewBitmap ("c:\\fakephoto.jpg"); //Lock the bitmap ' s bits. Rectangle rect =NewRectangle (0,0, BMP. Width, BMP.    Height); System.Drawing.Imaging.BitmapData Bmpdata=bmp. LockBits (Rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, BMP.    PixelFormat); //Get The address of the first line.IntPtr ptr =Bmpdata.scan0; //Declare an array to hold the bytes of the bitmap.    intbytes = Math.Abs (bmpdata.stride) *bmp.    Height; byte[] Rgbvalues =New byte[bytes]; //Copy the RGB values into the array.System.Runtime.InteropServices.Marshal.Copy (PTR, rgbvalues,0, bytes); //Set Every third value to 255.  A 24bpp bitmap would look red.      for(intCounter =2; Counter < Rgbvalues.length; Counter + =3) Rgbvalues[counter]=255; //Copy The RGB values back to the bitmapSystem.Runtime.InteropServices.Marshal.Copy (Rgbvalues,0, ptr, bytes); //Unlock the bits.bmp.    Unlockbits (Bmpdata); //Draw the modified image.E.graphics.drawimage (BMP,0, Max);}

[C # Drawing] Bitmap locked into system memory

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.