Windows Phone 8.1 - 在加载前获得缩小的图像

时间:2015-02-20 20:05:08

标签: c# windows-runtime windows-phone windows-phone-8.1

我正在制作Windows Phone 8.1应用程序(Windows运行时,而不是Windows Phone Silverlight 8.1)。在我的应用程序中,我需要处理图像。如果图像是12MP或更低,一切正常,但如果图像太大,比如说41MP,则抛出OutOfMemoryException。 我知道使用BitmapImage,您可以设置DecodePixelWidth或DecodePixelHeight属性,以便在解码之前使图像更小。 但我需要使用WriteableBitmap。那么,有没有办法将BitmapImage转换为WriteableBitmap?如果没有,如何在解码其内容之前将WriteableBitmap设置为缩小?

我的代码如下:

StorageFolder installationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        string imagePath = @"Assets\trees201205150056_1337809980.jpg";
        StorageFile file = await installationFolder.GetFileAsync(imagePath);
        using(Stream stream = await file.OpenStreamForReadAsync())
        {
            WriteableBitmap writeableBmp = await BitmapFactory.New(1, 1).FromStream(stream.AsRandomAccessStream());
            testPreviewImage.Source = writeableBmp;
            writeableBmp = null;
        }

使用BitmapImage会是这样的:

StorageFolder installationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        string imagePath = @"Assets\trees201205150056_1337809980.jpg";
        StorageFile file = await installationFolder.GetFileAsync(imagePath);
        using(Stream stream = await file.OpenStreamForReadAsync())
        {
            BitmapImage bitmapImage = new BitmapImage { DecodePixelWidth = 2000};
            testPreviewImage.Source = bitmapImage;
            bitmapImage = null;
        }

谢谢。

0 个答案:

没有答案