Solved

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

Posted on 2008-06-19
7
500 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now