我想将winrt中的一个Image保存到文件夹中。但我发现没有办法。你能帮忙吗?
var dataPackage = Clipboard.GetContent();
var t = await dataPackage.GetBitmapAsync();
var t2 = await t.OpenReadAsync();
t2.AsStream();
t2.Seek(0);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(t2);
Image image = new Image();
image.Source = bitmapImage;<
答案 0 :(得分:4)
你走了:))
请注意,您无法使用任何文件夹进行保存。我已经通过ApplicationData.Current.LocalFolder.Path
作为设计。您可以使用FolderPicker
然后传递拾取文件夹的路径。
private async Task StoreImageFromClipboardAsync()
{
var dataPackage = Clipboard.GetContent();
var formats = dataPackage.AvailableFormats;
if (formats.Contains("Bitmap"))
{
var t = await dataPackage.GetBitmapAsync();
var file = await ChangeIRASRToStorageFileAsync(t, ApplicationData.Current.LocalFolder.Path, "Clipboard.png");
}
}
private async Task<StorageFile> ChangeIRASRToStorageFileAsync(IRandomAccessStreamReference MyIRASR, String StorageFolderPath, String StorageFileName)
{
IRandomAccessStreamWithContentType MyIRASWCT = await MyIRASR.OpenReadAsync();
StorageFolder MyStorageFolder = await StorageFolder.GetFolderFromPathAsync(StorageFolderPath);
StorageFile MyStorageFile = await MyStorageFolder.CreateFileAsync(StorageFileName, CreationCollisionOption.ReplaceExisting);
Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(MyIRASWCT.Size));
IBuffer iBuf = await MyIRASWCT.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
await FileIO.WriteBufferAsync(MyStorageFile, iBuf);
return MyStorageFile;
}
答案 1 :(得分:0)
就这么简单,只需将最后一行添加到现有代码中(例如,对于PNG)
var dataPackage = Clipboard.GetContent();
var t = await dataPackage.GetBitmapAsync();
var t2 = await t.OpenReadAsync();
t2.AsStream();
t2.Seek(0);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(t2);
Image image = new Image();
image.Source = bitmapImage;
image.Save("ImagePathToStore.png", System.Drawing.Imaging.ImageFormat.PNG);
答案 2 :(得分:0)
上一篇文章示例保存BMP图像而不是PNG图像。如果我想从剪贴板保存PNG文件,以下解决方案适用于我:
private async Task StoreImageFromClipboardAsync()
{
var dataPackage = Clipboard.GetContent();
if (datapackage.Contains(StandardDataFormats.Bitmap))
{
var t = await dataPackage.GetBitmapAsync();
var file = await SaveToPngTaskFile(t, ApplicationData.Current.LocalFolder,
"Clipboard.png");
}
}
public static async Task<StorageFile> SaveToPngTaskFile
(IRandomAccessStreamReference rndAccessStreamReference,
StorageFolder storageFolder, String storageFileName)
{
IRandomAccessStreamWithContentType rndAccessStreamWithContentType =
await rndAccessStreamReference.OpenReadAsync();
StorageFile storageFile =
await storageFolder.CreateFileAsync(storageFileName,
CreationCollisionOption.GenerateUniqueName);
var decoder = await BitmapDecoder.CreateAsync(rndAccessStreamWithContentType);
var pixels = await decoder.GetPixelDataAsync();
var outStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream);
encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
decoder.DpiX, decoder.DpiY,
pixels.DetachPixelData());
await encoder.FlushAsync();
outStream.Dispose();
return storageFile;
}