Windows 8 Metro style development (8)-Image Compression

Source: Internet
Author: User

Currently, there is no image compression API in the Win8 SDK, which can only be processed by third-party class libraries. Unfortunately, there are no third-party class libraries dedicated to Win8 image compression. However, Win8 APIs are similar to Silverlight APIs, so you can port them directly from Silverlight.

I found a library source code for the Silverlight version of lib2.16.net. After a simple modification, I changed it to the image compression processing class library of the Win8 version. This class library can compress images by quality percentage.

BelowCode.

     Protected   Override   Async   Void  Onnavigatedto (navigationeventargs e ){  String STR = "  Http://gimg.vancl.com/img/02/8705542.jpg  "  ; VaR Storagefile = Await Windows. Storage. storagefile. createstreamedfilefromuriasync ( "  Temp.jpg  " , New Uri (STR, urikind. Absolute ), Null  );  VaR File = Await  Storagefile. openasync (Windows. Storage. fileaccessmode. Read );  //  Source image Bitmapimage bitimage1 = New  Bitmapimage (); bitimage1.setsource (File); IMG. Source = Bitimage1; TXT. Text = "  Source image size:  " + File. size;  //  Source image compression              VaR Required image = New  Using Image (file. asstream (); system. Io. memorystream stream =New  Memorystream (); writable image. writejpeg (stream,  New Compressionparameters () {Quality = 20 }); //  The compression quality is 20%.              Byte [] Buffer = New   Byte  [Stream. Length]; stream. Seek (  0  , Seekorigin. Begin); stream. Read (buffer,  0 , Buffer. Length );  VaR Ibuffer = Cryptographicbuffer. createfrombytearray (buffer); inmemoryrandomaccessstream memoryrandomstream = New  Inmemoryrandomaccessstream ();  Await  Memoryrandomstream. writeasync (ibuffer); memoryrandomstream. Seek (  0  ); Bitmapimage bitimage2 = New Bitmapimage (); bitimage2.setsource (memoryrandomstream); img2.source = Bitimage2; txt2.text = "  Size of the compressed image:  " + Memoryrandomstream. size ;} 

Download complete code

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.