שתף באמצעות


vb.net taking screenshot of windows form

Question

Friday, November 13, 2020 10:38 PM

Hi, Im taking a screenshot of a windows form that has a fixed size. I can take the screenshot and set the image size. The problem is if the user moves the window the screenshot will include some of the background I dont want. The code being used is.

Dim frmleft As System.Drawing.Point = Me.Bounds.Location
        Dim screenx As Integer = frmleft.X
        Dim screeny As Integer = frmleft.Y

        Dim r As New RECT
        GetWindowRect(GetActiveWindow, r)
        Dim img As New Bitmap(1410, 787)
        Dim gr As Graphics = Graphics.FromImage(img)
        gr.CopyFromScreen(screenx, screeny, 0, 0, img.Size)

        img.Save("test.png")
        Process.Start("test.png")

Is there away to make sure when I take a screenshot I only get the windows form?  so if I have other windows open in the background, none of that is included.

Thanks Nige

All replies (5)

Saturday, November 14, 2020 3:03 AM

I have a C# library which works with VB.NET (as done in this project)

  • To use it download the following C# project.
  • Check the .NET Framework used against your project, I used 4.7.2
  • Build the C# project.
  • Add a reference to the C# project to your VB.NET project
  • In the form, add an Import statement -> Imports ScreenLibrary.
  • Place a button on your form with the following code.
Dim fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Name}.jpg")
Dim screenOperations As New ScreenCapture
screenOperations.CaptureWindowToFile(Handle, fileName, Imaging.ImageFormat.Jpeg)
Process.Start(fileName)

I've used this library in both C# and VB.NET

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

My GitHub code samples
GitHub page


Saturday, November 14, 2020 9:50 AM

You can just use PrintWindow : (there is also DrawToBitmap but without DWM)

Using bmp As Bitmap = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
    Using gr As Graphics = Graphics.FromImage(bmp)
        Dim hDC As IntPtr = gr.GetHdc()
        PrintWindow(Me.Handle, hDC, PW_RENDERFULLCONTENT)
        gr.ReleaseHdc()
    End Using
    bmp.Save("E:\\test\\TestScreenshot.png", System.Drawing.Imaging.ImageFormat.Png)
End Using
    <DllImport("User32.dll", SetLastError:=True)>
    Private Shared Function PrintWindow(ByVal hwnd As IntPtr, ByVal hDC As IntPtr, ByVal nFlags As UInteger) As Boolean
    End Function

    Public Const PW_CLIENTONLY = &H1
    Public Const PW_RENDERFULLCONTENT = &H2

Saturday, November 14, 2020 10:42 AM

Hi Karen,

I've built the library and tried the code you suggested but unfortunately the image still takes a picture of the background as well, this is similar to what my other code did. Any ideas of how to fix this? 

Thanks, Nige.


Sunday, November 15, 2020 2:20 AM

I have this code running on Windows 10 on over 500 computers for taking screenshots when an exception is thrown which doesn't happen often yet when it does always works perfectly. Sample code ran on Windows 7 years ago, never an issue either.

So with that it may be something with your video card/software. Can you try it on another computer?

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

My GitHub code samples
GitHub page


Monday, November 16, 2020 8:30 AM

Hi nigelsvision,

You can consider first taking a screenshot of a active windows form before resizing the image.

I make a test on my side and you can refer to the following code.

        Dim destRect = New Rectangle(0, 0, 1410, 787)
        Dim destImage = New Bitmap(1410, 787)

        Dim frm = Form.ActiveForm
        Using bmp = New Bitmap(frm.Width, frm.Height)
            frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))

            destImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution)
            Using g = Graphics.FromImage(destImage)
                g.CompositingMode = CompositingMode.SourceCopy
                g.CompositingQuality = CompositingQuality.HighQuality
                g.InterpolationMode = InterpolationMode.HighQualityBicubic
                g.SmoothingMode = SmoothingMode.HighQuality
                g.PixelOffsetMode = PixelOffsetMode.HighQuality
                Using wrapMod = New ImageAttributes()
                    wrapMod.SetWrapMode(WrapMode.TileFlipXY)
                    g.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, wrapMod)
                End Using
            End Using

            destImage.Save("your file path")
        End Using

Some code based on this reference.

Hope it could be helpful.

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.