Save Screenshot of element
Visual Basic tutorials for UWP apps.
3 posts
Page 1 of 1
Snap Happy
In Windows 8.1 / VS 2013 you are now able to take a screen capture of an element in your app using 'RenderTargetBitmap'. Finally lol.
Here's the basic code taken from an SDK example for those who want it:
RenderTargetBitmap class
In Windows 8.1 / VS 2013 you are now able to take a screen capture of an element in your app using 'RenderTargetBitmap'. Finally lol.
Here's the basic code taken from an SDK example for those who want it:
Code: Select all
Just change "WHAT-TO-CAPTURE" to the name of your element/control.' Render to an image at the current system scale and retrieve pixel contents
Dim renderTargetBitmap As New RenderTargetBitmap()
Await renderTargetBitmap.RenderAsync(WHAT-TO-CAPTURE)
Dim pixelBuffer = Await renderTargetBitmap.GetPixelsAsync()
Dim savePicker = New FileSavePicker()
savePicker.DefaultFileExtension = ".png"
savePicker.FileTypeChoices.Add(".png", New List(Of String)() From {".png"})
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
savePicker.SuggestedFileName = "snapshot.png"
' Prompt the user to select a file
Dim saveFile = Await savePicker.PickSaveFileAsync()
' Verify the user selected a file
If saveFile Is Nothing Then
Return
End If
' Encode the image to the selected file on disk
Using fileStream = Await saveFile.OpenAsync(FileAccessMode.ReadWrite)
Dim encoder = Await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream)
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, CUInt(renderTargetBitmap.PixelWidth), CUInt(renderTargetBitmap.PixelHeight), DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray())
Await encoder.FlushAsync()
End Using
' Create the message dialog and set its content and title
Dim messageDialog = New MessageDialog("Image Saved Successfully.")
' Show the message dialog
Await messageDialog.ShowAsync
RenderTargetBitmap class
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
This makes screenshot programs so easy to make now (not that they were very difficult previously or something)
Wasn't it much simpler in VB.NET with control.drawBitmap() or something?
In any case, I'm suprised they've only just now added the ability to take screenshots of program elements... I'd think it would be an obvious thing to add lol
In any case, I'm suprised they've only just now added the ability to take screenshots of program elements... I'd think it would be an obvious thing to add lol
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023