熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

在VB.NET中進行抓屏

2013-11-15 12:48:06  來源: ASP編程 
Public Class Form
    Inherits SystemWindowsFormsForm
#Region Windows 窗體設計器生成的代碼
    Public Sub New()
        MyBaseNew()
        該調用是 Windows 窗體設計器所必需的
        InitializeComponent()
        在 InitializeComponent() 調用之後添加任何初始化
    End Sub
    窗體重寫處置以清理組件列表
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                componentsDispose()
            End If
        End If
        MyBaseDispose(disposing)
    End Sub
    Windows 窗體設計器所必需的
    Private components As SystemComponentModelIContainer
    注意以下過程是 Windows 窗體設計器所必需的
    可以使用 Windows 窗體設計器修改此過程
    不要使用代碼編輯器修改它
    Friend WithEvents Button As SystemWindowsFormsButton
    Friend WithEvents Timer As SystemWindowsFormsTimer
    Friend WithEvents PictureBox As SystemWindowsFormsPictureBox
    Friend WithEvents Button As SystemWindowsFormsButton
    <SystemDiagnosticsDebuggerStepThrough()> Private Sub InitializeComponent()
        ponents = New SystemComponentModelContainer
        MeButton = New SystemWindowsFormsButton
        MeTimer = New SystemWindowsFormsTimer(ponents)
        MePictureBox = New SystemWindowsFormsPictureBox
        MeButton = New SystemWindowsFormsButton
        MeSuspendLayout()
       
        Button
       
        MeButtonForeColor = SystemDrawingColorBlack
        MeButtonLocation = New SystemDrawingPoint( )
        MeButtonName = Button
        MeButtonSize = New SystemDrawingSize( )
        MeButtonTabIndex =
        MeButtonText = 抓屏
       
        PictureBox
       
        MePictureBoxLocation = New SystemDrawingPoint( )
        MePictureBoxName = PictureBox
        MePictureBoxSize = New SystemDrawingSize( )
        MePictureBoxTabIndex =
        MePictureBoxTabStop = False
       
        Button
       
        MeButtonForeColor = SystemDrawingColorBlack
        MeButtonLocation = New SystemDrawingPoint( )
        MeButtonName = Button
        MeButtonSize = New SystemDrawingSize( )
        MeButtonTabIndex =
        MeButtonText = 保存
       
        Form
       
        MeAutoScaleBaseSize = New SystemDrawingSize( )
        MeBackColor = SystemDrawingColorFromArgb(CType( Byte) CType( Byte) CType( Byte))
        MeClientSize = New SystemDrawingSize( )
        MeControlsAdd(MeButton)
        MeControlsAdd(MePictureBox)
        MeControlsAdd(MeButton)
        MeForeColor = SystemDrawingColorFromArgb(CType( Byte) CType( Byte) CType( Byte))
        MeName = Form
        MeText = wgscd
        MeResumeLayout(False)
    End Sub
#End Region

  VBNET中進行圖象捕獲 需要先引用一些API以下是聲明
    Private Declare Function CreateCompatibleDC Lib GDI (ByVal hDC As Integer) As Integer
    Private Declare Function CreateCompatibleBitmap Lib GDI (ByVal hDC As Integer ByVal nWidth As Integer ByVal nHeight As Integer) As Integer
    Private Declare Function SelectObject Lib GDI (ByVal hDC As Integer ByVal hObject As Integer) As Integer
    Private Declare Function BitBlt Lib GDI (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 GDI (ByVal hDC As Integer) As Integer
    Private Declare Function DeleteObject Lib GDI (ByVal hObj As Integer) As Integer
    Declare Function GetDC Lib user Alias GetDC (ByVal hwnd As Integer) As Integer
    Const SRCCOPY As Integer = &HCC
    將以下代碼添加到Button_Click事件中

  Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
        Dim hDC hMDC As Integer
        Dim hBMP hBMPOld As Integer
        Dim sw sh As Integer
        hDC = GetDC()
        hMDC = CreateCompatibleDC(hDC)
        sw = ScreenPrimaryScreenBoundsWidth
        sh = ScreenPrimaryScreenBoundsHeight
        hBMP = CreateCompatibleBitmap(hDC sw sh)
        hBMPOld = SelectObject(hMDC hBMP)
        BitBlt(hMDC sw sh hDC SRCCOPY)
        hBMP = SelectObject(hMDC hBMPOld)
        PictureBoxImage = ImageFromHbitmap(New IntPtr(hBMP))
        DeleteDC(hDC)
        DeleteDC(hMDC)
        DeleteObject(hBMP)
        MeButtonEnabled = True
    End Sub
    Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
        MeButtonEnabled = False
    End Sub
    Dim ofd As New SaveFileDialog
    Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
        ofdFilter = jpg file|*jpg|bmp file|*bmp
        Dim bmp As Bitmap = MePictureBoxImage
        If ofdShowDialog = DialogResultOK Then
            bmpSave(ofdFileName)
        End If
    End Sub
End Class


From:http://tw.wingwit.com/Article/program/ASP/201311/21658.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.