How do I reliably get an image dimensions in .NET without loading the image?
Asked Answered
M

7

29

I know how to get the size (x,y) of an image

Image.FromFile("cat.jpg").Size

BUT that requires loading the image from memory.

When I view the images in Windows Explorer it shows me the size.

  • How do I access that size?
  • Is it reliable for all images? Does Windows explorer need to have 'seen' the image first for that size to be available. I don't want to upload my images to a server and have no metadata available if I never opened the folder.
Murrhine answered 16/2, 2009 at 7:15 Comment(0)
M
41

You can just use Image.FromStream(Stream, bool, bool) with "false" 2nd and 3rd parameter to avoid loading file into memory.

Monmouthshire answered 16/2, 2009 at 8:44 Comment(4)
This is exactly what I needed; no byte management, etc. Thanks!Kulda
eg. Image.FromStream(File.OpenRead(file.PhysicalPath), false, false)Murrhine
How does this not read the whole file???Rudelson
I can confirm that this doesn't read the compile file. In my case it only reads the first 4 KB of a 128 KB file. I checked this by wrapping the supplied stream in another stream that outputs calls to the Stream.Read method. You can find my stream decorator here.Nadbus
C
11

If you want to get the image dimensions (width and hight) with out loading the image, you need to read some part of jpeg file yourself as below.

Here is an out of the box method for you :)

public static Size GetJpegImageSize(string filename) {
    FileStream stream=null;
    BinaryReader rdr=null;
    try {
        stream=File.OpenRead(filename);
        rdr=new BinaryReader(stream);
        // keep reading packets until we find one that contains Size info
        for(; ; ) {
            byte code=rdr.ReadByte();
            if(code!=0xFF) throw new ApplicationException(
                    "Unexpected value in file "+filename);
            code=rdr.ReadByte();
            switch(code) {
                // filler byte
                case 0xFF:
                    stream.Position--;
                    break;
                // packets without data
                case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: 
                case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:
                    break;
                // packets with size information
                case 0xC0: case 0xC1: case 0xC2: case 0xC3:
                case 0xC4: case 0xC5: case 0xC6: case 0xC7:
                case 0xC8: case 0xC9: case 0xCA: case 0xCB:
                case 0xCC: case 0xCD: case 0xCE: case 0xCF:
                    ReadBEUshort(rdr);
                    rdr.ReadByte();
                    ushort h=ReadBEUshort(rdr);
                    ushort w=ReadBEUshort(rdr);
                    return new Size(w, h);
                // irrelevant variable-length packets
                default:
                    int len=ReadBEUshort(rdr);
                    stream.Position+=len-2;
                    break;
            }
        }
    } finally {
        if(rdr!=null) rdr.Close();
        if(stream!=null) stream.Close();
    }
}

private static ushort ReadBEUshort(BinaryReader rdr) {
    ushort hi=rdr.ReadByte();
    hi<<=8;
    ushort lo=rdr.ReadByte();
    return (ushort)(hi|lo);
}

This is not my code, got this example some time back in code project. I copied and and saved it to my utility snippets, don't remember the link though.

Chlortetracycline answered 16/2, 2009 at 7:38 Comment(2)
ugh! lol. whoa thats a lot of code. plus i'm in a web environment so I may have gifs and pngs too. i'm leaning towards caching Image.FromFile("cat.jpg").Size - or at least timing how long it takes to read in an imageMurrhine
Yep - it opens the jpg to read the header with size informationChlortetracycline
C
4

The article Reading Image Headers to Get Width and Height provides several solution possibilities:

  • Multi-threading
  • Reading file headers, reverting to full image load if header is lacking
  • Creating your own index for image attributes
Cockoftherock answered 15/10, 2010 at 15:21 Comment(0)
E
3

Doing this usually involves actually opening the file and reading only its header (the first few bytes) to find its format and the size. You don't need to read the whole file to find out it's size. Sometimes the size is even in a specific offset in the file so finding the size is only reading 8 bytes.

You can use filemon.exe to actually find out what windows explorer itself does to find the size.

Erective answered 16/2, 2009 at 7:29 Comment(4)
Does not Windows explore go right to the OS as ask how much space the image has allocated on the disk?. Is it not a file attribute? Or as you say is it encoded meta data?Purebred
@panix - sorry if i confused you. i mean size as in width+height and not size as in bytes.Murrhine
@shy do you know if thats what explorer does? can i rely on the metadata that windows explorer has? i just think this is a little sneaky and possibly unreliable in some wierd edge cases - which is why i posted the questionMurrhine
@Simon: yes, header data can lie to you, but no, there are no methods other than reading the header or reading the complete file.Autacoid
I
1

From the top of my head - LoadImage() has an override that specifies progress callback. Maybe you can use that to cancel loading after header was read?

Invocation answered 15/10, 2010 at 15:33 Comment(0)
C
0

Magick.NET can do this and supports many file formats:

var info = new ImageMagick.MagickImageInfo(filename);

Console.WriteLine($"{info.Width} x {info.Height}");

More here

Clingstone answered 26/3 at 22:12 Comment(0)
D
0

Using ImageSharp:

var imageInfo = await Image.IdentifyAsync(imageStream);
Console.WriteLine($"{imageInfo.Width} x {imageInfo.Height}");
Dybbuk answered 5/6 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.