How to get the image size in. NET

Source: Internet
Author: User

With the recent TWaver. NET official release, many of the Java that has used TWaver, Web or Flex old customers can also try to use TWaver in the project. NET, although TWaver. NET has TWaver. wpf. dll has TWaver again. silverlight. the two dll Development Kits support both the WPF and Silverlight methods. You can select them based on your needs. However, according to our recent visit to TWaver from all over the country, we currently use TWaver. according to statistics from only 20 customers of NET, the majority of solutions using Silverlight are still available. Although the WPF feature is more powerful, after all, Silverlight can run on windows through a lightweight 6 m multi-plug-in, mac, or even the mobile Domain of symbian and windows phone 7, is so lightweight to release, so easy to update, and so widely deployed. Why not.

As we get back to today's theme, many old customers who have used other TWaver versions often complain that other TWaver versions are always so easy to understand, let's try to see why TWaver can also achieve Don't Repeat Yourself (DRY) in the development gui. 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.

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

View sourceprint? 01 internal static void RegisterPNGImage (string name, Uri uri)

02 {

03 StreamResourceInfo resourceInfo = Application. GetResourceStream (uri );

04 if (resourceInfo! = Null & resourceInfo. Stream! = Null)

05 {

06 BitmapImage image = new BitmapImage ();

07 # if SILVERLIGHT

08 image. SetSource (resourceInfo. Stream );

09 # else

10 image. BeginInit ();

11 image. StreamSource = resourceInfo. Stream;

12 image. EndInit ();

13 # endif

14 resourceInfo. Stream. Position = 0;

15 byte [] header = new byte [8];

16 resourceInfo. Stream. Read (header, 0, header. Length );

17 if (header [0] = 0x89 &&

18 header [1] = 0x50 & // P

19 header [2] = 0x4E & // N

20 header [3] = 0x47 & // G

21 header [4] = 0x0D & // CR

22 header [5] = 0x0A & // LF

23 header [6] = 0x1A & // EOF

24 header [7] = 0x0A) // LF

25 {

26 byte [] buffer = new byte [16];

27 resourceInfo. Stream. Read (buffer, 0, buffer. Length );

28 Array. Reverse (buffer, 8, 4 );

29 Array. Reverse (buffer, 12, 4 );

30

31 double width = BitConverter. ToInt32 (buffer, 8 );

32 double height = BitConverter. ToInt32 (buffer, 12 );

33

34 resourceInfo. Stream. Close ();

35 ImageUtils. RegisterImageAsset (name, new ImageAsset (image, width, height ));

36

37 return;

38}

39 resourceInfo. Stream. Close ();

40}

41 new Exception ("Can not load PNG image'" + name + "'uri:" + URI );

42}

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.