views:

46

answers:

4

I'm writing a method that needs to save a System.Drawing.Image to a file. Without knowing the original file the Image was created from, is there anyway to determine what file extension it should have?

The best solution I've come up with is to use a Switch/Case statement with the value of Image.RawFormat.

Does it even matter that I save the Image in it's original format? Is an Image generated from a PNG any different from say one generated from a JPEG? Or is the data stored in an Image object completely generic?

A: 

Yes, it definitely matters because different fileformats support different features such as compression, multiple frames, etc.

I've always used a switch statement like you have, perhaps baked into an extension method or something.

Steve Danner
So the Image class takes this into account then? If I load a jpeg, which is compressed, into an Image object that object retains the JPEG compression? It doesn't expand the image into a generic bitmap encoding?
Eric Anastas
I believe your take on this is correct, Eric. My understanding is that once it's in the Image class, it's format-neutral and can be subsequently saved into any other format (using ImageFormat).
Kirk Woll
While this may be true to a point, you always need to take into account what was originally loaded. Take the multiple frame example. If you loaded a multipage tiff into an image object and saved as any other image format, you would lose all frames (pages) except the active one.
Steve Danner
+1  A: 

Image.RawFormat has cooties, stay away from it. I've seen several reports of it having no legal value for no apparent reason. Undiagnosed as yet.

You are quite right, it doesn't matter what format you save it to. After you loaded the file, the internal format is the same for any bitmap (not vector) with the same pixel format. Generally avoid recompressing jpeg files, they tend to get bigger and acquire more artifacts. Steve mentions multi-frame files, they need to be saved a different way.

Hans Passant
+1  A: 

While Steve Danner is correct in that an image created from a JPG will look different to an image created from a PNG once it's loaded into memory it's an uncompressed data stream.

This means that you can save it out to any file format you want.

However, if you load a JPG image and then save it as another JPG you are throwing away more information due to the compression algorithm. If you do this repeatedly you will eventually lose the image.

If you can I'd recommend always saving as PNG.

ChrisF
Ahh ok, thanks for the tip. That's really what I've been trying to figure out. If it's ok to take an Image, that originated from any format, and save it to one specific format. So it sounds like this is ok if I use an uncompressed format like PNG?
Eric Anastas
@Eric - PNG is compressed - just not lossy. So if you open image A.png save it as B.png then open B.png and save it as C.png etc. there'll be no loss in quality.
ChrisF
Ahh yeah well lossy is what I meant. Thanks for clarifying
Eric Anastas
A: 

To answer your question 'Does it even matter that I save the Image in it's original format?' explicitly: Yes, it does, but in a negative way.

When you load the image, it is uncompressed internally to a bitmap (or as ChrisF calls it, an uncompressed data stream). So if the original image used a lossy compression (for example jpeg), saving it in the same format will again result in loss of information (i.e. more artifacts, less detail, lower quality). Especially if you have repeated actions of read - modify - save, this is something to avoid.

(Note that it is also something to avoid if you are not modifying the picture. Just the repeated decompress - compress cycles will degrade the image quality).

So if disk space is not an issue here (and it usually isn't in the age of hard disks that are big enough for HD video), always store any intermediate pictures in lossless compression formats, or uncompressed. You may consider saving the finall output in a compressed format, depending on what you use it for. (If you want to present those final pictures on the web, jpeg or png would be good choices).

Treb