Windows phone 在Windows Phone 8.1上压缩并保存base64映像

Windows phone 在Windows Phone 8.1上压缩并保存base64映像,windows-phone,windows-phone-8.1,Windows Phone,Windows Phone 8.1,我已经实现了下面的解决方案来压缩一个base64映像并返回新的base64字符串。它在Windows Phone 8.0中运行良好,但针对Windows Phone 8.1,环境似乎发生了变化 WriteableBitmap没有用于BitmapImage的构造函数,WriteableBitmap没有函数SaveJpeg。我知道SaveJpeg是一个扩展,有没有办法将此扩展添加到Windows Phone 8.1?或者有我可以使用的API吗?要使8.1兼容,我需要做哪些更改?我有点被困在这里:-/

我已经实现了下面的解决方案来压缩一个base64映像并返回新的base64字符串。它在Windows Phone 8.0中运行良好,但针对Windows Phone 8.1,环境似乎发生了变化

WriteableBitmap
没有用于
BitmapImage
的构造函数,
WriteableBitmap
没有函数
SaveJpeg
。我知道
SaveJpeg
是一个扩展,有没有办法将此扩展添加到Windows Phone 8.1?或者有我可以使用的API吗?要使8.1兼容,我需要做哪些更改?我有点被困在这里:-/

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运行时中,我曾经定义过压缩级别。下面是在流上操作的示例代码:

//
///压缩流中存储的图像的方法
/// 
///图像流
///新的图像质量0.0-1.0
/// 
专用异步任务CompressImageAsync(IRandomAccessStream sourceStream,双新质量)
{
//从源流创建位图解码器
BitmapDecoder bmpDecoder=等待BitmapDecoder.CreateAsync(sourceStream);
//位图转换,如果你需要的话
BitmapTransform bmpTransform=new BitmapTransform(){ScaledHeight=newHeight,ScaledWidth=newWidth,InterpolationMode=BitmapInterpolationMode.Cubic};
PixelDataProvider pixelData=await bmpDecoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8,BitmapAlphaMode.Straight,bmpTransform,ExiforOrientationMode.Respect ExiforOrientation,ColorManagementMode.DoNotColorManage);
InMemoryRandomAccessStream destStream=新建InMemoryRandomAccessStream();//目标流
//定义图像的新质量
var propertySet=新的BitmapPropertySet();
var quality=新的BitmapTypedValue(newQuality,PropertyType.Single);
添加(“图像质量”,质量);
//创建具有所需质量的编码器
BitmapEncoder bmpEncoder=等待BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId,destFileStream,propertySet);
bmpEncoder.SetPixelData(BitmapPixelFormat.Rgba8,BitmapAlphaMode.Stright,newHeight,newWidth,300300,pixelData.DetachPixelData());
等待bmpEncoder.FlushAsync();
回流;
}

如果您事先不知道图像的大小,该怎么办?@JerryNixon MSFT您的意思是您不知道要将图像缩放到的大小?我的意思是,如果您从base64编码,并且不知道要在图像中设置的高度和宽度,该怎么办SetPixelData@JerryNixon-MSFT当我看OP的问题时,我认为在压缩和调整大小之前,应该可以从BitmapImage中读取大小。查看如何硬编码300和300作为高度和宽度?这就是我试图解决的问题。