Png method for parsing image size in Windows Phone

Source: Internet
Author: User

We have previously introduced the image parsing format for Windows Phone. By parsing the image format, we can use related controls to display the image format. In Windows Phone development, whether it is the system Image control or the Image control provided by imageils, loading images is asynchronous, so that we cannot immediately obtain the size of the Image, you must wait until the image is loaded. In the development process, sometimes we need to obtain the image size before loading the image. Similar to the processing method used to parse the image format, we need to parse the header information of the image file and obtain the width and height of the image.
To parse the width and height of a Png image, you must first understand the data block structure of the Png image. The size information of the Png image is stored in the file header data block, therefore, we need to understand the data block structure of the file header.
Header chunk: it contains the basic information of the image data stored in the PNG file and needs to appear as the first data block in the PNG data stream, in addition, a PNG data stream can only contain one file header data block. The data block of the file header consists of 13 bytes. The first eight bytes indicate the width and height of the image, each of which occupies 4 bytes.
The file header data block is the first data block, but before the data block, that is, the initial position of the PNG image file, the first storage is the PNG file signature domain, which occupies 8 bytes, namely: 89 50 4e 47 0d 0a 1a 0a (. PNG ....), this can be used to determine whether the image is in PNG format. Next, the length and ID of the data block in the file header are 4 bytes each, and the length of the data block in the file header is 13, therefore, the length of the data block in the file header is fixed to 00 00 0D, and the ID of the data block in the file header is 49 48 44 52, that is, "IHDR ".
Through the above analysis, we can take a few steps to parse the width and height of the PNG image.
1. Read the 8 bytes of the start position, that is, the signature field of the PNG file, and determine whether the image is in PNG format. If not, exit.

// Read the first 8 bytes of the image file, and determine whether it is a PNG image based on the eight bytes.

The code is as follows: Copy code

Byte [] header = new byte [8];
Stream. Read (header, 0, 8 );
// Png image 8 bytes: 89 50 4E 47 0D 0A 1A 0A
If (! (Header [0] = 0x89 &&
Header [1] = 0x50 & // P
Header [2] = 0x4E & // N
Header [3] = 0x47 & // G
Header [4] = 0x0D &&
Header [5] = 0x0A &&
Header [6] = 0x1A &&
Header [7] = 0x0A ))
        {
// Not a PNG image
Return;
        }

2. Skip the eight bytes, that is, the length of the header data block is 00 00 00 0D, and the header data block ID is 49 48 44 52 (IHDR ).

// Data Domain length 4 specifies the Data Domain length, which is fixed to 00 00 00 0D
// Data block symbol 4 49 48 44 52, which is an IHDR Ascii code
Stream. Seek (8, SeekOrigin. Current );

3. the next step is to read the width and height of the image, and read 8 bytes. Because the storage is changed according to the high and low bit, the high and low bit conversion is required, after conversion, the byte type is directly converted to the integer type through the BitConverter class, that is, the image size.

// Read width, 4 bytes in height

The code is as follows: Copy code

Byte [] buffer = new byte [8];
Stream. Read (buffer, 0, buffer. Length );

Array. Reverse (buffer, 0, 4 );
Array. Reverse (buffer, 4, 4 );

Width _ = BitConverter. ToInt32 (buffer, 0 );
Height _ = BitConverter. ToInt32 (buffer, 4 );

Related Article

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.