永发信息网

为什么我用BitBlt截指定区域,大小的图片后,截取的图片数据是空的?

答案:2  悬赏:60  手机版
解决时间 2021-03-14 22:18
  • 提问者网友:山高云阔
  • 2021-03-14 13:28
如题,我用VB写的完整代码如下:
Option Explicit
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 RasterOpConstants) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Dim hDC2 As Long
Dim ctCi As Long
Private Const SRCCOPY As Long = &HCC0020

Private Sub Command1_Click()

Dim W As Long, H As Long, i As Long
Picture1.Width = 13335
Picture1.Height = 5655
Picture1.Visible = True
Picture1.AutoRedraw = True
Picture1.ScaleMode = 3
Picture1.Cls
hDC2 = Picture1.hdc
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then Command1.Caption = "暂停截屏" Else Command1.Caption = "开始截屏"
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 3000 '每隔 3000 毫秒(3秒)保存一次
Me.Caption = "自动定时截屏"
Command1.Caption = "开始截屏"
End Sub

Private Sub Timer1_Timer()
Dim DC As Long
Dim dl As Long
Dim nPath As String
Dim nName As String
Dim pl As Long
Dim hwnd As Long
nPath = "e:\MyPic"
If Dir(nPath, 23) = "" Then MkDir nPath

DC = GetWindowDC(0)

dl = BitBlt(hDC2, 0, 0, 900, 370, 0, 160, DC, vbSrcCopy)
ctCi = ctCi + 1
nName = ctCi
dl = 5 - Len(nName)
If dl > 0 Then nName = String(dl, "0") & nName
SavePicture Picture1.Image, nPath & "\P-" & nName & ".bmp" '***** P- 表示文件前缀
ReleaseDC 0, DC
End Sub
截下来的数据是空的,可以肯定的是问题出在BitBlt里,它用来截取全屏没问题,截指定区域,指定大小的图片时,就是空数据了,请教高手~~
最佳答案
  • 五星知识达人网友:傲气稳了全场
  • 2021-03-14 14:43
你可能不小心把这句写错了
dl = BitBlt(hDC2, 0, 0, 900, 370, 0, 160, DC, vbSrcCopy)
参数DC顺序不对,应写成:
dl = BitBlt(hDC2, 0, 0, 900, 370, DC, 0, 160, vbSrcCopy)
这样改了就可以了,试试吧!
全部回答
  • 1楼网友:罪歌
  • 2021-03-14 15:05
同问。。。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯