C # obtain the image size and resolution

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.