• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

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

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
npcs
Asked:
npcs
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
npcsAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
npcsAuthor Commented:
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
 
npcsAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now