I want to check if the file is an actual .png/.jpg file that can be displayed (for example not being a .txt file with a changed file extension to .png). I also want to check if the width and height of the image is in a valid range. How can I achieve this?
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<IActionResult> Create(IFormFile image)
{
// validate image
}
2
Answers
Check the file for a known header. (Info from link also mentioned in this answer)
The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10
You can also check Path.GetExtension Method
Here’s a simple sample:
First of all, don’t try to use
System.Drawing
in .NET Core applications. It’s deprecated and works only on Windows anyway. The MSDN docs themselves suggest using ImageSharp or SkiaSharp instead.Image files start with bytes that identify the file format. You’ll have to read at least some of the file’s contents to read image metadata like the image size, resolution etc. You can use ImageSharp’s Identify method to read only the format and image properties, without loading the entire image.
You can read an uploaded file’s contents using IFormFile.OpenReadStream.
The
format
parameter is an IImageFormat value that contains information about the image format, including its name and mime types.The IImageInfo object returned contains the image dimensions, pixel type, resolution etc.
The method documentation explains that the return value will be
null
if no suitable decoder is found:But an exception will be thrown if the content is invalid:
Without testing this, I assume that a text file will result in a
null
but a file with just a GIF header without valid content will result in an exception.You can use ImageSharp to resize the image or convert it to another format. In that case it’s not enough to just load the metadata. You can use Load to load the image from the stream, detect its format and then manipulate it.