2017-08-17 251 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