What type should I use for saving images using C#?
Question
I have this class called Hero that has the following attributes. String is for letters, int for numbers, etc. What can I use if I'm going to be saving an image there?
public class Hero
{
[Hero class's attributes (non-image related]
public byte[] Portrait { get; set; }
public byte[] Screenshot { get; set; }
}
Portrait and Screenshot are going to be .png or .jpg files. Should I use a byte[] array for them? I'm a bit confused.
LOL, nevermind. I figured out my error. I had to use System.WINDOWS.Media.Imaging. Go figure.
Solution
I'm going to be using them for just showing them on a WPF Form.
BitmapImage is good for XAML based apps, or another derived class of BitmapSource may be more appropriate for your needs, otherwise just a plain old Bitmap.
OTHER TIPS
There is an actual Bitmap class. I would strongly suggest against using a Byte array for it as that it could become corrupted and you would invalidate the image. If you kept it in a Bitmap object, you can directly pass the object to the PictureBox control and render it on the screen.
Additonally, unlike the BMP format PNG [assuming] and JPG do not store the pixel format in the same fashion or representation. JPG does model fitting [within blocks] (where it gets some of its savings and loses information) and stores the representation of the model instead of direct color values.
System.Drawing.Bitmap
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx
How are you going to be using them? The Image class might be more appropriate. Bitmap might be another good one.
EDIT:
Then you'll want to use a BitmapImage. See also SO 94456 for some usage.
It depends:
- If you need to manipulate that image (rotate, scale etc), then use Bitmap (or its base class - Image).
- If you store it just to pass the image to client (UI) then:
- For web-application use array of bytes so it can be directly rendered to the response stream.
- For the WPF application use the one that is easy to bind to (which can be BitmapImage, see related question).