如何实现屏幕拷贝?谢

解决方案 »

  1.   

    看看
    Option Explicit
    Private Type PALETTEENTRY
       peRed As Byte
       peGreen As Byte
       peBlue As Byte
       peFlags As Byte
    End Type
    Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Declare Function getDesktop Lib "ImageUtils.dll" (ByVal strFileName As String, ByVal blnEnableOverWrite As Boolean, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal blnJpeg As Boolean, ByVal JPGCompressQuality As Integer) As Integer
    Private Declare Function GrayScale Lib "ImageUtils.dll" (ByVal strInputFile As String, ByVal strOutputFile As String, ByVal blnEnableOverWrite As Boolean) As Integer
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function ConvertBMPtoJPG Lib "ImageUtils.dll" (ByVal strInputFile As String, ByVal strOutputFile As String, ByVal blnEnableOverWrite As Boolean, ByVal JPGCompressQuality As Integer, ByVal blnKeepBMP As Boolean) As Integer
    Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Private Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    Private Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) As Long
    Private Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    Private Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As Long
    Private 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 Long
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
    Private Type PicBmp
       Size As Long
       Type As Long
       hBmp As Long
       hPal As Long
       Reserved As Long
    End Type
    Private Type GUID
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(7) As Byte
    End Type
    Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    Private Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long
    Private Const RASTERCAPS As Long = 38
    Private Const RC_PALETTE As Long = &H100
    Private Const SIZEPALETTE As Long = 104
    Const MAX_PATH = 260
    Dim FileNumber As Long, Sentbyt As Long
    Dim strDestination As String
    Private Type LOGPALETTE
       palVersion As Integer
       palNumEntries As Integer
       palPalEntry(255) As PALETTEENTRY
    End Type
    Sub GetDesktops()
    On Error GoTo err:
    Dim strSource As String, retval As Long
    Set Main.PicCopy.Picture = CaptureScreen()
    strSource = GetTmpPath & "desktop.bmp"
    SavePicture Main.PicCopy, strSource
    retval = ConvertBMPtoJPG(strSource, strDestination, True, 100, True)
    err:
    End SubPrivate Sub Class_Initialize()
    strDestination = GetTmpPath & "desktop.jpg"
    dirDespPic = strDestination
    End SubFunction GetTmpPath() As String
    On Error Resume Next
    Dim strFolder As String
    Dim lngResult As Long
    strFolder = String(MAX_PATH, 0)
    lngResult = GetTempPath(MAX_PATH, strFolder)
    If lngResult <> 0 Then
    GetTmpPath = Left(strFolder, InStr(strFolder, Chr(0)) - 1)
    Else: GetTmpPath = ""
    End If
    End FunctionSub UnColor()
    On Error GoTo err
    Dim retval As Long
    retval = GrayScale(strDestination, strDestination, True)
    Main.PicCopy.Picture = LoadPicture(strDestination)
    err:
    End Sub
    Private Function CaptureScreen() As Picture
    On Error GoTo err
    Dim hWndScreen As Long
    hWndScreen = GetDesktopWindow()
    Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width \ Screen.TwipsPerPixelX, Screen.Height \ Screen.TwipsPerPixelY)
    err:
    End Function
    Private 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
    On Error GoTo err
    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)   If HasPaletteScrn And (PaletteSizeScrn = 256) Then      LogPal.palVersion = &H300
          LogPal.palNumEntries = 256
          r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
          hPal = CreatePalette(LogPal)      hPalPrev = SelectPalette(hDCMemory, hPal, 0)
          r = RealizePalette(hDCMemory)
       End If
       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)
    err:
    End Function
    Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
      On Error GoTo err
      Dim r As Long   Dim pic As PicBmp
     
       Dim IPic As IPicture
       Dim IID_IDispatch As GUID
       With IID_IDispatch
          .Data1 = &H20400
          .Data4(0) = &HC0
          .Data4(7) = &H46
       End With   With pic
          .Size = Len(pic)
          .Type = vbPicTypeBitmap
          .hBmp = hBmp
          .hPal = hPal
       End With   r = OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)   Set CreateBitmapPicture = IPic
    err:
    End Function
      

  2.   

    Where to find the ImageUtils.dll??
      

  3.   

    看我的Option Explicit
    Option Base 0Private Type PALETTEENTRY
       peRed As Byte
       peGreen As Byte
       peBlue As Byte
       peFlags As Byte
    End TypePrivate Type LOGPALETTE
       palVersion As Integer
       palNumEntries As Integer
       palPalEntry(255) As PALETTEENTRY  ' Enough for 256 colors.
    End TypePrivate Type GUID
       Data1 As Long
       Data2 As Integer
       Data3 As Integer
       Data4(7) As Byte
    End TypePrivate Const RASTERCAPS As Long = 38
    Private Const RC_PALETTE As Long = &H100
    Private Const SIZEPALETTE As Long = 104Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End TypePrivate Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal iCapabilitiy As Long) As Long
    Private Declare Function GetSystemPaletteEntries Lib "GDI32" (ByVal hDC As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    Private Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) As Long
    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Private 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 Long
    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
    Private Declare Function GetForegroundWindow Lib "USER32" () As Long
    Private Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    Private Declare Function RealizePalette Lib "GDI32" (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
    Private Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
    Private Declare Function GetDesktopWindow Lib "USER32" () As LongPrivate Type PicBmp
       Size As Long
       Type As Long
       hBmp As Long
       hPal As Long
       Reserved As Long
    End TypePrivate Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    ' Capture the entire screen.
    Private Sub Command1_Click()
       Set Picture1.Picture = CaptureScreen()
    End Sub'清空PictureBox
    Private Sub Command2_Click()
        Set Picture1.Picture = Nothing
    End 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   '设置图片信息
       With Pic
          .Size = Len(Pic)          '图片大小
          .Type = vbPicTypeBitmap   '图片类型
          .hBmp = hBmp              '图片句柄
          .hPal = hPal              '调色板句柄
       End With
       '创建新的图片
       r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
       Set CreateBitmapPicture = IPic
    End 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
        
          hDCSrc = GetWindowDC(hWndSrc) '获取整个屏幕的设备环境
          
          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) '调色板大小
          If HasPaletteScrn And (PaletteSizeScrn = 256) Then
             '复制系统调色板
             LogPal.palVersion = &H300
             LogPal.palNumEntries = 256
             r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
             hPal = CreatePalette(LogPal)
             '选择新的调色板
             hPalPrev = SelectPalette(hDCMemory, hPal, 0)
             r = RealizePalette(hDCMemory)
          End If
          '复制屏幕图像
          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)
          '生成BMP位图
          Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    End FunctionPublic 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 FunctionPrivate Sub Command3_Click()
        CommonDialog1.DefaultExt = ".BMP"
        CommonDialog1.Filter = "Bitmap Image (*.bmp)|*.bmp"
        CommonDialog1.ShowSave
        If CommonDialog1.FileName <> "" Then
            SavePicture Picture1.Picture, CommonDialog1.FileName
        End If
    End Sub
      

  4.   

    你的不就是vb的例子么,这个问题倒是能结了,不过又有新问题出来,该怎样将bmp存成jpg???