I am testing a demo at
this link .
I know what this LockBits method does, but not 100% sure why we need to use it.
is there any possibility that other processes can modify this
bmpData
?
I copy the source code here for your convenience:
private void LockUnlockBitsExample(PaintEventArgs e)
{
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
e.Graphics.DrawImage(bmp, 0, 150);
}
What I have tried:
test this demo and did some research, still not thoroughly convinced why this bmp needs to get locked in memory:
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);