Compress and save base64 image on Windows Phone 8.1

南笙酒味 提交于 2019-12-17 14:20:23

问题


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.

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;
}

回答1:


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;
}


来源:https://stackoverflow.com/questions/25994558/compress-and-save-base64-image-on-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!