Hold the screen in the vb.net

Source: Internet
Author: User
Tags integer modify
' AUTHOR:WGSCD
' Function: Grab screen
' QQ153964481
' Date:2005-4-12
’*********************************
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Form overrides disposition to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
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 = "Grab Screen"

' 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 (in), CType (in the same, 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 (in), CType (255, byte), CType (255, byte)
Me.Name = "Form1"
Me.Text = "WGSCD"
Me.resumelayout (False)
End Sub
#End Region

' VB. NET to capture the image, you need to refer to some APIs, the following is the 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 Button1_Click event:

Private Sub button1_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.