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