Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving image form Clipboard

i want to save one Image inside clipboard in winrt to file. but i found no way. can you help please?

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;<
like image 731
Taladan Avatar asked Oct 04 '22 22:10

Taladan


1 Answers

Here you go :)

Please note you can't use ANY folder to save. I have passed ApplicationData.Current.LocalFolder.Path as desination. You can use FolderPicker and then pass the path of picked folder.

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;
}
like image 172
Farhan Ghumra Avatar answered Oct 08 '22 05:10

Farhan Ghumra