Results 1 to 17 of 17

Thread: Screenshot? How?? [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460

    Screenshot? How?? [Resolved]

    I have searched through a tons of forums and other sites, but i cannot find any good code to make a screenshot and then save it as a *.jpg file. I know you can convert *.bmp to other formats quite easally with .net. Please, point me in the right direction!
    Last edited by Libero; Apr 23rd, 2004 at 08:32 AM.

  2. #2

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460

    Noone answered so i solved it myself. For you that is interested how, here is my code:
    VB Code:
    1. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    2.     Private Const VK_SNAPSHOT As Short = &H2Cs
    3.  
    4.  
    5. Public Function SaveScreen()
    6.         Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
    7.         Dim iData As IDataObject = Clipboard.GetDataObject()
    8.         PictureBox1.Image = iData.GetData(DataFormats.Bitmap)
    9.         PictureBox1.Image.Save("c:\test.jpg")
    10.     End Function
    Test it, works like a acharm...

  3. #3

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    Correction:
    VB Code:
    1. Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
    Change to
    VB Code:
    1. Call keybd_event(System.Windows.Forms.Keys.Snapshot, 1, 0, 0)

  4. #4
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Very Cool. Thanks.

  5. #5
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    u sure that saves as jpg? It looks like its saving as a bitmap to me with a false file extentsion...
    check the file size and check if its bmp or jpg, because i dont see anything (other than the filename) that could indicate any conversation from bitmap to jpeg. would be interesting to know.

    edit:
    PictureBox1.Image = iData.GetData(DataFormats.Bitmap)
    i may sound dumb, but seriously, that has to be a bitmap being saved here

  6. #6
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    LITHIA I think you are right about that. When I opened the image with iFranView it claimed it was a png file with the wrong extension.

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    It's working on my system...a 1024X768 bitmap is 2.25 MB!

    The test.jpg is only 52.7 KB and opens up fine as a JPG.

  8. #8

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    wey97 is correct. The size depends on if you got alot of color or not in your screenshot. Lets say that you take a screenie with this forum open ( maxixmized ) then you got got a much much smaller bitmap the if you take a screenie where you got a lot of colors...

  9. #9

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    Stupid stupid me LITHIA was right, "It looks like its saving as a bitmap to me with a false file extentsion". I have worked on a real solution and came up with this:

    VB Code:
    1. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    2.     Private Const VK_SNAPSHOT As Short = &H2CS
    3.    
    4.    
    5.     Public Function SaveScreen(ByVal theFile As String) As Boolean
    6.         Dim data As IDataObject
    7.         data = Clipboard.GetDataObject()
    8.         Dim bmap As Bitmap
    9.         If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    10.             bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
    11.             Me.PictureBox1.Image = bmap
    12.             Me.PictureBox1.Image.Save(theFile)
    13.             ConvertImage(theFile, System.Drawing.Imaging.ImageFormat.Jpeg, "c:\img.jpg")
    14.         End If
    15.     End Function
    16.  
    17.     Public Sub ConvertImage(ByVal Filename As String, _
    18.       ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, _
    19.       ByVal NewFilename As String)
    20.         Try
    21.             Dim imgFile As System.Drawing.Image = _
    22.               System.Drawing.Image.FromFile(Filename)
    23.             imgFile.Save(NewFilename, DesiredFormat)
    24.         Catch ex As Exception
    25.             Throw ex
    26.         End Try
    27.     End Sub

    Filesize before converted = 884Kb
    Filesize after converted = 146Kb

    This rocks!!


  10. #10

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    This was even better:
    VB Code:
    1. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
    2.     Private Const VK_SNAPSHOT As Short = &H2Cs
    3.    
    4.    
    5.     Public Function SaveScreen(ByVal theFile As String) As Boolean
    6.         Dim data As IDataObject
    7.         data = Clipboard.GetDataObject()
    8.         Dim bmap As Bitmap
    9.         If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    10.             bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
    11.             Me.PictureBox1.Image = bmap
    12.             Me.PictureBox1.Image.Save(theFile, Imaging.ImageFormat.Jpeg)
    13.         End If
    14.     End Function
    15.  
    16.     Private Sub Command2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command2.Click
    17.         Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
    18.         System.Threading.Thread.Sleep(200) ' To have time to catch the clipboard
    19.         SaveScreen("c:\test.jpg")
    20.     End Sub

  11. #11
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    nice, i had a problem like this in VB6 - had no idea how to convert to jpeg, looks a lot easier with .NET tho, thank goodness lol.

    good work

  12. #12

    Thread Starter
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460
    Thx! LITHIA> You´re right. I have fiddled around about one and a half week with VB.NET now, and damn its so much more powerful than VB6. Ooohhh! Im in love with .NET

  13. #13
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Screenshot? How?? [Resolved]

    Wow ...cool

    thanks for this thread.

    Two Questions:

    1. Must you have an picturebox in order for this to work?
    2. Can we adapt this so that it can only capture the form which called it?

    Cheers!

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Screenshot? How?? [Resolved]

    If you send the ALT key at the same time, you will only get the form that is in Focus.


    ØØ

  15. #15
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Screenshot? How?? [Resolved]

    This is fully using managed framework code...
    VB Code:
    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.jpg") 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
    16.             data = Clipboard.GetDataObject()
    17.             Dim bmp As Bitmap
    18.             If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    19.                 bmp = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
    20.                 bmp.Save(theFile, Imaging.ImageFormat.Jpeg)
    21.             End If
    22.             Clipboard.SetDataObject(0)        'save memory by removing the image from the clipboard
    23.             Return True
    24.         Catch ex As Exception
    25.             Return False
    26.         End Try
    27.  
    28.     End Function
    I don't live here any more.

  16. #16
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Screenshot? How?? [Resolved]

    Sorry to dig up an old thread, but is it possible to get a screenshot of a MDI Child form? instead of the whole form?
    If you find my thread helpful, please remember to rate me

  17. #17
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Screenshot? How?? [Resolved]

    I posted a thread about Blitting in the codebank a few months ago, the class therein contains a screengrab() function that you can pass the rectangle of yuor form into.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width