Solved

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

Posted on 2008-06-19
7
507 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 70

Expert Comment

by:Éric Moreau
ID: 21827540
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

785 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