位图区域已被锁定 [英] Bitmap region is already locked

查看:427
本文介绍了位图区域已被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取(位图区域已锁定)错误。







Getting (Bitmap region is already locked) error when locking two bitmaps to compare pixels.



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);

推荐答案

什么在此错误消息中不清楚?这很简单。你做错了事:你不能锁定同一个位图b 两次的位并使用 bmpData bmpData2 同时。而且,在实践中,它永远不需要。您可以创建一次位图数据,执行您需要执行的操作,如果需要执行其他操作,请稍后再次锁定位。在最坏的情况下,您可以创建相同位图的克隆并锁定这两个不同的位图。



-SA
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


这篇关于位图区域已被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆