Jump to content
  • Advertisement
Sign in to follow this  

Power of 2 issue with LockBits?

This topic is 3033 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

Got an odd issue here. The actual scenario is a bit more involved (extracting image data from a texture pack I don't compile myself). The texture is 'packed' by storing only the width, height, and raw, 8bpp pixel data of a graphic.

I need to extract that data and rebuild the image. It works fine for power-of-two images but not odd ones (like 25x25). Is this a limitation of GDI+?

Edit: It seems that .Stride has something to do with it. The Stride is 28 and if I look at the raw bytes from the source, I do get 0's padding at 25, 26, and 27. Question now is, is there any easy way to save the bitdata as is (25x25, in an array 625 in length) or am I going to have to twiddle bytes manually, padding the byte array according to bmd.Stride?

Dim src As Bitmap = Image.FromFile("source.gif")
Dim Bytes(srv.Width * src.Height - 1) As Byte ' source dimensions are 25x25

Dim r As Rectangle = Rectangle.Truncate(src.GetBounds(GraphicsUnit.Pixel)) ' r shows 25x25

Dim bmd As New Imaging.BitmapData = src.LockBits(r, Imaging.ImageLockMode.ReadOnly, src.PixelFormat)
Marshal.Copy(bmd.Scan0, Bytes, 0, Bytes.Length)

Dim dst As New Bitmap(src.Width, src.Height, Imaging.PixelFormat.Format8bppIndexed) ' Saving as gif
r = Rectangle.Truncate(dst.GetBounds(GraphicsUnit.Pixel))

dst.Palette = src.Palette
bmd = dst.LockBits(r, Imaging.ImageLockMode.WriteOnly, dst.PixelFormat)
Marshal.Copy(Bytes, 0, bmd.Scan0, Bytes.Length)

dst.Save("destination.gif", Imaging.ImageFormat.Gif)


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!