Print Screen

Hi Expert,

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

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.

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:

   if(Clipboard.GetDataObject() != null)
      IDataObject data = Clipboard.GetDataObject();

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


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  It seems that IDataObject is a object. How about "data" and "Image"?  Do I have to preload something to get them working?


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


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

Try this sample...


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)

    End Sub

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.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"
        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"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(290, 268)
        Me.Name = "frmPrintScreenCapture"
        Me.Text = "frmPrintScreenCapture"

    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
        PrintDocument1.DefaultPageSettings.Landscape = True
    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.

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.