locked
I need help to figure out how to save an image using the FileSavePicker

    Question

  • I Can not figure out how to get my RenderTargetBitmap into my saveStream, This is what i have so far:

    async void saveButton_Click(object sender, RoutedEventArgs e)
    {
      var rtb = new RenderTargetBitmap();
      await rtb.RenderAsync(m_ShieldGUI, (int)m_ShieldGUI.Width, (int)m_ShieldGUI.Height);

      var Picker = new FileSavePicker();
      Picker.FileTypeChoices.Add("Image", new List<string>() { ".png" });
      StorageFile file = await Picker.PickSaveFileAsync();
      using (Stream saveStream = await file.OpenStreamForWriteAsync())
      {
                  
      }
    }

    I would prefer if i could save the image as a png but at this point i be happy to be able to save it in any image-format.

    Monday, April 6, 2015 3:34 PM

Answers

  • Something along these lines...

            async void saveButton_Click(object sender, RoutedEventArgs e)
            {
                var rtb = new RenderTargetBitmap();
                await rtb.RenderAsync(toSave, (int)toSave.Width, (int)toSave.Height);
    
                var pixelBuffer = await rtb.GetPixelsAsync();
                byte[] pixels = pixelBuffer.ToArray();
    
                using (var stream = new InMemoryRandomAccessStream())
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, pixels);
                    await encoder.FlushAsync();
                    
                    var Picker = new FileSavePicker();
                    Picker.FileTypeChoices.Add("Image", new List<string>() { ".png" });
                    StorageFile file = await Picker.PickSaveFileAsync();
                    stream.Seek(0);
                    
                    using (var str = stream.GetInputStreamAt(0).AsStreamForRead())
                    using (Stream saveStream = await file.OpenStreamForWriteAsync())
                    {
                        await str.CopyToAsync(saveStream);
                    }
                }
            }
    


    http://peted.azurewebsites.net/

    • Marked as answer by Zadrith Monday, April 6, 2015 8:08 PM
    Monday, April 6, 2015 7:11 PM

All replies

  • Something along these lines...

            async void saveButton_Click(object sender, RoutedEventArgs e)
            {
                var rtb = new RenderTargetBitmap();
                await rtb.RenderAsync(toSave, (int)toSave.Width, (int)toSave.Height);
    
                var pixelBuffer = await rtb.GetPixelsAsync();
                byte[] pixels = pixelBuffer.ToArray();
    
                using (var stream = new InMemoryRandomAccessStream())
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96, 96, pixels);
                    await encoder.FlushAsync();
                    
                    var Picker = new FileSavePicker();
                    Picker.FileTypeChoices.Add("Image", new List<string>() { ".png" });
                    StorageFile file = await Picker.PickSaveFileAsync();
                    stream.Seek(0);
                    
                    using (var str = stream.GetInputStreamAt(0).AsStreamForRead())
                    using (Stream saveStream = await file.OpenStreamForWriteAsync())
                    {
                        await str.CopyToAsync(saveStream);
                    }
                }
            }
    


    http://peted.azurewebsites.net/

    • Marked as answer by Zadrith Monday, April 6, 2015 8:08 PM
    Monday, April 6, 2015 7:11 PM
  • Thank you, I found something simular in VB when i was searching for a solution but was not able to use it and i get the same errors with your code.

    The first problem is that pixelBuffer does not have a ToArray method.

    The second problem is that my code does not recognize BitmapEncoder, BitmapPixelFormat or BitmapAlphaMode. I guess i just need to add a using-statement of som kind. I tried Windows.UI.Xaml.Media.Imaging but it does not help.

     

    Monday, April 6, 2015 7:36 PM
  • try

    using System.Runtime.InteropServices.WindowsRuntime;

    for the toArray() - it's an extension method.

    and

    using Windows.Graphics.Imaging;

    for the Bitmap things.


    http://peted.azurewebsites.net/

    Monday, April 6, 2015 7:45 PM
  • Thank you!!!

    Now it works just like I wanted it to :) 

    Monday, April 6, 2015 8:08 PM