// C # Get jpg image size and inch resolution quickly
Public static int getJpgSize (string
FileName, out Size JpgSize, out float Wpx, out float Hpx)
{// C # Get jpg image size and inch resolution quickly
JpgSize = new Size (0, 0 );
Wpx = 0; Hpx = 0;
Int rx = 0;
If
(! File. Exists (FileName) return rx;
FileStream F_Stream =
File. OpenRead (FileName );
Int ff =
F_Stream.ReadByte ();
Int type =
F_Stream.ReadByte ();
If (ff! = 0xff | type! =
0xd8)
{// Non-JPG file
F_Stream.Close ();
Return rx;
}
Long ps = 0;
Do
{
Do
{
Ff =
F_Stream.ReadByte ();
If (ff <0)
// End the file
{
F_Stream.Close ();
Return rx;
}
} While (ff! = 0xff );
Do
{
Type =
F_Stream.ReadByte ();
} While (type = 0xff );
// MessageBox. Show (ff. ToString () + "," + type. ToString (),
F_Stream.Position.ToString ());
Ps =
F_Stream.Position;
Switch (type)
{
Case 0x00:
Case
0x01:
Case 0xD0:
Case
0xD1:
Case 0xD2:
Case
0xD3:
Case 0xD4:
Case
0xD5:
Case 0xD6:
Case
0xD7:
Break;
Case 0xc0:
// SOF0
Ps = F_Stream.ReadByte ()*
256;
Ps = F_Stream.Position + ps +
F_Stream.ReadByte ()-2; // segment length
F_Stream.ReadByte ();
// Discard precision data
// Height
JpgSize. Height = F_Stream.ReadByte () * 256;
JpgSize. Height = JpgSize. Height +
F_Stream.ReadByte ();
// Width
JpgSize. Width = F_Stream.ReadByte () * 256;
JpgSize. Width = JpgSize. Width + F_Stream.ReadByte ();
// Ignore the following information
If (rx! = 1 & rx <3) rx = rx +
1;
Break;
Case 0xe0:
// APP0 segment
Ps = F_Stream.ReadByte ()*
256;
Ps = F_Stream.Position + ps +
F_Stream.ReadByte ()-2; // segment length
F_Stream.Seek (5, SeekOrigin. Current );
// Discard the APP0 tag (5 bytes)
F_Stream.Seek (2, SeekOrigin. Current );
// Discard the primary version (1 bytes) and secondary version (1 bytes)
Int
Units = F_Stream.ReadByte ();
// Density units of X and Y, units = 0: No unit, units = 1: Points/inch, units = 2: Points/cm
// Horizontal (pixel/inch) Resolution
Wpx = F_Stream.ReadByte () * 256;
Wpx = Wpx + F_Stream.ReadByte ();
If (units = 2) Wpx =
(Float) (Wpx * 2.54); // The centimeter is changed to an inch.
// Vertical (pixel/inch) Resolution
Hpx = F_Stream.ReadByte ()*
256;
Hpx = Hpx +
F_Stream.ReadByte ();
If (units = 2) Hpx =
(Float) (Hpx * 2.54); // The centimeter is changed to an inch.
// Ignore the following information
If (rx! = 2 & rx <3) rx = rx +
2;
Break;
Default:
// Skip all other segments ////////////////
Ps = F_Stream.ReadByte ()*
256;
Ps = F_Stream.Position + ps +
F_Stream.ReadByte ()-2; // segment length
Break;
}
If (ps + 1> = F_Stream.Length)
// End the file
{
F_Stream.Close ();
Return rx;
}
F_Stream.Position = ps; // move the pointer
} While (type
! = 0xda); // starts scanning rows
F_Stream.Close ();
Return
Rx;
}