VB.net导出表格为PDF格式

时间:2017-08-17 17:16:11

标签: vb.net image pdf

因此,我正在制作物理治疗环境的文档程序,并且正在努力将表单上的数据导出到pdf或某种形式的图像。我的表格如下:

我尝试使用以下代码从中创建图像

    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

但每次它只作为表格的一部分回来。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以使用此代码:

  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

来自https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d258c2b-64b9-431f-9df8-398a7866de40/vbnet-save-windows-form-as-an-image-getformimage?forum=vbgeneral

然后调用该函数:

GetFormImage(*True to include the borders*).Save("C:\Student.jpg", ImageFormat.Jpeg)