Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Bitmap.Clone()

Started by TheUnbeliever Jul 13, 2008 at 2:41 PM 3 replies 2.3k views
Original Post
TheUnbeliever
TheUnbeliever
Does this method actually do a deep copy? The Microsoft docs claim it does, but either I'm missing something or the docs or wrong (i.e. the former is the case). I get a bitmap, clone it and then do some destructive processing of the clone - but this also appears to affect the original bitmap (literally all I'm doing is face.Image = (Bitmap)originalImage.Clone();; face then does some levels work (amongst other things), but, at viewing, both face.Image and originalImage appear to have been altered).
[TheUnbeliever]
Headkaze
Headkaze
Maybe try

Bitmap bitmapCopy = new Bitmap(originalImage);face.Image = bitmapCopy;

TheUnbeliever
TheUnbeliever
That looks like it works, thanks. Unfortunately, it's also disclosed an odd problem elsewhere that makes it a little difficult to tell, but c'est la vie. :-)
[TheUnbeliever]
Spodi
Spodi
For questions like this, Reflector will probably be your best friend.

public Bitmap Clone(Rectangle rect, PixelFormat format){    if ((rect.Width == 0) || (rect.Height == 0))    {        throw new ArgumentException(SR.GetString("GdiplusInvalidRectangle", new object[] { rect.ToString() }));    }    IntPtr zero = IntPtr.Zero;    int status = SafeNativeMethods.Gdip.GdipCloneBitmapAreaI(rect.X, rect.Y, rect.Width, rect.Height, (int) format, new HandleRef(this, base.nativeImage), out zero);    if ((status != 0) || (zero == IntPtr.Zero))    {        throw SafeNativeMethods.Gdip.StatusException(status);    }    return FromGDIplus(zero);}


Judging by the name of GdipCloneBitmapAreaI, it looks like it. Unfortunately this isn't the best example of Reflector's usefulness. ;)
NetGore - Open source multiplayer RPG engine
TheUnbeliever
TheUnbeliever
Hm, that's interesting. I've used Reflector occasionally before, but didn't realize that it could scrutinize the libraries (although, thinking about it, it makes sense - they're no different from normal assemblies).

I've got very odd things happening that I'm totally bamboozled, so it's quite possible that this wasn't actually the problem.

Cheers!
[TheUnbeliever]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.