Click here to Skip to main content
15,885,213 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Getting (Bitmap region is already locked) error when locking two bitmaps to compare pixels.



C#
Bitmap b = (Bitmap)pictureBox1.Image;
            Bitmap b2 = (Bitmap)pictureBox1.Image;

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bmData2 = b2.LockBits(new Rectangle(b.Width, b.Height, b2.Width, b2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            int stride2 = bmData2.Stride;

			unsafe
			{
                byte* p = (byte*)bmData.Scan0;
                byte* p2 = (byte*)bmData2.Scan0;

                int nOffset,nOffSet2;
                int nWidth,nWidth2;

                NOffSet(b, stride, out nOffset, out nWidth);
                NOffSet(b2, stride2, out nOffSet2, out nWidth2);
	
				for(int y=0;y<b.Height;++y)
				{
                    for (int x = 0; x < nWidth; ++x)
                    {
                        if (p[0].Equals(p2[0]))
                            listBox1.Items.Add(string.Format("({0},{1}) :{2}", x, y, p2[0]));
                        ++p; ++p2;

                    }
					p += nOffset;
				}
			}

			b.UnlockBits(bmData);
Posted
Updated 9-Jun-14 12:40pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Jun-14 18:24pm    
Why doing that?
—SA
Mohammed Nabeel Khan 9-Jun-14 18:41pm    
comparing two bitmaps

1 solution

What is unclear in this error message? It is quite straightforward. You are doing wrong thing: you cannot lock bits on the same Bitmap b twice and work with bmpData and bmpData2 at the same time. And, in practice, it's never needed. You can create bitmap data once, do what you need to do with that and, if you need to do something different, lock bits again later. In worst case, you can create a clone of the same bitmap and lock those two different bitmaps.

—SA
 
Share this answer
 
Comments
Mohammed Nabeel Khan 9-Jun-14 18:41pm    
thank sergey Alexandrovich , question updated
Sergey Alexandrovich Kryukov 9-Jun-14 18:50pm    
Where?
Ah, I see, b and b2 now. And what, still an exception? Where? Are the Solution 2 shows pictireBox1 and pictureBox2. Which code did you actually use.
In first case, b and b2 are still the same, because the same pictureBox1 will return to reference to the same bitmap object, so you have exact same problem.
The solution is apparent: don't try to use two different instances of bitmap data. How? it depends on what you want to do with this data...
—SA
Mohammed Nabeel Khan 9-Jun-14 19:12pm    
what should i do?
how to compare pixels of both bitmaps ? should i store them in array?

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