0

I imported a PNG file into Visual Studio 2013. A MIME mail library we're using adds visuals to HTML mail with a function that expects a byte array parameter. How to get the object returned by ResourceManager into a byte array?

ResourceManager rm;
rm = new ResourceManager("Foo.Properties.Resources", typeof(MYFORM).Assembly);
var obj = rm.GetObject("Logo");

When I try to use the .GetStream method, error says the object is not a stream, and to use .GetObject instead.

Tim
  • 8,669
  • 31
  • 105
  • 183
  • 1
    The PNG resource gets embedded as an `Image` object instead. If you want to whack that to a byte[] then you have to use Image.Save() to a MemoryStream. Or rename the file before you add it as a resource so VS doesn't know that it is an image. – Hans Passant Dec 17 '15 at 13:34

1 Answers1

5

The GetObject will return a System.Drawing.Image object if the file is an Image

Image img = (Image)rm.GetObject("Logo")

With the Image object you can directly save it to any System.IO.Stream object

MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Png);

Now you can make a copy of the bytes with the Stream.ToArray

byte[] bytes = stream.ToArray();

Or save it directly to a file

img.Save(Application.StartupPath + "/testImage.jpg")

Dont forget to close any used stream

Stream.Close();
FDH
  • 66
  • 4