Solved

adding a print screen button to a VB .net application not working

Posted on 2008-06-19
7
503 Views
Last Modified: 2013-12-25
I am trying to retreive a FULL screenshot by clicking a button in my vb .net application.  This is the code I am using, but it only captures the current application. Any help?
SendKeys.Send("{PRTSC}")

Open in new window

0
Comment
Question by:npcs
  • 3
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21827516
You can use this to get a full screenshot (or any portion of the screen you want):
    Private Const SRCCOPY As Integer = &HCC0020
 

    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer

    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer

    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer

    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer

    Private Declare Function BitBlt Lib "GDI32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal SrcX As Integer, ByVal SrcY As Integer, ByVal Rop As Integer) As Integer

    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer

    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
 

    Private Sub ScreenShot()

        Dim SR As Rectangle = Screen.GetBounds(New Point(0, 0))

        Dim bmp As Bitmap = GetScreen(SR.Left, SR.Top, SR.Width, SR.Height) ' capture the whole screen
 

        ' ...now do something with "bmp"...
 

        ' two examples of how to save it as a .bmp or a .jpg:

        bmp.Save("c:\someImage.bmp")

        bmp.Save("c:\someImage.jpg", Drawing.Imaging.ImageFormat.Jpeg)

    End Sub
 

    Private Function GetScreen(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap

        Dim hDeskDC As Integer

        Dim hTempDC As Integer

        Dim hBitmap As Integer

        Dim hTempBmp As Integer

        Dim desktopArea As Bitmap = Nothing
 

        hDeskDC = CreateDC("DISPLAY", "", "", "")

        If hDeskDC Then

            hTempDC = CreateCompatibleDC(hDeskDC)

            If hTempDC Then

                hBitmap = CreateCompatibleBitmap(hDeskDC, Width, Height)

                If hBitmap Then

                    hTempBmp = SelectObject(hTempDC, hBitmap)

                    BitBlt(hTempDC, 0, 0, Width, Height, hDeskDC, X, Y, SRCCOPY)

                    desktopArea = Bitmap.FromHbitmap(New IntPtr(hBitmap))

                    DeleteObject(SelectObject(hTempDC, hTempBmp))

                End If

                DeleteDC(hTempDC)

            End If

            DeleteDC(hDeskDC)

        End If

        Return desktopArea

    End Function

Open in new window

0
 

Accepted Solution

by:
npcs earned 0 total points
ID: 21827539
thanks for the reply, but i just talked to a programmer mate of mine and the answer was this:
SendKeys.Send("^{PRTSC}")

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 21827540
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21827578
Your answer of:

    SendKeys.Send("^{PRTSC}")

Did that actually work?!  ...that is Ctrl-PrtSc.

PrtSc is supposed to get the whole screen.
Alt-PrtSc is supposed to get the active window.

?
0
 

Author Comment

by:npcs
ID: 21827679
Yeah, it took a shot of the whole screen.... don't know why plain old prnt scrn didnt work.. but that did the trick :)
0
 

Author Comment

by:npcs
ID: 21827683
this is the source code and how im using it:
   1.

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   2.

       

   3.

              If Not SaveScreen(Application.StartupPath & "\screen.png") Then

   4.

                  MessageBox.Show("Sorry dude, couldn't do it.")

   5.

              End If

   6.

       

   7.

          End Sub

   8.

       

   9.

          Public Function SaveScreen(ByVal theFile As String) As Boolean

  10.

       

  11.

              Try

  12.

                  SendKeys.Send("%{PRTSC}")          '<alt + printscreen>

  13.

                  Application.DoEvents()

  14.

       

  15.

                  Dim data As IDataObject = Clipboard.GetDataObject()

  16.

                 

  17.

                  If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then

  18.

                      Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)

  19.

                      bmp.Save(theFile, Imaging.ImageFormat.Png)

  20.

                  End If

  21.

                  Clipboard.SetDataObject(0)      'save memory by removing the image from the clipboard

  22.

                  Return True

  23.

              Catch ex As Exception

  24.

                  Return False

  25.

              End Try

  26.

       

  27.

          End Function 

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21827698
Yeah...I just tried it and it worked for me also (VB.Net 2005 on WinXP Pro).

 No: SendKeys.Send("{PrtSc}")
 No: SendKeys.Send("%{PrtSc}")
Yes: SendKeys.Send("^{PrtSc}")

Don't know why though...counter-intuitive.

If you need to capture a specific part of the screen then reference my code above...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now