在 Windows Phone 8.1 上压缩并保存 base64 图像 [英] Compress and save base64 image on Windows Phone 8.1

查看:22
本文介绍了在 Windows Phone 8.1 上压缩并保存 base64 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实施了以下解决方案来压缩 base 64 图像并取回新的 base 64 字符串.它在 Windows Phone 8.0 中运行良好,但针对 Windows Phone 8.1 似乎环境发生了变化.

I have implemented the following solution to compress a base 64 image and get back the new base 64 string. It works fine in Windows Phone 8.0 but targeting Windows Phone 8.1 it seems that there are changes in the environment.

WriteableBitmap 没有 BitmapImage 的构造函数,WriteableBitmap 没有函数 SaveJpeg.我知道 SaveJpeg 是一个扩展,有没有办法将此扩展添加到 Windows Phone 8.1?或者是否有任何我可以使用的 API?我必须改变什么才能使这个 8.1 兼容?我有点坚持在这里:-/

The WriteableBitmap has no constructor for a BitmapImage and the WriteableBitmap has no function SaveJpeg. I know that SaveJpeg is an extension, is there a way to add this extension to Windows Phone 8.1? Or is there any API which I can use? What do I have to change to make this 8.1 compatible? I'm kinda stuck with it here :-/

public static string Compress(String base64String, int compression)
{
    String compressedImage;

    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);

    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(memoryStream.AsRandomAccessStream());

    WriteableBitmap bmp = new WriteableBitmap(bitmapImage);

    int height = bmp.PixelHeight;
    int width = bmp.PixelWidth;
    int orientation = 0;
    int quality = 100 - compression;

    MemoryStream targetStream = new MemoryStream();
    bmp.SaveJpeg(targetStream, width, height, orientation, quality);

    byte[] targetImage = targetStream.ToArray();
    compressedImage = System.Convert.ToBase64String(targetImage);

    return compressedImage;
}

推荐答案

在 WP8.1 运行时我使用了 BitmapPropertySet 定义压缩级别.下面是在 Streams 上运行的示例代码:

In WP8.1 Runtime I've used BitmapPropertySet to define a level of compression. Here below is sample code operating on Streams:

/// <summary>
/// Method compressing image stored in stream
/// </summary>
/// <param name="sourceStream">stream with the image</param>
/// <param name="quality">new quality of the image 0.0 - 1.0</param>
/// <returns></returns>
private async Task<IRandomAccessStream> CompressImageAsync(IRandomAccessStream sourceStream, double newQuality)
{
    // create bitmap decoder from source stream
    BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(sourceStream);

    // bitmap transform if you need any
    BitmapTransform bmpTransform = new BitmapTransform() { ScaledHeight = newHeight, ScaledWidth = newWidth, InterpolationMode = BitmapInterpolationMode.Cubic };

    PixelDataProvider pixelData = await bmpDecoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, bmpTransform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
    InMemoryRandomAccessStream destStream = new InMemoryRandomAccessStream(); // destination stream

    // define new quality for the image
    var propertySet = new BitmapPropertySet();
    var quality = new BitmapTypedValue(newQuality, PropertyType.Single);
    propertySet.Add("ImageQuality", quality);

    // create encoder with desired quality
    BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destFileStream, propertySet);
    bmpEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, newHeight, newWidth, 300, 300, pixelData.DetachPixelData());
    await bmpEncoder.FlushAsync();
    return destStream;
}

这篇关于在 Windows Phone 8.1 上压缩并保存 base64 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆