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;
}
@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