Bitmap lockbits and cloning
Can someone familiar with GDI shed some light on why the following s开发者_如何学Goequence throws?
var b = new Bitmap("some file");
var bd= b.LockBits(rect , readonly, px); //correct size and pixel type
var clone = (Bitmap)b.Clone();
var cd = clone.LockBits(rect , readonly , px); //okay
clone.UnlockBits(cd); //okay
b.UnlockBits(bd); //throws -- why?
It doesn't throw if I clone before locking the first bitmap, which is the behavior I expected.
I'd also expect that if it lets you clone a locked image, and then allows you to lock/unlock the clone, that the original wouldn't be affected.
I think Bitmap.Clone()
does not make a deep copy and the data is shared.
Edit: Following the advice given below, move the clone line just after var b
and make it like this: var clone = new Bitmap(b);
. It works now.
精彩评论