Sunday, August 3, 2014

Saving image from RenderTargetBitmap to disk - Visual Basic

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: http://ift.tt/1fArvuE

author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook



No comments:

Post a Comment