Share via


Bitmap::FromStream() issues

I've been writing some code the last day or so to pull a PNG bitmap out of a resource file and put it into a GDI+ bitmap. You would like to use:

Bitmap::FromResource()

but unfortunately, that only works for BITMAP resources. I found some code internally that uses Bitmap::FromStream() to create GDI+ bitmap - it's somewhat similar to this code.

That worked fine to draw an image sometimes, but didn't work other times. Specifically, I couldn't do something like:

graphics.DrawImage(&bitmap, 0, 0);

while it would work with:

graphics.DrawImage(&bitmap, 0, 0, 32, 32);

It seems that the bitmap you get back from Bitmap::FromStream() doesn't know how big it is. The first call therefore doesn't work, nor does trying to draw with transparency using ImageAttributes. If, however, you create a new bitmap:

Bitmap* pGoodBitmap = new Bitmap(width, height, PixelFormat32bppARGB);
Graphics graphics(pGoodBitmap);

Status status = graphics.DrawImage(pLoadedBitmap, 0, 0, width, height);

then that new bitmap (pGoodBitmap here) will be a fully-fledged bitmap that you can then use as you wish.

It might also work work to call Bitmap::Clone()

Comments

  • Anonymous
    October 12, 2005
    Eric, why you call this post "FromStream", and at same time you are speaking about "FromResource"?
  • Anonymous
    October 12, 2005
    Just use the following code. It works with any type of image.

    Bitmap bmp;
    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("blah.jpg")
    {
    bmp = Bitmap.FromStream(stream);
    }
  • Anonymous
    June 07, 2009
    PingBack from http://weakbladder.info/story.php?id=3815