我正在使用Windows Phone 8.1应用程序。我在应用程序中本地有不同的图片,并在列表视图中显示这些图片。现在选择一个图片ID并将其传递给详细信息屏幕,我必须保存该文件。
我的详细信息.Xaml屏幕包含名为Imaged的图像控件。 我在详细页面上获得了图像,然后按图像保存按钮。我正在使用此示例应用https://code.msdn.microsoft.com/windowsapps/Save-Edited-Picture-16d910c4/sourcecode?fileId=119937&pathId=946309183"示例"
我的详细信息页面代码在这里。
string tempimg;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var imagePath = e.Parameter as string;
imaged.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
tempimg = imagePath;
}
我的SaveImage按钮代码在这里。
if (tempimg == null)
return;
// WriteableBitmap abc = new WriteableBitmap(400, 400);
string filename = "Image-" + DateTime.Now.ToFileTime() + ".jpeg";
// await SaveWriteableBitmapAsJpeg(img, filename);
块引用
等待SaveWriteableBitmapAsJpeg(tempimg,filename);
private static async Task SaveWriteableBitmapAsJpeg(WriteableBitmap bmp, string fileName)
{
// Create file in Pictures library and write jpeg to it
var outputFile = await KnownFolders.CameraRoll.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
{
await EncodeWriteableBitmap(bmp, writeStream, BitmapEncoder.JpegEncoderId);
}
}
private static async Task EncodeWriteableBitmap(WriteableBitmap bmp, IRandomAccessStream writeStream, Guid encoderId)
{
// Copy buffer to pixels
byte[] pixels;
using (var stream = bmp.PixelBuffer.AsStream())
{
pixels = new byte[(uint)stream.Length];
await stream.ReadAsync(pixels, 0, pixels.Length);
}
// Encode pixels into stream
var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
(uint)bmp.PixelWidth, (uint)bmp.PixelHeight,
96, 96, pixels);
await encoder.FlushAsync();
}
我的这个功能"等待SaveWriteableBitmapAsJpeg(tempimg,filename);"造成问题。我只有图像源,而需要writeablebitmap。我只是想保存图像而不能从这里继续。让我知道确切的解决方案或问题。