如何在windows phone 8.1中利用FileSavePicker保存图片.windows phone 8.1图像编码的类是BitmapEncoder,解码的类是BitmapDecoder.关于图像的编码和解码的详细说明请参考msdn文档(
http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh465080.aspx
同时在啰唆一句:MSDN真心很强大!大家一定要好好利用!)
使用FileSavePicker保存图片步骤和思路和
FileSavePicker保存文件
基本是一样的,不同之处就是确定保存文件位置再次返回应用的处理.具体处理如下:
private async void ContinuFileSavePicker(FileSavePickerContinuationEventArgs args)
{
if (args.ContinuationData["Operation"] as string == "ImageSave" && args.File != null)
{
StorageFile imageFile = args.File;
StorageFile inFile= await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("3.jpg");
var inStream = await inFile.OpenAsync(FileAccessMode.Read);//获取要保存的图片的流
var outStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite);//获取要新建的图片的流
outStream.Size = 0;
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inStream);//建立解码器
PixelDataProvider provider = await decoder.GetPixelDataAsync();//获取像素数据
byte[] data = provider.DetachPixelData(); //获取像素数据的字节数组
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream);//建立编码器
encoder.SetPixelData(decoder.BitmapPixelFormat,decoder.BitmapAlphaMode,
decoder.PixelWidth,decoder.PixelHeight,
decoder.DpiX,decoder.DpiY,data
);//设置要保存的图片的数据 参数依次表示 像素格式,Alpha格式,宽度,高度,水平分辨率,垂直分辨率,要保存的像素数据
try
{
await encoder.FlushAsync(); //提交和刷新像素数据
}
catch (Exception err)
{
Debug.WriteLine(err.ToString());
}
finally
{
inStream.Dispose(); //释放流
outStream.Dispose();//释放流
}
}
}