dan
dan

Reputation: 9852

Bitmap lockbits and cloning

Can someone familiar with GDI shed some light on why the following sequence 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.

Upvotes: 2

Views: 1831

Answers (1)

Petar Minchev
Petar Minchev

Reputation: 47383

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.

Upvotes: 4

Related Questions