Read a tiff file's dimension and resolution without loading it first

z1x*_*1x2 8 c# memory tiff

How to read a tiff file's dimension (width and height) and resolution (horizontal and vertical) without first loading it into memory by using code like the following. It is too slow for big files and I don't need to manipulate them.

Image tif = Image.FromFile(@"C:\large_size.tif");
float width = tif.PhysicalDimension.Width;
float height = tif.PhysicalDimension.Height;
float hresolution = tif.HorizontalResolution;
float vresolution = tif.VerticalResolution;
tif.Dispose();
Run Code Online (Sandbox Code Playgroud)

Edit:

Those tiff files are Bilevel and have a dimension of 30x42 inch. The file sizes are about 1~2 MB. So the method above works Ok but slow.

Joe*_*eau 11

自己进入这个并找到解决方案(可能在这里).Image.FromStreamwith validateImageData = false允许您访问您正在查找的信息,而无需加载整个文件.

using(FileStream stream = new FileStream(@"C:\large_size.tif", FileMode.Open, FileAccess.Read))
{
  using(Image tif = Image.FromStream(stream, false, false))
  {
    float width = tif.PhysicalDimension.Width;
    float height = tif.PhysicalDimension.Height;
    float hresolution = tif.HorizontalResolution;
    float vresolution = tif.VerticalResolution;
  }
}
Run Code Online (Sandbox Code Playgroud)