Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-19
7
Medium Priority
?
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 86

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 70

Expert Comment

by:Éric Moreau
ID: 21827540
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

609 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