Skip to content

Instantly share code, notes, and snippets.

@dejanstojanovic
Created February 18, 2015 15:06
Show Gist options
  • Save dejanstojanovic/c5df7310174b570c16bc to your computer and use it in GitHub Desktop.
Save dejanstojanovic/c5df7310174b570c16bc to your computer and use it in GitHub Desktop.
Fast and short way to get image size in C#
using(var img = Image.FromFile(file.Name))
{
var height = img.Height;
var width = img.Width;
}
@giammin
Copy link

giammin commented Apr 30, 2019

this is about 250 time faster:

 using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    using (var image = Image.FromStream(fileStream, false, false))
    {       
         var height = image.Height;
         var width = image.Width;
    }
}

@willianwrm
Copy link

I had my doubts but that is really faster, it'll require less data (reads) from stream/file.

The big difference is from both parameters useEmbeddedColorManagement and validateImageData set to false.
Tested with a 195001 bytes JPEG, the original load Image.FromStream(fileStream) did read all the bytes from the stream and the Image.FromStream(fileStream, false, false) version did read only 4268 bytes (about 2,1%).

@MikeJPelton
Copy link

Huge performance improvement over the first option - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment