VB. NET screenshot capture

Source: Internet
Author: User
'Author: wgscd
'Function: screen capture
'Qq153964481
'Date: 2005-4-12
'*********************************
Public class form1
Inherits system. Windows. Forms. Form
# Region "code generated by Windows Form Designer"
Public sub new ()
Mybase. New ()
'The call is required by the Windows Form Designer.
Initializecomponent ()
'Add any initialization after initializecomponent () is called.
End sub
'Form rewriting disposal to clear the component list.
Protected overloads overrides sub dispose (byval disposing as Boolean)
If disposing then
If not (components is nothing) then
Components. Dispose ()
End if
End if
Mybase. Dispose (disposing)
End sub
'Windows forms designer required
Private components as system. componentmodel. icontainer
'Note: The following procedure is required by the Windows Forms designer
'You can use the Windows Form Designer to modify this process.
'Do not use the code editor to modify it.
Friend withevents button1 as system. Windows. Forms. Button
Friend withevents timer1 as system. Windows. Forms. Timer
Friend withevents picturebox1 as system. Windows. Forms. picturebox
Friend withevents button2 as system. Windows. Forms. Button
<System. Diagnostics. debuggerstepthrough ()> private sub initializecomponent ()
Me. components = new system. componentmodel. Container
Me. button1 = new system. Windows. Forms. Button
Me. timer1 = new system. Windows. Forms. Timer (Me. components)
Me. picturebox1 = new system. Windows. Forms. picturebox
Me. button2 = new system. Windows. Forms. Button
Me. suspendlayout ()
'
'Button1
'
Me. button1.forecolor = system. Drawing. color. Black
Me. button1.location = new system. Drawing. Point (8,312)
Me. button1.name = "button1"
Me. button1.size = new system. Drawing. Size (112, 32)
Me. button1.tabindex = 0
Me. button1.text = "screen capture"
'
'Picturebox1
'
Me. picturebox1.location = new system. Drawing. Point (8, 8)
Me. picturebox1.name = "picturebox1"
Me. picturebox1.size = new system. Drawing. Size (392,288)
Me. picturebox1.tabindex = 4
Me. picturebox1.tabstop = false
'
'Button2
'
Me. button2.forecolor = system. Drawing. color. Black
Me. button2.location = new system. Drawing. Point (264,312)
Me. button2.name = "button2"
Me. button2.size = new system. Drawing. Size (112, 32)
Me. button2.tabindex = 5
Me. button2.text = "save"
'
'Form1
'
Me. autoscalebasesize = new system. Drawing. Size (6, 14)
Me. backcolor = system. Drawing. color. fromargb (ctype (192, byte), ctype (192, byte), ctype (255, byte ))
Me. clientsize = new system. Drawing. Size (408,358)
Me. Controls. Add (Me. button2)
Me. Controls. Add (Me. picturebox1)
Me. Controls. Add (Me. button1)
Me. forecolor = system. Drawing. color. fromargb (ctype (192, byte), ctype (255, byte), ctype (255, byte ))
Me. Name = "form1"
Me. Text = "wgscd"
Me. resumelayout (false)
End sub
# End Region

'Vb. Net needs to reference Some APIs for Image Capture. The following is a declaration:
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 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
Declare function getdc lib "USER32" alias "getdc" (byval hwnd as integer) as integer
Const srccopy as integer = & hcc0020
'Add the following code to the button#click event:

Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim HDC, hmdc as integer
Dim hbmp, hbmpold as integer
Dim SW, SH as integer
HDC = getdc (0)
Hmdc = createcompatibledc (HDC)
Sw = screen. primaryscreen. bounds. Width
SH = screen. primaryscreen. bounds. Height
Hbmp = createcompatiblebitmap (HDC, SW, SH)
Hbmpold = SelectObject (hmdc, hbmp)
Bitblt (hmdc, 0, 0, SW, sh, HDC, 0, 0, srccopy)
Hbmp = SelectObject (hmdc, hbmpold)
Picturebox1.image = image. fromhbitmap (New intptr (hbmp ))
Deletedc (HDC)
Deletedc (hmdc)
Deleteobject (hbmp)
Me. button2.enabled = true
End sub
Private sub form1_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Me. button2.enabled = false
End sub
Dim ofd as new savefiledialog
Private sub button2_click (byval sender as system. Object, byval e as system. eventargs) handles button2.click
Ofd. Filter = "JPG file | *. jpg | BMP file | *. BMP"
Dim BMP as bitmap = me. picturebox1.image
If ofd. showdialog = dialogresult. OK then
BMP. Save (OFD. filename)
End if
End sub
End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.