how to use the image magic "identify" command from Magic.Net library

27 Views Asked by At

I need to validate if the HTTP-request response is an image, I would like to do it using Magick.NET rather than relying on the content-type of the response as I will resize the image and need to be sure that the input is processable by the library.

I saw some posts that suggest reading the image, but I know that there is a specialized command "identify" that will do the trick with lower impact on the performance, unfortunately, I haven't found a way to use it from Magic.NET.

Is there a way to use the identify command from C# or should I read the stream using new MagickImage(inputStream).Format?

1

There are 1 best solutions below

0
lorjuelag On

I found that the identify command can be used through the MagickImageInfo MagickImageInfo class that under the hood uses the MagickImage.Ping

 private static void ValidateContent(Stream imageStream)
 {
     try
     {
         if (imageStream.CanSeek)
             imageStream.Seek(0, SeekOrigin.Begin);
         MagickImageInfo info = new MagickImageInfo(imageStream);
         Console.WriteLine($"stream content format {info.Format}");
         if (imageStream.CanSeek)
             imageStream.Seek(0, SeekOrigin.Begin);
     }
     catch (MagickErrorException)
     {
         {...}
     }
 }