|
-
Apr 23rd, 2004, 12:24 AM
#1
Thread Starter
Hyperactive Member
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.
-
Apr 23rd, 2004, 08:31 AM
#2
Thread Starter
Hyperactive Member
Noone answered so i solved it myself. For you that is interested how, here is my code:
VB Code:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2Cs
Public Function SaveScreen()
Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
Dim iData As IDataObject = Clipboard.GetDataObject()
PictureBox1.Image = iData.GetData(DataFormats.Bitmap)
PictureBox1.Image.Save("c:\test.jpg")
End Function
Test it, works like a acharm...
-
Apr 23rd, 2004, 08:54 AM
#3
Thread Starter
Hyperactive Member
Correction:
VB Code:
Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
Change to
VB Code:
Call keybd_event(System.Windows.Forms.Keys.Snapshot, 1, 0, 0)
-
Apr 23rd, 2004, 10:02 AM
#4
Hyperactive Member
-
Apr 23rd, 2004, 10:18 AM
#5
Fanatic Member
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
-
Apr 23rd, 2004, 10:39 AM
#6
Hyperactive Member
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.
-
Apr 23rd, 2004, 04:01 PM
#7
Frenzied Member
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.
-
Apr 24th, 2004, 02:00 AM
#8
Thread Starter
Hyperactive Member
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...
-
Apr 24th, 2004, 02:25 AM
#9
Thread Starter
Hyperactive Member
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:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2CS
Public Function SaveScreen(ByVal theFile As String) As Boolean
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)
Me.PictureBox1.Image = bmap
Me.PictureBox1.Image.Save(theFile)
ConvertImage(theFile, System.Drawing.Imaging.ImageFormat.Jpeg, "c:\img.jpg")
End If
End Function
Public Sub ConvertImage(ByVal Filename As String, _
ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, _
ByVal NewFilename As String)
Try
Dim imgFile As System.Drawing.Image = _
System.Drawing.Image.FromFile(Filename)
imgFile.Save(NewFilename, DesiredFormat)
Catch ex As Exception
Throw ex
End Try
End Sub
Filesize before converted = 884Kb
Filesize after converted = 146Kb
This rocks!!
-
Apr 24th, 2004, 06:50 AM
#10
Thread Starter
Hyperactive Member
This was even better:
VB Code:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Private Const VK_SNAPSHOT As Short = &H2Cs
Public Function SaveScreen(ByVal theFile As String) As Boolean
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)
Me.PictureBox1.Image = bmap
Me.PictureBox1.Image.Save(theFile, Imaging.ImageFormat.Jpeg)
End If
End Function
Private Sub Command2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Command2.Click
Call keybd_event(System.Windows.Forms.Keys.Snapshot, 0, 0, 0)
System.Threading.Thread.Sleep(200) ' To have time to catch the clipboard
SaveScreen("c:\test.jpg")
End Sub
-
Apr 26th, 2004, 03:09 AM
#11
Fanatic Member
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
-
Apr 26th, 2004, 03:29 AM
#12
Thread Starter
Hyperactive Member
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
-
Mar 18th, 2005, 07:44 AM
#13
Frenzied Member
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!
-
Mar 18th, 2005, 08:11 AM
#14
Re: Screenshot? How?? [Resolved]
If you send the ALT key at the same time, you will only get the form that is in Focus.
ØØ
-
Mar 18th, 2005, 09:05 AM
#15
Re: Screenshot? How?? [Resolved]
This is fully using managed framework code...
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not SaveScreen(Application.StartupPath & "\screen.jpg") Then
MessageBox.Show("Sorry dude, couldn't do it.")
End If
End Sub
Public Function SaveScreen(ByVal theFile As String) As Boolean
Try
SendKeys.Send("%{PRTSC}") '<alt + printscreen>
Application.DoEvents()
Dim data As IDataObject
data = Clipboard.GetDataObject()
Dim bmp As Bitmap
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmp = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
bmp.Save(theFile, Imaging.ImageFormat.Jpeg)
End If
Clipboard.SetDataObject(0) 'save memory by removing the image from the clipboard
Return True
Catch ex As Exception
Return False
End Try
End Function
I don't live here any more.
-
Sep 6th, 2005, 04:31 AM
#16
Frenzied Member
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 
-
Sep 6th, 2005, 06:38 AM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|