从剪贴板保存图像
Posted
技术标签:
【中文标题】从剪贴板保存图像【英文标题】:Saving image form Clipboard 【发布时间】:2013-06-06 15:12:20 【问题描述】:我想在 winrt 中将剪贴板中的一个图像保存到文件中。但我没有办法。你能帮忙吗?
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
对象...所以:image.Save("C:\\myDir\\myFile.png", System.Drawing.Imaging.ImageFormat.Png);
Image.save 在 Windows RT 的 Windows.ui.controls.image 中不可用
如果对您有帮助,请接受我的回答。这也将有助于其他可能面临此类问题的开发人员。
【参考方案1】:
给你:)
请注意,您不能使用任何文件夹进行保存。我已经通过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;
【讨论】:
【参考方案2】:就这么简单,只需将最后一行添加到现有代码中(例如用于 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);
Link for Supported imageformates
【讨论】:
Image.save 在 Windows RT 的 Windows.ui.controls.image 中不可用。image.Save
不是 WinRT API 的一部分。【参考方案3】:
最后一个帖子示例保存 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;
【讨论】:
以上是关于从剪贴板保存图像的主要内容,如果未能解决你的问题,请参考以下文章
python 当做图床使用,读取粘贴板的图像保存上传,完成后将URL地址写入到剪贴板