在VB6和VB.NET中進行圖象捕獲

來源:互聯網
上載者:User

在VB6和VB.NET中進行圖象捕獲

VB6中進行圖象捕獲

 

'----------------------------------------------------------------------------

'

'Author:lihonggen0

'Date:2002-6-19

'功能:抓屏

'----------------------------------------------------------------------------

Private Type POINTAPI

    x As Long

    y As Long

End Type

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

 

Private Sub Command1_Click()

    Dim hdc As Long

    Dim sw As Integer

    Dim sh As Integer

    Dim CurPos As POINTAPI

    Dim Cur As Long

    Me.Hide

    DoEvents

    Picture1.AutoRedraw = True

    hdc = GetDC(0)

    GetCursorPos CurPos

    Cur = GetCursor

    Picture1.Width = Screen.Width

    Picture1.Height = Screen.Height

    sw = Screen.Width / Screen.TwipsPerPixelX

    sh = Screen.Height / Screen.TwipsPerPixelY

    BitBlt Picture1.hdc, 0, 0, sw, sh, hdc, 0, 0, vbSrcCopy

    Me.Show

    DrawIcon Picture1.hdc, CurPos.x - 10, CurPos.y - 10, Cur

    ReleaseDC 0, hdc

    Picture1.AutoRedraw = False

 

End Sub

'參考

'http://support.microsoft.com/?kbid=210108
http://support.microsoft.com/?id=161299


 

VB.NET中進行圖象捕獲 ,需要先引用一些API,以下是聲明:

    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

將以下代碼添加到Button1_Click事件中:

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)

End Sub

 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。