Printing a screen

TomPhillips

Active member
Joined
Feb 24, 2005
Messages
33
Programming Experience
10+
Printing a screen RESOLVED

To print a form from the screen, I borrowed the following from ms-help:

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

Dim memoryImage As Bitmap

Private Sub CaptureScreen()

Dim mygraphics As Graphics = Me.CreateGraphics()

Dim s As Size = Me.Size

memoryImage = New Bitmap(s.Width, s.Height, mygraphics)

Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)

Dim dc1 As IntPtr = mygraphics.GetHdc

Dim dc2 As IntPtr = memoryGraphics.GetHdc

BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, _

Me.ClientRectangle.Height, dc1, 20, 0, 13369376)

mygraphics.ReleaseHdc(dc1)

memoryGraphics.ReleaseHdc(dc2)

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _

PrintDocument1.PrintPage

e.Graphics.DrawImage(memoryImage, 0, 0)

End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click

Dim PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog

CaptureScreen()

Me.PrintDocument1.DefaultPageSettings.Landscape = True

PrintPreviewDialog1.Document = PrintDocument1

PrintPreviewDialog1.Show()

End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click

PrintDialog1.ShowDialog()

End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click

CaptureScreen()

Me.PrintDocument1.DefaultPageSettings.Landscape = True

Me.PrintDocument1.Print()

End Sub


It works fine except that it wants to fill the page overflowing the margins.


Question: How can I reduce the captured image to fit the paper?
 
Last edited:
You need to resize memoryImage. You can do that using another bitmap like this:
Dim ResizedBmp as new bitmap(memoryImage,Width,Height)
 
Not seeing it.

Here is how I added it:

Private Sub CaptureScreen()
Dim mygraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.SizememoryImage = New Bitmap(s.Width, s.Height, mygraphics)

Dim ResizedBmp As New Bitmap(memoryImage, s.Width - 200, s.Height - 200)
Dim memoryGraphics As Graphics = Graphics.FromImage(ResizedBmp)
'Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)

Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
BitBlt(dc2, 10, 20, Me.ClientRectangle.Width - 10, _
Me.ClientRectangle.Height - 10, dc1, 10, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
End Sub

When I run it, I get a blank image.
 
Resize it in your print event, that way you know the paper size.

The reason it's blank is probably because your not setting memoryimage to the resizedimage. But if you resize it in the print event it won't matter...
 
Back
Top