5

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;<
5
  • 1
    What is the problem? Does this error? Commented Jun 6, 2013 at 15:14
  • no error. but i don´t know how i can save it to file. Commented Jun 6, 2013 at 15:18
  • You have an Image object...so: image.Save("C:\\myDir\\myFile.png", System.Drawing.Imaging.ImageFormat.Png); Commented Jun 6, 2013 at 15:20
  • Image.save is not aviable in Windows.ui.controls.image at Windows RT Commented Jun 6, 2013 at 16:29
  • Please accept my answer if it helped you. It will also help to other developers who could face such problem. Commented Mar 13, 2015 at 9:04

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As simple as that, just added the last line to your existing code (e.g. for 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

2 Comments

Image.save is not aviable in Windows.ui.controls.image at Windows RT.
image.Save is not the part of WinRT APIs.
0

Last post example saves an BMP image instead of a PNG image. The following solution works for me if I want to save PNG files from Clipboard:

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.