Graphics and Multimedia Topics include: GDI+, drawing 2D/3D, printing and Managed DirectX and the XNA Framework

Go Back  Xtreme .NET Talk > .NET > Graphics and Multimedia > printing screen in vb.net


Reply
 
Thread Tools Display Modes
  #1  
Old 09-11-2002, 10:25 AM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default printing screen in vb.net

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 don't 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
Reply With Quote
  #2  
Old 09-11-2002, 12:05 PM
Thinker's Avatar
Thinker Thinker is offline
Joe Programmer

Preferred language:
VB.NET
 
Join Date: Jul 2001
Location: Fayetteville, Arkansas, USA
Posts: 384
Thinker is on a distinguished road
Default

If you can use APIs, then CL's VB6 example in the Code Library
should be easy enough to convert.
http://www.xtremevbtalk.com/show...threadid=11040
__________________
Posting Guidelines
Reply With Quote
  #3  
Old 09-26-2002, 09:28 AM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default

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 can't 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....
Reply With Quote
  #4  
Old 09-26-2002, 01:25 PM
Bucky's Avatar
Bucky Bucky is offline
Contributor

Preferred language:
C#
 
Join Date: Dec 2001
Location: East Coast
Posts: 791
Bucky is on a distinguished road
Default

GetDC is an API call, which is basically a declare to allow you to use the functions in a Windows DLL. You can use
API's both in VB6 and VB.NET.

Follow the link for more information.
__________________
"Being grown up isn't half as fun as growing up
These are the best days of our lives"
-The Ataris, In This Diary
Reply With Quote
  #5  
Old 09-27-2002, 10:18 AM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default

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 I'm 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 can't 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!
Reply With Quote
  #6  
Old 09-27-2002, 01:49 PM
divil's Avatar
divil divil is offline
Ultimate Contributor
 
Join Date: Nov 2002
Location: England
Posts: 2,747
divil is on a distinguished road
Default

I haven't printed from .NET yet, but presumably there is some way of getting a Graphics object from a PrintDocument. After then, you are correct, you'd use the DrawImage method of the Graphics object.
__________________
MVP, Visual Developer - .NET

Now you see why evil will always triumph - because good is dumb.

My free .NET Windows Forms Controls and Articles
Reply With Quote
  #7  
Old 09-27-2002, 04:00 PM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Angry Getting Frustrated

I'm really having some problems with this printing. I keep reading the same stuff on MS's website and in the VB .NET help, but none of it works, or I'm 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

It's 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
Reply With Quote
  #8  
Old 09-27-2002, 10:19 PM
Derek Stone's Avatar
Derek Stone Derek Stone is offline
Exalted One

Preferred language:
C#
 
Join Date: Nov 2002
Location: Rhode Island, USA
Posts: 1,877
Derek Stone is on a distinguished road
Default

Try getting the data after you printscreen, not beforehand.
__________________
Posting Guidelines
Reply With Quote
  #9  
Old 09-30-2002, 08:56 AM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default

I don't 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.
Reply With Quote
  #10  
Old 09-30-2002, 02:18 PM
Derek Stone's Avatar
Derek Stone Derek Stone is offline
Exalted One

Preferred language:
C#
 
Join Date: Nov 2002
Location: Rhode Island, USA
Posts: 1,877
Derek Stone is on a distinguished road
Default

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
__________________
Posting Guidelines
Reply With Quote
  #11  
Old 09-30-2002, 03:51 PM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default no luck

Still no luck. I'm 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.
Reply With Quote
  #12  
Old 09-30-2002, 06:18 PM
Derek Stone's Avatar
Derek Stone Derek Stone is offline
Exalted One

Preferred language:
C#
 
Join Date: Nov 2002
Location: Rhode Island, USA
Posts: 1,877
Derek Stone is on a distinguished road
Default

Have you declared a PrintDocument object?
__________________
Posting Guidelines
Reply With Quote
  #13  
Old 10-04-2002, 01:45 PM
shootsnlad shootsnlad is offline
Freshman
 
Join Date: Feb 2002
Posts: 46
shootsnlad is on a distinguished road
Default

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 don't have any option at all the dim a printdocument. Any other ideas? Thanks!
Reply With Quote
  #14  
Old 11-05-2002, 08:39 AM
StehleJason
Guest
 
Posts: n/a
Default 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
Reply With Quote
  #15  
Old 11-05-2002, 09:35 AM
Ed Simon
Guest
 
Posts: n/a
Default

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
Reply With Quote
  #16  
Old 06-18-2003, 10:53 AM
Humble Seeker Humble Seeker is offline
Newcomer
 
Join Date: Jun 2003
Posts: 10
Humble Seeker is on a distinguished road
Default

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: Øyvind Rustan - Compositae, 27.05.2002
' www.compositae.no
' Revision:
'
'********************************************************************* *************

Imports System.Windows
Imports System.Drawing

Public Class PrintForm
'------------------------------------------------------------------------------
' New
' Constructor
'------------------------------------------------------------------------------
Public Sub New()
End Sub

'------------------------------------------------------------------------------
' Print
' Print active window (form)
'------------------------------------------------------------------------------
Public Sub Print()
Try
'-- Keys Alt+PrintScreen copies active window to clipboard
Forms.SendKeys.SendWait("%{PRTSC}")
'-- Create print document and printing handler
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
'-- Setup document
pd.DocumentName = "PrintScreen"
With pd.DefaultPageSettings
.Landscape = False
'-- Reduce margins to accomodate larger pictures
.Margins.Left -= .Margins.Left \ 3
.Margins.Top -= .Margins.Top \ 3
.Margins.Right -= .Margins.Right \ 3
.Margins.Bottom -= .Margins.Bottom \ 3
End With
'-- Print document
pd.Print()
Catch ex As Exception
Throw ex
End Try
End Sub

'------------------------------------------------------------------------------
' pd_PrintPage
' PrintPage handler
'------------------------------------------------------------------------------
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
Try
'-- Get PrintScreen bitmap from clipboard
Dim iData As IDataObject = Clipboard.GetDataObject()
If iData.GetDataPresent(DataFormats.Bitmap) Then
Dim bm As Bitmap = CType(iData.GetData(DataFormats.Bitmap), Bitmap)
'-- Set size of destination rectangle (printer)
Dim rDest As New RectangleF(ev.MarginBounds.Left, ev.MarginBounds.Top, 0, 0)
If bm.Width < ev.MarginBounds.Width Then
'-- Set to bitmap size if bitmap is smaller than paper
rDest.Width = bm.Width
rDest.Height = bm.Height
Else
'-- Set to paper size if bitmap is larger than paper
'-- Scale height to retain proportions
rDest.Width = ev.MarginBounds.Width
rDest.Height = CType(rDest.Width * (bm.Height / bm.Width), Single)
End If
'-- Draw bitmap
ev.Graphics.DrawImage(bm, rDest, bm.GetBounds(GraphicsUnit.Pixel), GraphicsUnit.Pixel)
'-- Draw timestamp
Dim printFont As Font = New Font("Arial", 10, FontStyle.Regular)
ev.Graphics.DrawString(Now.ToString, printFont, Brushes.Black, rDest.Left, rDest.Top + rDest.Height + printFont.GetHeight(ev.Graphics))
Else
'-- No bitmap, cancel printing
ev.Cancel = True
End If
Catch ex As Exception
'-- Cancel printing
ev.Cancel = True
Throw ex
Finally
'-- Print only on page
ev.HasMorePages = False
End Try
End Sub
End Class
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get www site ( Screen Scrapes ) faster??? -= .NET C# / VB.NET =- gicio Network 1 11-01-2004 07:49 PM
Important!! Printing Whats On The Screen Incognetosoft Windows Forms 8 07-31-2003 11:52 AM
Capture the Screen in VB.NET afrinspray Graphics and Multimedia 1 06-18-2003 10:54 AM

Advertisement:

Powered by liquidweb