1

I'm working on a class that will help me read a game file, and part of the file is an image. Is there an image object that I can create from a byte array, or should I just store the image as a byte array? If I were to put that image into a picture displayer in winforms, can I do that with a byte array?

What's the best way to store the data from the file?

mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • What have you tried? Did you read the documentation on the `System.Drawing` namespace? What did you have problems with? – Oded Dec 23 '11 at 10:15

2 Answers2

3

like this:

byte[] data = getYourImageData();
MemoryStream ms = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(ms);

to answer the other part of your question, it is fine to store it as a byte array - maybe provide a helper method that returns a memory stream as seen above, or alternatively store it in a System.Drawing.Bitmap and return that:

return new Bitmap(ms);
Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63
1

The Bitmap class in System.Drawing supports a constructor that takes a stream as a parameter. This stream can be supplied by a MemoryStream that is created from a byte array.

Once you have the bitmap, a PictureBox can be used to display it.

References:

http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx (for the bitmap)

http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx (for the stream)

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx (PictureBox)

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95