gpt4 book ai didi

c# - 从剪贴板保存图像

转载 作者:太空狗 更新时间:2023-10-29 23:23:32 25 4
gpt4 key购买 nike

我想在 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;<

最佳答案

给你:)

请注意您不能使用任何文件夹来保存。我已通过 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;
}

关于c# - 从剪贴板保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16965776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com