我有Windows.Graphics.Imaging ImageStream。很容易将它作为XAML图像的来源:
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
XAMLImage.Source = bitmapImage;
但我没想出如何将它保存到Windows 10应用程序中的图像文件(png,jpg,...)。
Thnx寻求帮助。
答案 0 :(得分:3)
你不能将BitmapImage保存为png或jepg文件,你应该使用WriteableBitmap代替。
CODE(如何将WriteableBitmap保存为JPEG):
var image = new WriteableBitmap(50, 50);
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/StoreLogo.png"));
var content = await file.OpenReadAsync();
image.SetSource(content);
var saveAsTarget = await KnownFolders.PicturesLibrary.CreateFileAsync("saveas.jpg");
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
BitmapEncoder.JpegEncoderId,
await saveAsTarget.OpenAsync(FileAccessMode.ReadWrite));
Stream pixelStream = image.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)image.PixelWidth, (uint)image.PixelHeight, 96.0, 96.0, pixels);
await encoder.FlushAsync();