Jump to content
  • Advertisement
Sign in to follow this  
Thevenin

[.net] I do not feel like unlocking a Bitmap...

This topic is 4238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm after the raw pixels of a Bitmap object, and I'm often after the raw pixels of said bitmap object. Locking and unlocking it every frame of my game loop seems somewhat redundant. When you lock a Bitmap it returns the BitmapData that you use to manipulate the pixels. It is my understanding that when you unlock the Bitmap, the pointer given to the BitmapData is released, and will no longer work. What I'm hunching at is Marshall.Copy() the BitmapData so I have a local copy for use, after unlocking the Bitmap. I have no idea what is actually occuring during a lock/unlock though, so if anyone would mind explaining to me if this will work. And whether or not it is bad to leave it locked -- since I'm not rendering it, or even writing to it, just reading it. Here is my current code (pure .NET awesomeness):
MyBDSprites01 = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("MClient.Sprites01.PNG")).
                LockBits(new Rectangle(0, 0, MyTexSize, MyTexSize), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
MyBDSprites02 = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("MClient.Sprites02.PNG")).
                LockBits(new Rectangle(0, 0, MyTexSize, MyTexSize), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
MyBDTiles =     new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("MClient.Tiles.PNG")).
                LockBits(new Rectangle(0, 0, MyTexSize, MyTexSize), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
MyBDUI =        new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("MClient.UI.PNG")).
                LockBits(new Rectangle(0, 0, MyTexSize, MyTexSize), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

Share this post


Link to post
Share on other sites
Advertisement
Hi, I found a semi solution by accident, although I don't use it, and it was when I was using freeimage with bitmaps, freeimage passed an IntPtr back which contained it's bits, and I found I could use that in one of the the Bitmap ctors, and I could change the contents within the IntPtr and it would reflect in the bitmap without having to do a LockBits.

So you might have to something similar.

byte[] buffer = new byte[w*h*d];
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

then pass handle.AddrOfPinnedObject(); to the BitMap ctor.

I'm sure there's a better solution, but this is one I've actually come across by accident, and to be honest, I'm not even sure how much of an overhead lockbits even has, it maybe hardly anything, it depends if if all the lockbits actually does it mark the memory it's at as fixed.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!