This project has moved and is read-only. For the latest updates, please go here.

Detect best save format PNG or JPG

May 24, 2015 at 10:28 PM
Is it possible to detect the best format to save the image as?

Example: User uploads and image, this image could be in any support format. The image is resized and then saved. How to know if its best to save the image as a PNG or a JPG?

May 25, 2015 at 8:48 AM
What are you planning to do with the image after the user has uploaded it?
May 25, 2015 at 12:15 PM
The image will be used for web viewing.

I was hoping the pure vector files and clip art could be saved as PNG, Photos as JPG.

If the user uploaded a SVG that has an embedded image then i guess JPG would be the best.

Just trying to work out if there is a way to detect the type and go from there.

Maybe counting the numbers of colors if that's possible...
May 25, 2015 at 1:20 PM
You can always get the type of the image with the Format property of a MagickImage. But you cannot detect if an SVG file has an embedded image without parsing the xml of that file and Magick.NET cannot do that for you. I think using the total number of colors to detect clip art is a good idea, you could use the TotalColors property for that. If you want to get more detailed color information you could use the Histogram method. You might also want to use HasAlpha to check if the image has an alpha channel and then save a PNG because JPG does not support transparency.