Hi all,
how do I define a rectangle of a portion of that sceen and create a bitmap from it?
kevin
Printable View
Hi all,
how do I define a rectangle of a portion of that sceen and create a bitmap from it?
kevin
This code might be helpful to you...
VB Code:
Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (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 xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer Public Function ScreenGrab(ByVal SourceRect As Rectangle) As Bitmap 'returns a screengrab of a known area of the desktop Dim destBMP As Bitmap = New Bitmap(SourceRect.Width, SourceRect.Height) Dim gDest As Graphics = Graphics.FromImage(destBMP) Dim DestHDC As IntPtr = gDest.GetHdc() 'copy the pixel data BitBlt(DestHDC.ToInt32, 0, 0, SourceRect.Width, SourceRect.Height, GetDC(0), SourceRect.Left, SourceRect.Top, &HCC0020) gDest.ReleaseHdc(DestHDC) gDest.Dispose() Return destBMP End Function
and here is a demo of how to use it...
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyRect As Rectangle = New Rectangle(0, Screen.PrimaryScreen.Bounds.Bottom - 100, 100, 100) Dim MyPic As Bitmap 'take the snapshot and store it in a bitmap object MyPic = ScreenGrab(MyRect) 'save it to disc MyPic.Save("C:\TestScreenGrabImage.png", Drawing.Imaging.ImageFormat.Png) MessageBox.Show("Screenshot saved!") End Sub
On my machine I get this image...
cool beans wossy!!! :thumb: I had been working with your BlitterChip class, but couldn't figure it out. Your code does it perfectly!
thanks
Glad to help :D
Blitterchip was intended to be used for copying image data between 2 controls on the same form, rather than what you wanted but now you have mentioned it I'll add this sub to that class and revise the codebank thread :D
well, none really, I just could figure out how to make it do what I needed. I think it is more about not understanding how Bitblt works. It makes a bit more sense now though.
kevin
coolQuote:
Originally Posted by wossname
:thumb:
Done.
http://www.vbforums.com/showthread.p...=1#post2023863
This must be worth a rating :D :lol:
It asolutely is..... I need to spread some around first though. I'll be back.Quote:
Originally Posted by wossname
Thanks again. With your help I was able to taken dozens of screen shot of my app in only a few minutes. Way kick @ss cool I say!