Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
    {

        // Create a new bitmap.
        Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

        // Lock the bitmap's bits.  
        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);  //<==== why do we need to lock this bmp?

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes  = 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 will look red.  
        for (int counter = 2; counter < rgbValues.Length; counter += 3)
            rgbValues[counter] = 255;

        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        // Unlock the bits.
        bmp.UnlockBits(bmpData);

        // Draw the modified image.
        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);  //<==== why do we need to lock this bmp?
Posted
Updated 20-May-22 22:28pm

1 solution

 
Share this answer
 
Comments
Southmountain 21-May-22 10:56am    
thanks for your time. I read this remark and wonder: if I don't use BitLock method, I can still manipulate this bimtmap in memory, maybe other threads can manipulate it at the same time?
Use the LockBits method to lock an existing bitmap in system memory so that it can be changed programmatically. You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.
Richard MacCutchan 21-May-22 12:05pm    
It's about getting direct access to the memory location of the Bitmap, rather than using the class methods.
Southmountain 21-May-22 13:50pm    
so this memory area needs to be locked to avoid other system processes overriding?
Richard MacCutchan 21-May-22 14:04pm    
As stated above. That is, to allow direct access to the bits of the map.
Southmountain 21-May-22 21:50pm    
thank you for your clarification!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900