Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • 2 weeks later...
Posted

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....

  • *Experts*
Posted

[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

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

Posted
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!
Posted

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:

 


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

Posted

no luck

 

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

 

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.

Posted
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!
  • 1 month later...
Guest StehleJason
Posted

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

Guest Ed Simon
Posted

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

  • 7 months later...
Posted

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

' http://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

  • 8 years later...
Posted
@Humble seeker...your code is working okay, i have used it and everything is perfect now. I was searching for this code about two weeks and now i finally got it. I got a lot of other information from this thread as well. Thanks for sharing with us the nice information.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...