[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
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.
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.
Last edited by VS2013; Apr 12th, 2024 at 03:34 PM.
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
Re: Not showing any data exists on a form except a blank window while taking screensh
Originally Posted by dbasnett
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.
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
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.
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.
Last edited by VS2013; Apr 22nd, 2024 at 05:59 PM.
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
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.
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.