File Properties as in Windows

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

Hello,

Is there any way in C# or maybe a Third Party Library to get the file
properties?

If, in windows, I check the properties of an image I can see the size,
dimensions, type, etc.
And the same for other file types ...

Thank You,
Miguel
 
Is there any way in C# or maybe a Third Party Library to get the file
properties?

If, in windows, I check the properties of an image I can see the size,
dimensions, type, etc.
And the same for other file types ...

Windows Explorer has a whole bunch of handlers which it calls to get extra
information out of certain types of files. You'd have to call those handlers
yourself. I would assume they're COM.
 
Is there any way in C# or maybe a Third Party Library to get the file
properties?

If, in windows, I check the properties of an image I can see the size,
dimensions, type, etc.
And the same for other file types ...

Getting dimensions etc. of an image requires image format specific
code.

The code can located various places:
* some .NET code within the framework
* some third party .NET code
* some native code
* some C# code you write yourself

As an example of the lat option I am attaching some code
I wrote for TIFF many years ago.

Arne

====

using System;
using System.IO;

namespace E
{
public class TiffDim
{
public struct HWDim
{
public int H;
public int W;
}
private static short ReadWord(Stream stm, int ix)
{
stm.Seek(ix, SeekOrigin.Begin);
byte[] word = new byte[2];
stm.Read(word, 0, 2);
return BitConverter.ToInt16(word, 0);
}
private static int ReadDWord(Stream stm, int ix)
{
stm.Seek(ix, SeekOrigin.Begin);
byte[] dword = new byte[4];
stm.Read(dword, 0, 4);
return BitConverter.ToInt32(dword, 0);
}
public static HWDim Find(string fnm)
{
HWDim res = new HWDim();
using(Stream stm = new FileStream(fnm, FileMode.Open,
FileAccess.Read))
{
int ix = ReadWord(stm, 4);
for(;;)
{
int ntag = ReadWord(stm, ix);
ix += 2;
for(int i = 0; i < ntag; i++)
{
int tagid = ReadWord(stm, ix);
if(tagid == 257)
{
res.H = ReadDWord(stm, ix + 8);
}
else if(tagid == 256)
{
res.W = ReadDWord(stm, ix + 8);
}
ix += 12;
}
ix = ReadDWord(stm, ix);
if(ix == 0) break;
}
}
return res;
}
public static void Main(string[] args)
{
HWDim hw = TiffDim.Find(@"C:\test.tif");
Console.WriteLine(hw.H + " " + hw.W);
}
}
}
 
And the same for other file types ...

Getting dimensions etc. of an image requires image format specific
code.

The code can located various places:
* some .NET code within the framework
* some third party .NET code
* some native code
* some C# code you write yourself

As an example of the lat option I am attaching some code
I wrote for TIFF many years ago. [...]

Note that if it is acceptable to read the entire image into memory (this
is both slower and consumes more memory than reading the file format
directly), and it is also acceptable to be limited to the image formats
that .NET can handle (which includes TIFF, by the way), then from a
coding standpoint, the simplest thing is to just use one of the built-in
image object types and have .NET do the hard work.

If .NET supports the format then it is obviously the easiest approach.

Arne
 
Back
Top