Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the mime-type of an Image class instance in memory in c#?

In a library I am writing for some infrastructure projects at work, I have a method to create various scales of an image (for thumbnails etc...). However, the system that I am storing this data in is requiring a mime-type declared in the database for various reasons.

Is there a way to get a Mime type from the passed in c# Image class, or will I have to have external applications pass in the Mimetype along with the image?

like image 359
KallDrexx Avatar asked Nov 27 '12 19:11

KallDrexx


People also ask

How do I find MIME type?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

How is MIME type stored?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.


1 Answers

You can get the ImageFormat from the Image, and you can get the MIME type from the ImageCodecInfo. All you need to do is tie the two together:

ImageFormat format = yourImage.RawFormat;
ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == format.Guid);
string mimeType = codec.MimeType;
like image 173
Richard Deeming Avatar answered Sep 30 '22 15:09

Richard Deeming