在VB6和VB.NET中进行图象捕获

本文介绍了在VB6和VB.NET中进行图象捕获的方法。在VB6中,通过一系列API函数实现抓屏功能;在VB.NET中,需先引用一些API,然后将特定代码添加到Button1_Click事件中,以完成图象捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VB6VB.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

 

 

 

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值