2017-08-17 158 views
0

因此,我正在为物理治疗环境编写一个文档程序,并试图将出口带有其上的数据的表单导入到某种PDF或图像中。我的形式如下:VB.net导出为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 

但每次它回来,因为只有形式的部分尝试。

任何帮助,将不胜感激!

+0

我建议你放弃'Me.Size'和使用[Form.RestoreBounds](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.restorebounds(v = vs.110).aspx) –

回答

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) 
+0

对于GetFormImage(* True to include边界*)。保存(“C:\ Student.jpg “,ImageFormat.Jpeg),第一个*给出了一个表达期望的错误? –

+0

您需要将* True替换为True或False,取决于您希望图像边框是否包含在图像中。 – 0liveradam8