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?
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With