I am embedding images into my assembly using .resx files. Upon runtime, I need to save the images into standalone files, restoring the original content. How can I extract the original file stream from an System.Drawing.Bitmap instance? I know I can create a stream using Bitmap.Save(), but this transcodes (and in effect – inflates) the images, even when saving a PNG back as PNG.
Or perhaps my mistake is reading them from Resource as Bitmap in the first place?
Yes, once you have read it as a
Bitmapobject, you can’t get the original file stream back. TheBitmapobject only contains the uncompressed data, not the original data.You should read the resource as byte data intead. You can read from the resource stream and write to a file:
Note: this requires adding the image as an embedded resource, not a managed resource. You can create an
Imagefrom it usingImage.FromStream().