Results 1 to 14 of 14

Thread: [RESOLVED] Not showing any data exists on a form except a blank window while taking screenshot..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Resolved [RESOLVED] Not showing any data exists on a form except a blank window while taking screenshot..

    Hi,

    The following code doesn't show any data that exists on an active form. It just captures the blank window. The following is the code that I am using:

    Code:
    Private Sub btnSnapshot_Click(sender As Object, e As EventArgs) Handles btnSnapshot.Click
            Dim bmp As New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(bmp, New Rectangle(Point.Empty, bmp.Size))
            SaveSnapshot("D:\Snapshots\Invoice-001.png", bmp)
        End Sub
    Code:
    Private Sub SaveSnapshot(strFileName As String, bmp As Bitmap)
            If IO.File.Exists(strFileName) Then
                If MessageBox.Show("""" & IO.Path.GetFileName(strFileName) & """ already exists." & ControlChars.NewLine & "Do you want to replace it?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                    bmp.Save(strFileName, Imaging.ImageFormat.Png)
                Else
                    Dim dlg As New SaveFileDialog
                    dlg.InitialDirectory = IO.Path.GetDirectoryName(strFileName)
                    dlg.Filter = "PNG Picture (*.png)|*.png|All files (*.*)|*.*"
                    dlg.AddExtension = True
     
                    If dlg.ShowDialog() = DialogResult.OK Then
                        bmp.Save(dlg.FileName, Imaging.ImageFormat.Png)
                    End If
                End If
     
            Else
                bmp.Save(strFileName, Imaging.ImageFormat.Png)
            End If
        End Sub
    Please provide help to resolve the issue.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    508

    Re: Not showing any data exists on a form except a blank window while taking screensh

    You didn't include which .net version you're using. I went with 4.8.1 as a test. You didn't show a manual screenshot to show what you wanted to save compared to what was being created with your code. Using this code:

    Code:
        Private Sub btnScreenshot_Click(sender As Object, e As EventArgs) Handles btnScreenshot.Click
            Dim frm = Form.ActiveForm
            Using bmp = New Bitmap(frm.Width, frm.Height)
                frm.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
                bmp.Save("F:\temp\screenshot.png")
            End Using
    
        End Sub
    I created an image of this test form after typing some text in a datagridview, this is just a form I use when I attempt assisting others with their code.

    Everything worked as expected. The only difference I see if that this code uses ActiveForm. Which was available up until 4.8.1.

    edit: added wrong screenshot
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Target Framework: .Net 4.8

    I have a few labels and text boxes on the form. The text boxes have some data in them. I want that data to be shown when clicking the button "btnSnapshot". But it only captures the empty form. Please do support.
    Attached Images Attached Images  
    Last edited by VS2013; Apr 12th, 2024 at 03:34 PM.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,861

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Pass 'Me' to this method and then save the Bitmap

    Code:
    Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
        Dim tmpImg As New Bitmap(Control.Width, Control.Height)
        Using g As Graphics = Graphics.FromImage(tmpImg)
            g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
        End Using
        Return tmpImg
    End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Quote Originally Posted by dbasnett View Post
    Pass 'Me' to this method and then save the Bitmap

    Code:
    Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
        Dim tmpImg As New Bitmap(Control.Width, Control.Height)
        Using g As Graphics = Graphics.FromImage(tmpImg)
            g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
        End Using
        Return tmpImg
    End Function
    Got a little confusion as I don't know where to insert the code that you have provided (to my existing code). The code that I have on my form is as follows.

    Code:
    Private Sub btnSnapshot_Click(sender As Object, e As EventArgs) Handles btnSnapshot.Click
            Dim bmp As New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(bmp, New Rectangle(Point.Empty, bmp.Size))
            SaveSnapshot("D:\Snapshots\Invoice-001.png", bmp)
        End Sub
    Code:
    Private Sub SaveSnapshot(strFileName As String, bmp As Bitmap)
            If IO.File.Exists(strFileName) Then
                If MessageBox.Show("""" & IO.Path.GetFileName(strFileName) & """ already exists." & ControlChars.NewLine & "Do you want to replace it?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                    bmp.Save(strFileName, Imaging.ImageFormat.Png)
                Else
                    Dim dlg As New SaveFileDialog
                    dlg.InitialDirectory = IO.Path.GetDirectoryName(strFileName)
                    dlg.Filter = "PNG Picture (*.png)|*.png|All files (*.*)|*.*"
                    dlg.AddExtension = True
     
                    If dlg.ShowDialog() = DialogResult.OK Then
                        bmp.Save(dlg.FileName, Imaging.ImageFormat.Png)
                    End If
                End If
     
            Else
                bmp.Save(strFileName, Imaging.ImageFormat.Png)
            End If
        End Sub
    What is the full code that works for me? Seeking your support to resolve my problem.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,083

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Code:
    Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
        Dim tmpImg As New Bitmap(Control.Width, Control.Height)
        Using g As Graphics = Graphics.FromImage(tmpImg)
            g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
        End Using
        Return tmpImg
    End Function
    Code:
    Private Sub SaveSnapshot(strFileName As String, bmp As Bitmap)
        If IO.File.Exists(strFileName) Then
            If MessageBox.Show("""" & IO.Path.GetFileName(strFileName) & """ already exists." & ControlChars.NewLine & "Do you want to replace it?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                bmp.Save(strFileName, Imaging.ImageFormat.Png)
            Else
                Dim dlg As New SaveFileDialog
                dlg.InitialDirectory = IO.Path.GetDirectoryName(strFileName)
                dlg.Filter = "PNG Picture (*.png)|*.png|All files (*.*)|*.*"
                dlg.AddExtension = True
     
                If dlg.ShowDialog() = DialogResult.OK Then
                    bmp.Save(dlg.FileName, Imaging.ImageFormat.Png)
                End If
            End If
     
        Else
            bmp.Save(strFileName, Imaging.ImageFormat.Png)
        End If
    End Sub
    Code:
    Private Sub btnSnapshot_Click(sender As Object, e As EventArgs) Handles btnSnapshot.Click
        SaveSnapshot("D:\Snapshots\Invoice-001.png", TakeScreenShot(Me))
    End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Thanks for your kind support. Now, it shows data. But the problem is the active form is captured approx. 95% and around 5% is the second form which is under the active form. I only need to capture the active form. Please extend your kind support.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,083

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Is your display 100% or some different scaling?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    It is 125%. Even if I changed it to 100%, it shows the active form with textboxes and labels on the form which needs to be captured but it also shows the form beneath it (which is not required). My Active form has a Green Background. One more thing is that the active form header is not shown here which is required.
    Attached Images Attached Images  
    Last edited by VS2013; Apr 22nd, 2024 at 05:59 PM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,083

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Try this... (note, it'll only work on your monitor, or any monitor with the same scaling)

    Code:
    Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
        Dim tmpImg As New Bitmap(CInt(Control.Width / 1.25), CInt(Control.Height / 1.25))
        Using g As Graphics = Graphics.FromImage(tmpImg)
            g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(CInt(Control.Width / 1.25), CInt(Control.Height / 1.25)))
        End Using
        Return tmpImg
    End Function

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Thanks for your kind support. It's working great.

    Is it possible to get the Receipt Number automatically from the txtReceiptNo.text and pass it to the below code where it is Invoice-001.png?
    Code:
    SaveSnapshot("D:\Snapshots\Invoice-001.png", TakeScreenShot(Me))
    .

    Instead of Invoice-001.png it should be like Receipt-1037 before it is saved. (for example: In my above attachment the receipt number is 1037). The Receipt Number should be added dynamically whatever in the txtReceiptNumber.text Textbox.
    Last edited by VS2013; Apr 22nd, 2024 at 07:03 PM.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,083

    Re: Not showing any data exists on a form except a blank window while taking screensh

    You could concatenate a receipt path like this…

    SaveSnapshot("D:\Snapshots\” & txtReceiptNumber.text & “.png", TakeScreenShot(Me))

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    352

    Re: Not showing any data exists on a form except a blank window while taking screensh

    Quote Originally Posted by .paul. View Post
    You could concatenate a receipt path like this…

    SaveSnapshot("D:\Snapshots\” & txtReceiptNumber.text & “.png", TakeScreenShot(Me))
    Thanks a lot for being so kind and helpful.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,083

    Re: [RESOLVED] Not showing any data exists on a form except a blank window while taki

    Best to ensure the textbox doesn’t contain any illegal characters before saving the screenshot. If the text in the textbox is generated by your app. and always in the format you told me, and also in a readonly textbox that can’t be changed by the user, that’s not a problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width