Windows Phone Resolution Image Format

Source: Internet
Author: User
Tags bmp image

In Windows Phone, the Image control is used to display images. However, the Image control currently only supports two formats: Png and Jpg. We usually use two types of images: Gif and Bmp, which cannot be displayed through Image. You need to use third-party controls to show that ImageTools is provided by the open-source community CodePlex. You can download dllat http://imagetools.codeplex.com/to download the source code and use imagetoolsto display GIF and BMP images.
Therefore, for commonly used image formats such as Png, Jpg, Gif, and Bmp, we need to use different controls to display different image formats. Here we have a question about how to parse the image format. We cannot identify the image format by using the file suffix .png).jpg0000.0000.gifini.bmp, because we can directly modify the image suffix name, the suffix name cannot be changed, and the image remains in its original format.
In the format result of an image file, the header information (usually in the first few bytes of the image file) contains the image format information. The following describes the head information identifier (hexadecimal) of the images commonly used by trains in these formats ).
1. The Png image file contains 8 Bytes: 89 50 4E 47 0D 0A 1A 0A. That is,. PNG .....
2. The jpg image file contains 2 bytes: FF D8.
3. The GIF image file contains 6 bytes: 47 49 46 38 39 | 37 61. GIF89 (7).
4. The Bmp image file contains 2 bytes: 42 4D. That is, BM.
Based on the image question header identification information, we can easily determine the file format. First, we need to obtain the byte stream information of the image file. The Code is as follows.

View Code

// Obtain the image file stream, which is processed separately according to whether the image is a resource file or an independent storage file
Stream stream = null;
// For resource file processing
StreamResourceInfo info = Application. GetResourceStream (new Uri (path, UriKind. Relative ));
If (info! = Null)
{
Stream = info. Stream;
}
// If it is an independent storage file processing
Using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile. GetUserStoreForApplication ())
{
// Open the file
Stream = myIsolatedStorage. OpenFile (path, FileMode. Open, FileAccess. Read );
}

Read 8 bytes from the stream of the image file stream, and then compare the file header matching based on different image formats to determine the file format. The Code is as follows.

View Code

/// <Summary>
/// Define the image format
/// </Summary>
Public enum ImageType
{
Null,
Png,
Jpg,
Gif,
Bmp
}

/// <Summary>
/// Obtain the image format
/// </Summary>
Private ImageType getImageType (Stream stream)
{
// Image format
ImageType type = ImageType. Null;

// Read the first eight bytes of the image file, and determine the image format based on several bytes.
Byte [] header = new byte [8];
Stream. Read (header, 0, 8 );

// Determine the image format
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)
{
// Png Image 8 Bytes: 89 50 4E 47 0D 0A 1A 0A
Type = ImageType. Png;
}
Else if (header [0] = 0xFF &&
Header [1] = 0xD8)
{
// Jpg image 2 bytes: FF D8
Type = ImageType. Jpg;
}
Else if (header [0] = 0x47 & // G
Header [1] = 0x49 & // I
Header [2] = 0x46 & // F
Header [3] = 0x38 & // 8
(Header [4] = 0x39 | // 9
Header [4] = 0x37) & // 7
Header [5] = 0x61) //
{
// GIF image 6 bytes: 47 49 46 38 39 | 37 61
Type = ImageType. Gif;
}
Else if (header [0] = 0x42 & // B
Header [1] = 0x4D) // M
{
// Bmp Image 2 bytes: 42 4D
Type = ImageType. Bmp;
}

// Close the byte stream
Stream. Close ();

Return type;
}

After the image format is parsed, we can select the corresponding control based on the image format to display the image.

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.