-1

How to get the file extension or content type from file byte array. in C#

we have a problem in our production environment that, after last production upgrade PNG files uploaded previously were saved as JPG file extension. so when i read that file i cannot set mimetype as PNG, since currently it takes from path.getextension, but this is wrong- my actual file is PNG type. this is causing issues in rendering images.

How to get the file extension or content type from file byte array. in C#

pvaju896
  • 1,397
  • 6
  • 25
  • 46

1 Answers1

0

You may use Image.RawFormat.

if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
    // it is JPEG
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
    // it is PNG
}
else if (ImageFormat.Gif.Equals(img.RawFormat))
{
    // it is GIF
}
Peyman Mehrabani
  • 619
  • 6
  • 17