0

I need to save an image of my application's form to a picture file, like a jpeg, bmp, or png, etc.

Numerous sources on the internet give this or similar code examples for capturing an image of a form:

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

and this to save the image of the form captured by that code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TakeScreenShot(Me).Save("c:\My Folder\Screenshot.png", System.Drawing.Imaging.ImageFormat.Png)
End Sub

The code for capturing the form's image seems to work (does not result in an error) but the line of code saving the image to file gives this error:

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in >System.Drawing.dll A generic error occurred in GDI+.

I have tried variations of the code, but in all cases the statement for saving the image to file

.Save("c:\My Folder\Screenshot.png", System.Drawing.Imaging.ImageFormat.Png)

always gives the exception error. What am I doing wrong? Any suggestions?

  • Does the user account the program is running under have write permission to the directory you want to save to? – Andrew Morton Apr 20 '21 at 16:13
  • Does this answer your question? [A generic error occurred in GDI+, JPEG Image to MemoryStream](https://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream) – Andrew Morton Apr 20 '21 at 16:17
  • Your question about write permissions gave me the solution to my problem. Thank you. – Daniel Rutschman Apr 21 '21 at 17:52
  • You're welcome :) I think that you can accept the duplicate suggestion to get this question closed, to help keep the site tidy. – Andrew Morton Apr 21 '21 at 17:54

2 Answers2

0

You are taking a rectangle from screen (in bounds of your Form) not your Form screenshot. However you have not to save directly from tmpImage (as it’s not yet released) but from a source copy of that image. The code below shows how (one of ways):

Using final As Bitmap = New Bitmap(TakeScreenShot(Me))
    final.Save("C:\My Folder\Screenshot.png")
End Using
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
0

As Andrew pointed out it is most likely a permission problem. I did it this way so you could try different locations.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim tmpImg As Bitmap = TakeScreenShot(Me)
    Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    path = IO.Path.Combine(path, "Screen.png")
    tmpImg.Save(path, System.Drawing.Imaging.ImageFormat.Png)
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • Duh to me for not thinking of that. Your suggestion solved the problem and I would "upvote" it but I don't have enough status here to do that. So I'll just say thanks and hope I don't get in trouble for that :-) – Daniel Rutschman Apr 21 '21 at 17:50
  • @DanielRutschman thanks. Mark as answer if you can. – dbasnett Apr 21 '21 at 20:53