Reputation: 13
So I am making a documentation program for a physical therapy environment, and am trying to encorporate the ability to export the form with the data on it to a pdf or image of some sort. My form looks like this:
I have tried using the following code to create an image from it
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
' Take a screenshot of the entire Form1
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot
bmpScreenshot.Save("C:\Student.jpg", ImageFormat.Jpeg)
End Sub
But every time it comes back as only part of the form.
Any help would be appreciated!
Upvotes: 0
Views: 2552
Reputation: 778
You can use this code:
Private Function GetFormImage(ByVal include_borders As Boolean) As Bitmap
' Make the bitmap.
Dim wid As Integer = Me.Width
Dim hgt As Integer = Me.Height
Dim bm As New Bitmap(wid, hgt)
' Draw the form onto the bitmap.
Me.DrawToBitmap(bm, New Rectangle(0, 0, wid, hgt))
' Make a smaller bitmap without borders.
wid = Me.ClientSize.Width
hgt = Me.ClientSize.Height
Dim bm2 As New Bitmap(wid, hgt)
' Get the offset from the window's corner to its client
' area's corner.
Dim pt As New Point(0, 0)
pt = PointToScreen(pt)
Dim dx As Integer = pt.X - Me.Left
Dim dy As Integer = pt.Y - Me.Top
' Copy the part of the original bitmap that we want
' into the bitmap.
Dim gr As Graphics = Graphics.FromImage(bm2)
gr.DrawImage(bm, 0, 0, New Rectangle(dx, dy, wid, hgt), GraphicsUnit.Pixel)
Return bm
End Function
And then call the function as so:
GetFormImage(*True to include the borders*).Save("C:\Student.jpg", ImageFormat.Jpeg)
Upvotes: 1