Copy of Screen

Good day to you.

I don't know much about the ClipBoard and GetData and so forth but there seems to me that 2005 Express is not quite the same as .NET 2003. The code that I shall give in a moment gave me a full screen copy on the Form_Load event and gave a copy of the Form on a button_click event. Now, when using the code in 2005 Express the Load event only gives a copy of part of the screen. Why is this happening Is there a way in which I can rectify matters. I would be very pleased for any info. I have tried with Imports and Inherits and Paint. It is the sizing I believe.

Private Sub frmScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SendKeys.SendWait("{PRTSC}")

Dim data As IDataObject

data = Clipboard.GetDataObject()

Dim bmap As Bitmap

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

bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)

bmap.Save("C:\SeePrint.bmp")

bmap.Dispose()

bmap = Nothing

MsgBox("Copy Taken!")

End If

End Sub

Thanks for any help. AliBong.




Answer this question

Copy of Screen

  • Jason N

    Hello nobugz, I did write a mail in answer to yours but not only was I disconnected but I had error message of sorts.

    Those links you gave look good and I have been doing something with BitBlt.

    But thankyou for your interest and help. Have a good Weekend.



  • Vani M

    Hi everyone!

    Thanks a lot for your code weirdbeardmt.
    It was a big help for me! (I'm writing a program something like remote desktop and this way for capture the screen was a big help for me...)

    Anyway I have two little questions:

    1.: Is it possible to capture the notification window (You know that comics like bubble...)
    2.: Do you have a good link about GDI functions (I mean a table like list, or something like this...)

    ps: Thank a lot for the code again! I love it!!! :)


  • SterlingH

    Hello Alibong!

    I use little changed programm code for screen capture after each 5 seconds:

    Public Sub CaptureScreen()

    Dim hSDC, hMDC As Integer
    Dim hBMP, hBMPOld As Integer
    Dim r As Integer
    Try
    hSDC = CreateDC("DISPLAY", "", "", "") 'Get the screen content
    hMDC = CreateCompatibleDC(hSDC) 'Get image from memory

    FW = GetDeviceCaps(hSDC, 8)
    FH = GetDeviceCaps(hSDC, 10)
    hBMP = CreateCompatibleBitmap(hSDC, FW, FH)

    hBMPOld = SelectObject(hMDC, hBMP)
    r = BitBlt(hMDC, 0, 0, FH, FW, hSDC, 0, 0, 13369376)
    hBMP = SelectObject(hMDC, hBMPOld)

    r = DeleteDC(hSDC)
    r = DeleteDC(hMDC)

    Dim FileName As String
    Dim FolderName As String
    Dim FilePath As String
    FileName = Date.Now.ToString("ddMMyyyyhhmmss") & ".png"
    FolderName = Date.Now.ToString("ddMMyyy") & "\"
    FilePath = "C:\Program Files\Screencap\"
    System.IO.Directory.CreateDirectory(FilePath & FolderName)

    oBackground = Image.FromHbitmap(New IntPtr(hBMP))
    oBackground.Save(FilePath & FolderName & FileName, Imaging.ImageFormat.Png)
    oBackground.Dispose()

    DeleteObject(hBMPOld)
    DeleteObject(hBMP)
    DeleteObject(hSDC)
    DeleteObject(hMDC)
    DeleteObject(FW)
    DeleteObject(FH)
    Threading.Thread.Sleep(5000)
    Me.CaptureScreen()
    Catch ex As Exception
    MsgBox(ex.Message & vbCrLf & vbCrLf & vbCrLf & ex.StackTrace)
    End Try
    End Sub



  • hohsen

    Hello.. GDI+ Those links given are good! If you can get hold of this book, "Programming in MS VB.Net" by Francesco Balena (ISBN: 0-7356-1375-3) I think you'd like it. His treatment of the subject will make you love it too! There are about 20 short projects which are easily to follow. I have built each one and use them as reference when needed.

    Thing is though! It is expensive. Borrow it from a library sevice. Good Luck. A.



  • Sianspheric

    Check this thread for a simple way to make Print Screen work.


  • hawash

    Good Day to you weirdbeardmt. Thanks for your reply. I have taken a copy of your code. With my learning (I'm not at college) I have a set-back in that I am still lost as to where to put code (ie In events, etc). But once I get those sort of things I am quick to pick up the logic and so on.

    I shall endeavour to sort this out. But I still have that feeling that the code I put in originally is OK and the trouble lies elsewhere.

    But, to iterate, "endeavour" I shall, AND thanks a lot.



  • briggins5

    Hi alibong,

    Sorry for the errors.

    1) Remove the End If from the first block. Note you also need to add that code to either a function or to an event handler somewhere.
    2) Could you tell me what error you get using CaptureScreen (i.e., what is the wiggly line ). At a guess I'd say you need to add a reference to system.drawing but without knowing the error it's hard to say.

    Thanks.


  • John05

    Hello weirdbeardmt. I wrote all the code as you put but two things wasn't liked by our mutual friend - Vb - both in first part.

    CaptureScreen was 'wiggle-lined' and of course, End If. (No "If")

    I would be pleased if you could put me right. Cheers.



  • ycjj

    1. I'm not sure about the notification window. I don't see why it shouldn't get captured (but of course it will be the whole screen rather than say just the bubble).

    2. Couple of things here:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/gdicpp/GDIPlus/aboutGDIPlus.asp

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnanchor/html/gdi.asp

    Cheers.



  • killerless

    Hello nobugz, thanks for your interest. It is near bed-time but a quick look at that doc page you gave gives me something to look at on the morrow - and that other link. Some many months ago I got hold of that code I put in when I used .Net 2003 and using it on a Load gave me a whole screen shot - I can't swear by it though. On a button click event it gave a shot of just the Form. Just lately I have been juggling around with some code incorporating sWidth and sHeight ; Dim screen As screen and Upper bounds and had some other effects as part screen and part form. That is a bit beyond my comprehension - (and so's that word) at the moment and I have been wondering if a delve into graphics with Rectangle would do.

    Anyhow, cheers for your concern. Have a good WeekEnd. AliBong.



  • hemo

    Try this using GDI:

     

    Dim scrn As Bitmap

    Try

    scrn = CaptureScreen()

    scrn.Save("test.png")

    End If

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

     

    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 GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex 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 srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer

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

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

    Const SRCCOPY As Integer = &HCC0020

    Public Function CaptureScreen() As Bitmap

    Dim FW, FH As Integer

    Dim hSDC, hMDC As Integer

    Dim hBMP, hBMPOld As Integer

    Dim r As Integer

    hSDC = CreateDC("DISPLAY", "", "", "")

    hMDC = CreateCompatibleDC(hSDC)

    FW = GetDeviceCaps(hSDC, 8)

    FH = GetDeviceCaps(hSDC, 10)

    hBMP = CreateCompatibleBitmap(hSDC, FW, FH)

    hBMPOld = SelectObject(hMDC, hBMP)

    r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, 13369376)

    hBMP = SelectObject(hMDC, hBMPOld)

    r = DeleteDC(hSDC)

    r = DeleteDC(hMDC)

    CaptureScreen = Image.FromHbitmap(New IntPtr(hBMP))

    DeleteObject(hBMP)

    End Function



  • Dave21

    Interesting problem, I wrestled with it for a while. SendKeys.SendWait({PRTSC}) puts the image of the current window on the clipboard, not the desktop. In other words, it behaves like SendKeys.SendWait("%{PRTSC}). That makes the clipboard image weird when you do it in the form's Load event. At that point, the form hasn't been displayed yet and the underlying window still has a 300x300 size. The image you'll get is therefore a 300x300 screenshot of the desktop area where the form eventually will be displayed.

    I tried all kinds of tricks to coax it into making a snapshot of the entire desktop, no luck. Then I saw the note on this documentation page: "PRINT SCREEN, {PRTSC} (reserved for future use)". MSFT speak for: "we couldn't make it work, maybe someday we will".

    You'll have to do it old-school, see this project for example.


  • Copy of Screen