windows 8 metro 風格開發(8)-圖片壓縮

來源:互聯網
上載者:User

目前win8 的SDK中還沒有圖片壓縮處理的API,只能藉助第三方的類庫來處理。可惜專門針對win8的圖片壓縮處理的第三方類庫也沒有。不過還好,win8的API跟Silverlight的API是差不多的,所以可以直接從Silverlight中移植過來。

我找了一個LibJpeg.net的Silverlight版的類庫源碼,經過簡單的修改,改為win8版的圖片壓縮處理類庫。這個類庫可以對圖片進行按品質百分比的壓縮。

下面貼出部分代碼。

    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);            //原圖            BitmapImage bitImage1 = new BitmapImage();            bitImage1.SetSource(file);            img.Source = bitImage1;            txt.Text = "原圖大小:" + file.Size;            //原圖壓縮處理            var jpegimage = new JpegImage(file.AsStream());            System.IO.MemoryStream stream = new MemoryStream();            jpegimage.WriteJpeg(stream, new CompressionParameters() { Quality = 20 });//壓縮品質為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 = "壓縮後圖片大小:" + memoryRandomStream.Size;        }

完整代碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.