C # obtain the image size and resolution [13:39:19] font size: large, medium, and small
Source: CSDN blog
// 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 of 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; // Add 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; // Add 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 unit of X and Y, units = 0: No unit, units = 1: Points/inch, units = 2: Points/centimeter
// 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; // Add segment length
Break;
}
If (ps + 1> = F_Stream.Length) // end of 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;
}
I 've been playing and Weibo recently. It's very convenient and practical. Come and join me!
Look at my Weibo! Http://t.hexun.com/4194019/default.html