Bitmap bmp = new Bitmap("example.png"); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); // Perform some operation on the locked pixel data // ... bmp.UnlockBits(bmpData);
Bitmap bmp = new Bitmap("example.png"); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { // Manipulate pixel directly ptr[0] += 20; // Increase blue channel by 20 ptr += 3; // Move to next pixel } ptr += bmpData.Stride - (bmp.Width * 3); // Move to next scanline } } bmp.UnlockBits(bmpData);
Bitmap bmp = new Bitmap(100, 100); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); unsafe { byte* ptr = (byte*)bmpData.Scan0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { // Set pixel directly ptr[0] = 255; // Set red channel to max ptr[1] = 255; // Set green channel to max ptr[2] = 255; // Set blue channel to max ptr += 3; // Move to next pixel } ptr += bmpData.Stride - (bmp.Width * 3); // Move to next scanline } } bmp.UnlockBits(bmpData);The System UnlockBits method is part of the System.Drawing package library, which is responsible for all the graphical rendering of bitmap, vector and other graphical image data in C# applications.