VB.Net/GUI/Screen Capture

Материал из VB Эксперт
Перейти к: навигация, поиск

Screen Capture Demo

<source lang="vbnet"> Imports System.Windows.Forms Imports System.Drawing Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
   Friend WithEvents Button1 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.PictureBox1 = New System.Windows.Forms.PictureBox()
       Me.Button1 = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       Me.PictureBox1.Location = New System.Drawing.Point(288, 16)
       Me.PictureBox1.Size = New System.Drawing.Size(192, 136)
       Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
       "
       Me.Button1.Location = New System.Drawing.Point(32, 40)
       Me.Button1.Size = New System.Drawing.Size(208, 40)
       Me.Button1.Text = "Capture the Screen"
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(496, 166)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox1})
       Me.Text = "Capturing the Screen, Quick and Easy"
       Me.ResumeLayout(False)
   End Sub
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       GetScreenCapture(True).Save("c:\screengrab.bmp")
       PictureBox1.Image = GetScreenCapture()
   End Sub
   Public Function GetScreenCapture( _
       Optional ByVal FullScreen As Boolean = False) As Image
       Dim objSK As SendKeys
       Dim imgCapture As Image
       If FullScreen = True Then
           objSK.SendWait("{PRTSC 2}")
       Else
           objSK.SendWait("%{PRTSC}")
       End If
       Dim objData As IDataObject = Clipboard.GetDataObject()
       Return objData.GetData(DataFormats.Bitmap)
   End Function

End Class


 </source>


Screen snapshot

<source lang="vbnet"> Imports System.Windows.Forms Imports System.Drawing Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       InitializeComponent()
   End Sub
   Friend WithEvents MyPrintDocument As System.Drawing.Printing.PrintDocument
   Friend WithEvents MyPrintDialog As System.Windows.Forms.PrintDialog
   Friend WithEvents MyPictureBox As System.Windows.Forms.PictureBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
   Friend WithEvents MonthCalendar1 As System.Windows.Forms.MonthCalendar
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents Label3 As System.Windows.Forms.Label
   Friend WithEvents MonthCalendar2 As System.Windows.Forms.MonthCalendar
   Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.MyPrintDocument = New System.Drawing.Printing.PrintDocument()
       Me.MyPrintDialog = New System.Windows.Forms.PrintDialog()
       Me.MyPictureBox = New System.Windows.Forms.PictureBox()
       Me.Label1 = New System.Windows.Forms.Label()
       Me.DataGrid1 = New System.Windows.Forms.DataGrid()
       Me.MonthCalendar1 = New System.Windows.Forms.MonthCalendar()
       Me.Label2 = New System.Windows.Forms.Label()
       Me.Label3 = New System.Windows.Forms.Label()
       Me.MonthCalendar2 = New System.Windows.Forms.MonthCalendar()
       Me.LinkLabel1 = New System.Windows.Forms.LinkLabel()
       CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       Me.MyPictureBox.Location = New System.Drawing.Point(296, 16)
       Me.MyPictureBox.Size = New System.Drawing.Size(16, 16)
       Me.MyPictureBox.Visible = False
       "
       Me.Label1.Font = New System.Drawing.Font("Tahoma", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Label1.Location = New System.Drawing.Point(16, 16)
       Me.Label1.Size = New System.Drawing.Size(224, 23)
       Me.Label1.Text = "Report"
       "
       Me.DataGrid1.Font = New System.Drawing.Font("Tahoma", 8.0!)
       Me.DataGrid1.GridLineColor = System.Drawing.Color.RoyalBlue
       Me.DataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue
       Me.DataGrid1.Location = New System.Drawing.Point(16, 48)
       Me.DataGrid1.Size = New System.Drawing.Size(392, 368)
       "
       Me.MonthCalendar1.Location = New System.Drawing.Point(432, 256)
       "
       Me.Label2.Font = New System.Drawing.Font("Tahoma", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Label2.Location = New System.Drawing.Point(432, 224)
       Me.Label2.Size = New System.Drawing.Size(200, 23)
       Me.Label2.Text = "Report:"
       "
       Me.Label3.Font = New System.Drawing.Font("Tahoma", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Label3.Location = New System.Drawing.Point(432, 16)
       Me.Label3.Size = New System.Drawing.Size(200, 23)
       Me.Label3.Text = "Report:"
       "
       Me.MonthCalendar2.Location = New System.Drawing.Point(432, 48)
       "
       Me.LinkLabel1.Location = New System.Drawing.Point(368, 16)
       Me.LinkLabel1.Size = New System.Drawing.Size(32, 16)
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(648, 430)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.LinkLabel1, Me.Label3, Me.MonthCalendar2, Me.Label2, Me.MonthCalendar1, Me.DataGrid1, Me.Label1, Me.MyPictureBox})
       CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
   End Sub
   Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal _
       nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight _
       As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, _
       ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean
   Private bmpScreenshot As Bitmap
   Private Sub MyPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles MyPrintDocument.PrintPage
       Dim objImageToPrint As Graphics = e.Graphics
       objImageToPrint.DrawImage(bmpScreenshot, 0, 0)
       bmpScreenshot.Dispose()
       objImageToPrint.Dispose()
       e.HasMorePages = False
   End Sub
   Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
       LinkLabel1.Visible = False
       Dim objGraphics As Graphics = Me.CreateGraphics
       Dim objSize As Size = Me.Size
       Const SRCCOPY As Integer = &HCC0020
       bmpScreenshot = New Bitmap(objSize.Width, objSize.Height, objGraphics)
       Dim objGraphics2 As Graphics = objGraphics.FromImage(bmpScreenshot)
       Dim deviceContext1 As IntPtr = objGraphics.GetHdc
       Dim deviceContext2 As IntPtr = objGraphics2.GetHdc
       BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, _
           Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
       objGraphics.ReleaseHdc(deviceContext1)
       objGraphics2.ReleaseHdc(deviceContext2)
       MyPrintDialog.Document = MyPrintDocument
       If MyPrintDialog.ShowDialog = DialogResult.OK Then
           MyPrintDocument.Print()
       End If
       LinkLabel1.Visible = True
   End Sub

End Class


 </source>