locked
Saving image from RenderTargetBitmap to disk - Visual Basic

    Question

  • Hi,

    I'm very new to developing Windows Store apps so bear with me...Anyways, I'm developing an app that allows a user to select one of their images, do some edits and then save it to their computer. The problem is, I can't figure out how to save the edited image to their computer. I managed to get by with importing the user's image thanks to a Channel9 demo, but now I' stuck. I'd like the user to be able to select the save location themselves, so I'd like to use a FolderPicker if possible. Here's what I have so far:   

    Private Async Sub saveBtn_Click(sender As Object, e As RoutedEventArgs) Handles savebtn.Click
            Dim fileBitmap = New RenderTargetBitmap()
            Await fileBitmap.RenderAsync(imageToRender)

    As you can see, when the user clicks a save button, I render a bitmap (called fileBitmap) from a UI element (called imageToRender) using RenderTargetBitmap. Now, I'd like to save this rendered bitmap to their computer with a FolderPicker. How, using Visual Basic code, can I do this?

    Thanks, Brendon.

    Monday, August 04, 2014 2:56 AM

Answers

  • Hi Brendon,

    here the following code you need for your method to store it as a JPEG:

     Private Async Sub saveBtn_Click(sender As Object, e As RoutedEventArgs)
            Dim fileBitmaq = New RenderTargetBitmap()
            Await fileBitmaq.RenderAsync(imageToRender)
    
            Dim pixelBuffer As IBuffer = Await fileBitmaq.GetPixelsAsync()
            Dim pixels As Byte() = pixelBuffer.ToArray()
    
            Dim picker = New FileSavePicker()
            picker.FileTypeChoices.Add("JPEG", New List(Of String)() From { _
                ".jpeg", _
                ".jpg" _
            })
            Dim storageFile = Await picker.PickSaveFileAsync()
            If storageFile IsNot Nothing Then
                Using stream = Await storageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
                    Dim encoder = Await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream)
    
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, CUInt(fileBitmaq.PixelWidth), CUInt(fileBitmaq.PixelHeight), 96, 96, _
                        pixels)
    
                    Await encoder.FlushAsync()
                End Using
            End If
        End Sub


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Monday, August 04, 2014 6:12 AM

All replies

  • Hi Brendon,

    here the following code you need for your method to store it as a JPEG:

     Private Async Sub saveBtn_Click(sender As Object, e As RoutedEventArgs)
            Dim fileBitmaq = New RenderTargetBitmap()
            Await fileBitmaq.RenderAsync(imageToRender)
    
            Dim pixelBuffer As IBuffer = Await fileBitmaq.GetPixelsAsync()
            Dim pixels As Byte() = pixelBuffer.ToArray()
    
            Dim picker = New FileSavePicker()
            picker.FileTypeChoices.Add("JPEG", New List(Of String)() From { _
                ".jpeg", _
                ".jpg" _
            })
            Dim storageFile = Await picker.PickSaveFileAsync()
            If storageFile IsNot Nothing Then
                Using stream = Await storageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
                    Dim encoder = Await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream)
    
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, CUInt(fileBitmaq.PixelWidth), CUInt(fileBitmaq.PixelHeight), 96, 96, _
                        pixels)
    
                    Await encoder.FlushAsync()
                End Using
            End If
        End Sub


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Monday, August 04, 2014 6:12 AM
  • Hi Thomas,

    That's exactly what I was looking for. Thanks for your help!

    Brendon

    Monday, August 04, 2014 3:26 PM