3

I want to save an image using File Save Picker . I am using this link to save but it is only for text , how I modify it to save an image?

Romasz
  • 29,662
  • 13
  • 79
  • 154
user3758386
  • 83
  • 1
  • 8
  • Do you mean how you could add the option to save a file as .jpg for example? – sebhaub Jul 17 '14 at 14:07
  • You get as a result [StorageFile](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.storagefile.aspx) - open a stream for it and then write to that stream. Have you tried something similar? Also are you targetting Silverlight or Runtime? – Romasz Jul 17 '14 at 14:14
  • Yes I mean how could I add options to save it as png and I am targetting runtime not silverlight. – user3758386 Jul 17 '14 at 15:06

1 Answers1

3

As you have provided the link then I assume that you managed to get StorageFile after Continuation (this is how it works at WP8.1 Runtime).

I also assume that you have a Stream with your image or you know how to obtain such a one. Basing on those two, you can save your image in png format to a file selected by picker for example like this:

public async Task SaveStreamAsync(IRandomAccessStream streamToSave, StorageFile destination)
{
    BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(streamToSave);
    PixelDataProvider pixelData = await bmpDecoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, null, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
    using (var destFileStream = await destination.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destFileStream);
        uint yourWidthAndOrHeight = 1024;
        bmpEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, yourWidthAndOrHeight, yourWidthAndOrHeight, 300, 300, pixelData.DetachPixelData());
        await bmpEncoder.FlushAsync();
    }
}

Also please remember to Dispose your streamToSave (and other resources) after finishing working with them.

If you take a look at BitmapEncoder and BitmapDecoder classes, then you will see more options including transformation and various properties.

(I've not tested the code above rough, but hopefully it will work fine)

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • I have a video in StorgaeFile object. How can I get the first frame from video and display it using BitmapImage to user? I am also saving the video in other folder. – Kinjan Bhavsar Apr 15 '15 at 09:56
  • @KinjanBhavsar There is no place in comments to ask/answer such question. It would be better if you have asked simply a new question, post some of your code, mention the research you have done. Currently I don't know, but maybe someone will. – Romasz Apr 15 '15 at 10:08
  • @Romasz The width and height in setpixeldata method should be get from bmpDecoder.getPixelWidth()/bmpDecoder.getPixelHeight() or the pixels should be calcaulated manually based on yourWidthAndOrHeight. The above code can sometimes throw “The buffer allocated is insufficient” exception. – MohanRajNK May 19 '15 at 10:45
  • @MohanRajNK This is only a sample and I assume that the person who uses it knows what height/width to use or how to get it. – Romasz May 19 '15 at 12:40