Printing a form with VB.Net

  • Thread starter Thread starter Hotrod2000
  • Start date Start date
H

Hotrod2000

I'am trying to print a windows form with the following code, but keep
getting this error message (posted below code).
My form has a few unused labels on in and a print button (Button1)

Any help would be greatly appreiciated :-

Public Class Form1

Private WithEvents pd As Printing.PrintDocument
Dim formImage As Bitmap
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 hdcScr As IntPtr, ByVal nXSrc As Integer, ByVal NySrc As
Integer, ByVal dwRop As System.Int32) As Long


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
pd = New Printing.PrintDocument
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
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 Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
GetFormImage()
pd.Print()
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
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, dc1, 0
- borderSize, 0 - heightTitleBar, 13369376)
g.ReleaseHdc(dc1)
mg.ReleaseHdc(dc2)

End Sub
End Class


The error I get is this :-

Argument not specified for parameter 'dwRop' of 'Declare Ansi Function
BitBlt Lib "gdi32.dll" Alias "BitBlt" (hdcDest As System.IntPtr,
nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As
Integer, hdcScr As System.IntPtr, nXSrc As Integer, NySrc As Integer,
dwRop As Integer) As Long'.

Thanks in anticipation.

P
 
Am 08.03.2010 13:05, schrieb Hotrod2000:
I'am trying to print a windows form with the following code, but keep
getting this error message (posted below code).
My form has a few unused labels on in and a print button (Button1)

Any help would be greatly appreiciated :-

Public Class Form1

Private WithEvents pd As Printing.PrintDocument
Dim formImage As Bitmap
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 hdcScr As IntPtr, ByVal nXSrc As Integer, ByVal NySrc As
Integer, ByVal dwRop As System.Int32) As Long

I didn't read everything because the first step is declaring the
return value As Boolean (not Long).

(dwRop can be declared as UInteger (UInt32), but that doesn't change anything)
 
Am 08.03.2010 13:05, schrieb Hotrod2000:
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 hdcScr As IntPtr, ByVal nXSrc As Integer, ByVal NySrc As
Integer, ByVal dwRop As System.Int32) As Long


BitBlt(dc2, 0, 0, Me.ClientRectangle.Width + widthDiff, dc1, 0
- borderSize, 0 - heightTitleBar, 13369376)


The error I get is this :-

Argument not specified for parameter 'dwRop' of 'Declare Ansi Function
BitBlt Lib "gdi32.dll" Alias "BitBlt" (hdcDest As System.IntPtr,
nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As
Integer, hdcScr As System.IntPtr, nXSrc As Integer, NySrc As Integer,
dwRop As Integer) As Long'.

I should have read everything...

The message says an argument is missing. Use intellisense while typing.

BitBlt( _
dc2, 0, 0, _
Me.ClientRectangle.Width + widthDiff, <ARGUMENT MISSING HERE>, _
dc1, 0 - borderSize, 0 - heightTitleBar, 13369376 _
)

The missing argument is the height of the rectangle.


In addition, have a look at the methods
- System.Drawing.Graphics.CopyFromScreen
- System.Windows.Forms.Control.DrawToBitmap
 
Am 08.03.2010 13:05, schrieb Hotrod2000:



I should have read everything...

The message says an argument is missing. Use intellisense while typing.

   BitBlt( _
      dc2, 0, 0, _
      Me.ClientRectangle.Width + widthDiff, <ARGUMENT MISSING HERE>, _
      dc1, 0 - borderSize, 0 - heightTitleBar, 13369376 _
   )

The missing argument is the height of the rectangle.

In addition, have a look at the methods
- System.Drawing.Graphics.CopyFromScreen
- System.Windows.Forms.Control.DrawToBitmap


Thanks Armin,

Yep I've put the missing argument in !!! I must check my code writing
more carefully in future !!!

Works a treat with some height code !!!

Thanks again.
 
Back
Top