. Net to get the image size code

Source: Internet
Author: User

NET. utils. registerImageByImageSource (string name, ImageSource imageSource, double width, double height). You should blame Microsoft for this problem, why does the Image have to be bound to the source and displayed on Silverlight to obtain the width and height information? When the component is not displayed, you can only get 0 for the width and height of the image.

. NET. utils. registerImageByImageSource (string name, ImageSource imageSource, double width, double height). You should blame Microsoft for this problem, why does the Image have to be bound to the source and displayed on Silverlight to obtain the width and height information? When the component is not displayed, you can only get 0 for the width and height of the image.

Internal static void RegisterPNGImage (string name, Uri uri)
{
StreamResourceInfo resourceInfo = Application. GetResourceStream (uri );
If (resourceInfo! = Null & resourceInfo. Stream! = Null)
{
BitmapImage image = new BitmapImage ();
# If SILVERLIGHT
Image. SetSource (resourceInfo. Stream );
# Else
Image. BeginInit ();
Image. StreamSource = resourceInfo. Stream;
Image. EndInit ();
# Endif
ResourceInfo. Stream. Position = 0;
Byte [] header = new byte [8];
ResourceInfo. Stream. Read (header, 0, header. Length );
If (header [0] = 0x89 &&
Header [1] = 0x50 & // P
Header [2] = 0x4E & // N
Header [3] = 0x47 & // G
Header [4] = 0x0D & // CR
Header [5] = 0x0A & // LF
Header [6] = 0x1A & // EOF
Header [7] = 0x0A) // LF
{
Byte [] buffer = new byte [16];
ResourceInfo. Stream. Read (buffer, 0, buffer. Length );
Array. Reverse (buffer, 8, 4 );
Array. Reverse (buffer, 12, 4 );

Double width = BitConverter. ToInt32 (buffer, 8 );
Double height = BitConverter. ToInt32 (buffer, 12 );

ResourceInfo. Stream. Close ();
ImageUtils. RegisterImageAsset (name, new ImageAsset (image, width, height ));

Return;
}
ResourceInfo. Stream. Close ();
}
New Exception ("Can not load PNG image'" + name + "'uri:" + URI );
}

I am here to serve as TWaver for Microsoft last year. NET has already started to pre-state and use the customer's apologies in the project in beta. We have not completely done DRY, but the TWaver Development Team, which has always adhered to the principle of minimizing the workload of users, has officially started TWaver. NET 1.0 provides a more comfortable RegisterPNGImage (string name, Uri uri) function when it is released. Yes, you don't have to tell TWaver width or height. TWaver will automatically get it, as for how to get a lot of people are very interested, I will not explain the following implementation of the Code principle is very simple, in short, all the way to Rome, Silverlight does not provide us can go out of their own way

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.