printing screen in vb.net

shootsnlad

Well-known member
Joined
Feb 14, 2002
Messages
46
I am attempting to put in a print screen option in my vb.net program. I did some reading in the help and it says that the PrintForm function is no longer in vb.net and it suggests using a third party program to do the same functionality. The problem is that multiple users will be using this program. I dont want to have to find some third party program and then install it on every PC that needs to run the prog. Is there anyway to do something similar to PrintForm in vb.net? or Does Windows come with something that I could use and automate this procedure without using a third party program? Thanks for the help!

shootsnlad
 
This reply is for CL or Thinker. I have most of this program converted to VB .NET. I am having some problems with a few things, however. The statements "hDC = GetDC(0)" has a problem and I also cant get the HDCToPicture function to work. Where do these statements/functions come from? They are unrecognizable in .NET. The only other problem I am having is figuring out how to actually print the picture. The Printer.PaintPicture is replaced with Graphics.DrawImage in .NET.

Thanks for the help....
 
[api]GetDC[/api] is an API call, which is basically a declare to allow you to use the functions in a Windows DLL. You can use
APIs both in VB6 and VB.NET.

Follow the link for more information.
 
Well I basically have everything going except the actual printing itself. I can capture the image of the screen to an image variable. I have viewed it in a picturebox and have confirmed it to work. Now my problem is actually sending my image variable to the printer. I know I need to use a PrintDocument and Im guessing I need to use the Graphics.DrawImage method, but I can not for the life of me figure it out. I have tried numerous ways, but I just cant get the right code, and as we all know, the help in VB .NET is worthless. Has anyone done a print of an image variable to the printer successfully? Thanks for the help!
 
I havent printed from .NET yet, but presumably there is some way of getting a Graphics object from a PrintDocument. After then, you are correct, youd use the DrawImage method of the Graphics object.
 
Getting Frustrated

Im really having some problems with this printing. I keep reading the same stuff on MSs website and in the VB .NET help, but none of it works, or Im not understanding it right. I decided to post my code in hopes some humane soul quenches my frustration with some knowledge. Here it is:

Code:
Public Class mainfrm
    Private pimage As Image
      
    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim sk As SendKeys
        Dim cdata As IDataObject = Clipboard.GetDataObject()
        sk.Send("%{PRTSC}")
        pimage = cdata.GetData(DataFormats.Bitmap)
    End Sub
End Class

Its pretty simple really, I hit the Print Screen key for the user
and snap the image from the Clipboard into a variable. I have display the variable in a picturebox and it shows the printscreen. It works pretty good. Now what do I add to print the pimage variable? Help!

shootsnlad
 
I dont understand. (?) I do get the data after I hit the print screen. As it sits now, the image is stored in pimage and just needs to be printed.
 
Code:
Dim s As SendKeys
Dim pimage As Image
s.Send("%{PRTSC}")
Dim cdata As IDataObject = Clipboard.GetDataObject()
pimage = cdata.GetData(DataFormats.Bitmap)
PictureBox1.Image = pimage
 
no luck

Still no luck. Im using the drawimage method. Here is what the code now stands at:

Code:
Public Class frmHydrantCard
    Private pimage As Image
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim sk As SendKeys
        sk.Send("%{PRTSC}")
        Dim cdata As IDataObject = Clipboard.GetDataObject()
        pimage = cdata.GetData(DataFormats.Bitmap)
        PrintDocument1.Print()
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PaintEventArgs)
        e.Graphics.DrawImage(pimage, 50, 50)
    End Sub
End class

It looks like the codes there.
 
I have a create a printdocument object through the toolbox that is attached to the form. On some of the help examples it has some things like dimming a new PrintDocument. I dont have any option at all the dim a printdocument. Any other ideas? Thanks!
 
This worked for me

Just call the PrintScreen() sub from your buttonclick event.


Private imgScreen As Image


Public Sub PrintScreen()
Dim prnScreen As New Printing.PrintDocument()
AddHandler prnScreen.PrintPage, AddressOf OnPrintScreen

Dim skScreen As SendKeys
skScreen.Send("%{PRTSC}") Send ALT-Print Screen Keystroke

Application.DoEvents() Use DoEvents to avoid incomplete capture
Dim idoClip As IDataObject = Clipboard.GetDataObject() Get Clipboard
Application.DoEvents() Use DoEvents to avoid incomplete capture
imgScreen = idoClip.GetData(DataFormats.Bitmap) Get bitmap copied to clipboard
Application.DoEvents() Use DoEvents to avoid incomplete capture

If Not (imgScreen Is Nothing) Then
prnScreen.Print() Print screen - calls OnPrintScreen through PrintPage event
Else
MsgBox("Screen Capture Error") Using DoEvents above should avoid this
End If
End Sub

Private Sub OnPrintScreen(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
ev.Graphics.DrawImage(imgScreen, 40, 40)
ev.HasMorePages = False
End Sub
 
Ther is an easier way to handle this:

1 - Add a line on code to define the print document at the module level

Private PD withevents as printdocument

2- This will give you the printpage event automatically when you find the PD object in the code window object list

In the "PrintPage" event add the following code

e.graphics.drawimage(pimage,50,50)

3 - then before you call the print method in your click event you should create a new print document:

printDocument1 = new PrintDocument


Here a the code that works for me --------------

Imports System.Drawing.Printing

Public Class mainfrm
Private pimage As Image
Private WithEvents PD As PrintDocument
--------------------------------------------------------------------
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim sk As SendKeys
Dim cdata As IDataObject = Clipboard.GetDataObject()
sk.Send("%{PRTSC}")
pimage = cdata.GetData(DataFormats.Bitmap)

PD = New PrintDocument()
PD.Print()

End Sub

----------------------------------------------------------------
Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
Dim Where As PointF = New PointF(0, 0)
e.Graphics.DrawImage(pimage, Where)
End Sub
 
Have a try playing around with this code, I got it off the net for a project I was working on

Option Explicit On
Option Strict On
**********************************************************************************
PrintForm Class

Purpose: Print active window

Dependencies:

Author:
 
Back
Top