sitecoresitecore7.2

Sitecore fast way to check the mediaItem in the Media Library is an Image or a picture


For the item in Sitecore 'Media Library':

MediaItem mediaItem = Sitecore.Context.Database.GetItem("{E47591D0-48D2-4543-80E4-3836B02AA1A8}");  

The above item is a picture "Tulips.jpg".

How can I check, from code behind, that the above item is actually a Picture/Image?


Solution

  • Once you have your MediaItem object, you can do this:

    MediaItem mediaItem = ...
    
    bool isPicture = mediaItem.MimeType.StartsWith("image/");
    

    This works because MIME types of images are formed like this: image/.... For example: