Save Screenshot of element

Visual Basic tutorials for UWP apps.
3 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Save Screenshot of element
CodenStuff
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:
Code: Select all
' 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
Just change "WHAT-TO-CAPTURE" to the name of your element/control.

RenderTargetBitmap class
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
kolega28
VIP - Donator
VIP - Donator
Posts: 338
Joined: Mon Jan 17, 2011 7:12 pm

Re: Save Screenshot of element
kolega28
This makes screenshot programs so easy to make now (not that they were very difficult previously or something)
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Save Screenshot of element
comathi
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
3 posts Page 1 of 1
Return to “Visual Basic”