views:

110

answers:

5

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.

+1  A: 

System.Drawing.Bitmap

http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

Rob Windsor
+2  A: 

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.

monksy
I can't find that class. I think it's not available on .NET 4.0 or on WPF. Any help?
Sergio Tapia
Its been in the framework since 2.0, I think it may have been there since 1, I can't be sure.
monksy
It is under system.drawing. http://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28VS.100%29.aspx
monksy
Add a reference to System.Drawing.dll
shf301
That fixed it shf301. Thanks a bunch.
Sergio Tapia
Wait, I can't find it anywhere. Are you sure it's on .Net 4.0.Should I just use 3.5? :S
Sergio Tapia
They are giving you answers for WinForms (or, more specifically, GDI) development. The correct answer is Si's answer.
Adam Robinson
+1  A: 

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.

Joe Doyle
I'm going to be using them for just showing them on a WPF Form.
Sergio Tapia
A: 

It depends:

  1. If you need to manipulate that image (rotate, scale etc), then use Bitmap (or its base class - Image).
  2. If you store it just to pass the image to client (UI) then:
    1. For web-application use array of bytes so it can be directly rendered to the response stream.
    2. For the WPF application use the one that is easy to bind to (which can be BitmapImage, see related question).
Dmytrii Nagirniak
+4  A: 

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.

Si
BitmapImage is in PresentationCore, under the System.Windows.Media.Imaging namespace.
Adam Robinson