Print form prints a solid black box!!!


Nov 10, 2009
Programming Experience
I am using a very simple print form procedure that I found on the internet. In one project it prints the form flawlessly. In another project it prints a solid black box!!! I'm not sure what is going on. Any assistance would be greatly appreciated.

Public Class RateSheet
' create a printing component
Private WithEvents pd As Printing.PrintDocument
' storage for form image
Dim formImage As Bitmap
' create API prototype
Private Declare Function BitBlt Lib "gdi32.dll" Alias _
"BitBlt" (ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, ByVal nYDest As _
Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal _
hdcSrc As IntPtr, ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As System.Int32) As Long
' Callback from PrintDocument component to
' do the actual printing
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles pd.PrintPage
e.Graphics.DrawImage(formImage, 100, 100)
End Sub

Private Sub RateSheet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' create an instance of the PrintDocument component
pd = New Printing.PrintDocument
Me.StartPosition = FormStartPosition.CenterScreen

lblCar1.Text = CCARater.txtCar1MakeModel.Text
lblCar2.Text = CCARater.txtCar2MakeModel.Text
lblCar3.Text = CCARater.txtCar3MakeModel.Text
lblCar4.Text = CCARater.txtCar4MakeModel.Text
lblCar5.Text = CCARater.txtCar5MakeModel.Text

lblYear1.Text = CCARater.txtCar1Year.Text
lblYear2.Text = CCARater.txtCar2Year.Text
lblYear3.Text = CCARater.txtCar3Year.Text
lblYear4.Text = CCARater.txtCar4Year.Text
lblYear5.Text = CCARater.txtCar5Year.Text

lblTerr1.Text = CCARater.txtTerritory.Text


End Sub

Private Sub GetFormImage()
Dim g As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
formImage = New Bitmap(s.Width, s.Height, g)
Dim mg As Graphics = Graphics.FromImage(formImage)
Dim dc1 As IntPtr = g.GetHdc
Dim dc2 As IntPtr = mg.GetHdc
' added code to compute and capture the form
' title bar and borders
Dim widthDiff As Integer = _
(Me.Width - Me.ClientRectangle.Width)
Dim heightDiff As Integer = _
(Me.Height - Me.ClientRectangle.Height)
Dim borderSize As Integer = widthDiff \ 2
Dim heightTitleBar As Integer = heightDiff - borderSize
BitBlt(dc2, 0, 0, _
Me.ClientRectangle.Width + widthDiff, _
Me.ClientRectangle.Height + heightDiff, dc1, _
0 - borderSize, 0 - heightTitleBar, 13369376)

End Sub

End Class
Top Bottom