利用VB捕捉并保存屏幕图象 大家知道在VB下利用API函数Bitblt可以将屏幕或者窗口上的图象拷贝到VB中的PictureBox对象中,但是如果简单的利用PictureBox的SavePicture函数来保存图象,会发现什么也保存不了。这篇文章就是介绍如何捕获并利用Windows下的OLE API函数保存图象。 首先来看源程序,首先建立一个新的工程文件,然后在Form1中加入5个CommandButton对象和一个PictureBox对象,然后在Form1中加入以下代码:Option ExplicitOption Base 0
Private Type PALETTEENTRY peRed As Byte peGreen As Byte peBlue As Byte peFlags As ByteEnd Type
Private Type LOGPALETTE palVersion As Integer palNumEntries As Integer palPalEntry(255) As PALETTEENTRYEnd Type
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As ByteEnd Type
Private Const RASTERCAPS As Long = 38Private Const RC_PALETTE As Long = &H100Private Const SIZEPALETTE As Long = 104
Private Type RECT Left As Long Top As Long Right As Long Bottom As LongEnd Type
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, _ ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal _ iCapabilitiy As Long) As LongPrivate Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, _ ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries _ As PALETTEENTRY) As LongPrivate Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) _ As LongPrivate Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject _ As Long) As LongPrivate Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, ByVal XDest As _ Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, _ ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop _ As Long) As LongPrivate Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function GetForegroundWindow Lib "USER32" () As LongPrivate Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette _ As Long, ByVal bForceBackground As Long) As LongPrivate Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As LongPrivate Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As LongPrivate Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As _ RECT) As LongPrivate Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As _ Long) As LongPrivate Declare Function GetDesktopWindow Lib "USER32" () As Long
Private Type PicBmp Size As Long Type As Long hBmp As Long hPal As Long Reserved As LongEnd Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As _ PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'捕捉整个屏幕Private Sub Command1_Click() Set Picture1.Picture = CaptureScreen()End Sub
'在两秒钟后捕捉当前的活动窗口Private Sub Command2_Click() MsgBox "当你关闭这个对话框两秒钟之后程序会捕捉处于活动状态的窗口." '等待两秒钟 Dim EndTime As Date EndTime = DateAdd("s", 2, Now) Do Until Now > EndTime DoEvents Loop Set Picture1.Picture = CaptureActiveWindow() Me.SetFocusEnd Sub
Private Sub Command3_Click() Set Picture1.Picture = NothingEnd Sub
Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture Dim r As Long
Dim Pic As PicBmp Dim IPic As IPicture Dim IID_IDispatch As GUID
'填充IDispatch界面 With IID_IDispatch .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With
'填充Pic With Pic .Size = Len(Pic) ' Pic结构长度 .Type = vbPicTypeBitmap ' 图象类型 .hBmp = hBmp ' 位图句柄 .hPal = hPal ' 调色板句柄 End With
'建立Picture图象 r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
'返回Picture对象 Set CreateBitmapPicture = IPicEnd Function
Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal _ LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc _ As Long) As Picture
Dim hDCMemory As Long Dim hBmp As Long Dim hBmpPrev As Long Dim r As Long Dim hDCSrc As Long Dim hPal As Long Dim hPalPrev As Long Dim RasterCapsScrn As Long Dim HasPaletteScrn As Long Dim PaletteSizeScrn As Long Dim LogPal As LOGPALETTE
If Client Then hDCSrc = GetDC(hWndSrc) Else hDCSrc = GetWindowDC(hWndSrc) End If
hDCMemory = CreateCompatibleDC(hDCSrc) hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc) hBmpPrev = SelectObject(hDCMemory, hBmp)
'获得屏幕属性 RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) HasPaletteScrn = RasterCapsScrn And RC_PALETTE PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
Private Type PALETTEENTRY peRed As Byte peGreen As Byte peBlue As Byte peFlags As ByteEnd Type
Private Type LOGPALETTE palVersion As Integer palNumEntries As Integer palPalEntry(255) As PALETTEENTRYEnd Type
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As ByteEnd Type
Private Const RASTERCAPS As Long = 38Private Const RC_PALETTE As Long = &H100Private Const SIZEPALETTE As Long = 104
Private Type RECT Left As Long Top As Long Right As Long Bottom As LongEnd Type
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, _ ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal _ iCapabilitiy As Long) As LongPrivate Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, _ ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries _ As PALETTEENTRY) As LongPrivate Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) _ As LongPrivate Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject _ As Long) As LongPrivate Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, ByVal XDest As _ Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, _ ByVal hDCSrc As Long, ByVal XSrc As Long, ByVal YSrc As Long, ByVal dwRop _ As Long) As LongPrivate Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function GetForegroundWindow Lib "USER32" () As LongPrivate Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette _ As Long, ByVal bForceBackground As Long) As LongPrivate Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As LongPrivate Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As LongPrivate Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As LongPrivate Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As _ RECT) As LongPrivate Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As _ Long) As LongPrivate Declare Function GetDesktopWindow Lib "USER32" () As Long
Private Type PicBmp Size As Long Type As Long hBmp As Long hPal As Long Reserved As LongEnd Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As _ PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'捕捉整个屏幕Private Sub Command1_Click() Set Picture1.Picture = CaptureScreen()End Sub
'在两秒钟后捕捉当前的活动窗口Private Sub Command2_Click() MsgBox "当你关闭这个对话框两秒钟之后程序会捕捉处于活动状态的窗口." '等待两秒钟 Dim EndTime As Date EndTime = DateAdd("s", 2, Now) Do Until Now > EndTime DoEvents Loop Set Picture1.Picture = CaptureActiveWindow() Me.SetFocusEnd Sub
Private Sub Command3_Click() Set Picture1.Picture = NothingEnd Sub
Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture Dim r As Long
Dim Pic As PicBmp Dim IPic As IPicture Dim IID_IDispatch As GUID
'填充IDispatch界面 With IID_IDispatch .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With
'填充Pic With Pic .Size = Len(Pic) ' Pic结构长度 .Type = vbPicTypeBitmap ' 图象类型 .hBmp = hBmp ' 位图句柄 .hPal = hPal ' 调色板句柄 End With
'建立Picture图象 r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
'返回Picture对象 Set CreateBitmapPicture = IPicEnd Function
Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal _ LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc _ As Long) As Picture
Dim hDCMemory As Long Dim hBmp As Long Dim hBmpPrev As Long Dim r As Long Dim hDCSrc As Long Dim hPal As Long Dim hPalPrev As Long Dim RasterCapsScrn As Long Dim HasPaletteScrn As Long Dim PaletteSizeScrn As Long Dim LogPal As LOGPALETTE
If Client Then hDCSrc = GetDC(hWndSrc) Else hDCSrc = GetWindowDC(hWndSrc) End If
hDCMemory = CreateCompatibleDC(hDCSrc) hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc) hBmpPrev = SelectObject(hDCMemory, hBmp)
'获得屏幕属性 RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) HasPaletteScrn = RasterCapsScrn And RC_PALETTE PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
'拷贝图象 r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
hBmp = SelectObject(hDCMemory, hBmpPrev)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then hPal = SelectPalette(hDCMemory, hPalPrev, 0) End If
'释放资源 r = DeleteDC(hDCMemory) r = ReleaseDC(hWndSrc, hDCSrc)
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)End Function'capturescreen函数捕捉整个屏幕图象Public Function CaptureScreen() As Picture Dim hWndScreen As Long
'获得桌面的窗口句柄 hWndScreen = GetDesktopWindow() Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width _ \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY)End Function
Public Function CaptureActiveWindow() As Picture Dim hWndActive As Long Dim r As Long Dim RectActive As RECT hWndActive = GetForegroundWindow() r = GetWindowRect(hWndActive, RectActive) Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, _ RectActive.Right - RectActive.Left, RectActive.Bottom - RectActive.Top)End Function
Public Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture) Const vbHiMetric As Integer = 8 Dim PicRatio As Double Dim PrnWidth As Double Dim PrnHeight As Double Dim PrnRatio As Double Dim PrnPicWidth As Double Dim PrnPicHeight As Double If Pic.Height >= Pic.Width Then Prn.Orientation = vbPRORPortrait Else Prn.Orientation = vbPRORLandscape End If PicRatio = Pic.Width / Pic.Height PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric) PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric) PrnRatio = PrnWidth / PrnHeight If PicRatio >= PrnRatio Then PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode) PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode) Else PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode) PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode) End If Prn.PaintPicture Pic, 0, 0, PrnPicWidth, PrnPicHeightEnd Sub
Private Sub Command4_Click() CommonDialog1.DefaultExt = ".BMP" CommonDialog1.Filter = "Bitmap Image (*.bmp)|*.bmp" CommonDialog1.ShowSave If CommonDialog1.FileName <> "" Then SavePicture Picture1.Picture, CommonDialog1.FileName End IfEnd Sub
Private Sub Command5_Click() PrintPictureToFitPage Printer, Picture1.Picture Printer.EndDocEnd Sub
Private Sub Form_Load() Command1.Caption = "捕捉整个屏幕" Command2.Caption = "两秒钟后捕捉活动窗口" Command3.Caption = "清除图象" Command4.Caption = "保存图象" Command5.Caption = "打印图象"End Sub
运行程序,点击command1或者Command2就可以捕捉成个屏幕或者窗口到Picture1中,然后点击Command4或者Command5就可以保存或打印图象。 上面的程序中最重要的是CaptureWindow函数以及CreateBitmapPicture函数,CaptureWindow函数建立与要捕捉的窗口的绘图设备(Device Context)句柄相兼容的绘图设备(Device Context)句柄,然后建立相应的调色板,最后将绘图设备(Device Context)中的图象拷贝到一个hBitmap对象句柄中。CreateBitmapPicture函数则根据传递过来的hBitmap对象句柄和调色板句柄建立一个Picture对象。在将这个对象赋予PictureBox的Picture属性,然后就可以使用SavePicture函数来保存图象了。 OleCreatePictureIndirect函数支持的不仅有BMP图象,同时也支持Ico图标,所以利用该函数和ExtractIcon函数也可以提取并保存Windows文件中的图标