Print Screen

Hi Expert,

How do I print a screen to a printer by a button click?  The sreeen is a VB.net application's front panel.  I know "SendKeys' class.  But sendkeys only copy and paste sreeen to clipboard.
Thanks in advance for your help.

Heartland
heartlandAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaHaCommented:
You know about SendKeys... throw on a read from the clipboard and you are all set. In a recent application my TakeScreenShot function looked not unlike:

   SendKeys.SendWait("%{PRTSC}");
           
   if(Clipboard.GetDataObject() != null)
   {
      IDataObject data = Clipboard.GetDataObject();

      if (data.GetDataPresent(DataFormats.Bitmap))
      {
         Image image = (Image)data.GetData(DataFormats.Bitmap,true);

         image.Save(filename,
            System.Drawing.Imaging.ImageFormat.Jpeg);
      }
   }

With this code, I cause my app to hit the alt-print screen keystroke, wait for the message to be processed and then make sure that it is image data within the clipboard, if it is, I save it to disk.

This is probably the easiest way for you to do so within code. You do run the risk of having something else end up in the clipboard between your check of for image data and your saving of it, however it is far easier than some of the alternatives (like some Win32 calls to get the same data).
heartlandAuthor Commented:
Hi DaHa,

Thank you for your code.  However, I am having the difficult to understand your code.  Is it in C?  How do I convert it to VB.net?  It seems that IDataObject is a object. How about "data" and "Image"?  Do I have to preload something to get them working?

Thanks.

Heartland
DaHaCommented:
I do apologize, it seems that I forgot what section I was in... here it is again, but in VB.NET:

   SendKeys.SendWait("%{PRTSC}")

   If Not (Clipboard.GetDataObject Is Nothing) Then
      Dim data As IDataObject = Clipboard.GetDataObject

      If data.GetDataPresent(DataFormats.Bitmap) Then
         Dim image As Image = CType(data.GetData(DataFormats.Bitmap, True), Image)
         image.Save(filename, _
            System.Drawing.Imaging.ImageFormat.Jpeg)
      End If
   End If
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

heartlandAuthor Commented:
Hi DaHa,

I tried the code and it did save a Jpeg file with VB front panel.  However, I do not waant to save file, I want to print a hard copy instead.   Thanks again.

heartland
planoczCommented:
Try this sample...


'FORM 1

Imports System.IO
Imports System.Drawing.Printing
Public Class frmPrintScreenCapture
    Inherits System.Windows.Forms.Form
    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, 0, 0, 13369376)
        mygraphics.ReleaseHdc(dc1)
        memoryGraphics.ReleaseHdc(dc2)

    End Sub

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(120, 224)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(140, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Print Screen"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(12, 36)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(244, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Print Screen this form in Landscape"
        '
        'frmPrintScreenCapture
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(290, 268)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "frmPrintScreenCapture"
        Me.Text = "frmPrintScreenCapture"
        Me.ResumeLayout(False)

    End Sub

#End Region
    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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CaptureScreen()
        PrintDocument1.DefaultPageSettings.Landscape = True
        PrintDocument1.Print()
    End Sub
End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heartlandAuthor Commented:
Hi Planocz,

The code works just like what I want.  Thanks very much for your help.

heartland
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.