Dim relWH As Single Dim W As Integer Dim H As Integer
W = Picture1.Width H = Picture1.Height
If W > Thumb_max_size Or H > Thumb_max_size Then If W >= H Then '# Stretch to width: Image_DB.Width = Thumb_max_size Image_DB.Height = (H / W) * Thumb_max_size Else '# Stretch to height: Image_DB.Height = Thumb_max_size Image_DB.Width = (W / H) * Thumb_max_size End If '# Resize StretchBlt Image_DB.hdc, _ 0, 0, _ Image_DB.Width, Image_DB.Height, _ Picture1.hdc, _ 0, 0, _ Picture1.Width, Picture1.Height, _ SRCCOPY Else '# No stretch Image_DB = Picture1 End If
这段代码你看看 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 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 LongPublic Function SaveTohBmp(ByVal hdcSrc As Long, 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 hPal As Long Dim hPalPrev As Long Dim RasterCapsScrn As Long Dim HasPaletteScrn As Long Dim PaletteSizeScrn As Long Dim LogPal As LOGPALETTE
' '建立一个内存图形设备句柄 hDCMemory = CreateCompatibleDC(hdcSrc) '建立一个bitmap并保存到hDCMemory中 hBmp = CreateCompatibleBitmap(hdcSrc, WidthSrc, HeightSrc) hBmpPrev = SelectObject(hDCMemory, hBmp) ' Get screen properties. RasterCapsScrn = GetDeviceCaps(hdcSrc, RASTERCAPS) ' Raster ' capabilities. HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette ' support. PaletteSizeScrn = GetDeviceCaps(hdcSrc, SIZEPALETTE) ' Size of ' palette. 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) Debug.Print r
'调用CreateBitmapPicture函数从指定的bitmap对象和调色板中建立一个picture对象 Set SaveTohBmp = CreateBitmapPicture(hBmp, hPal) End Function 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) ' Length of structure. .Type = vbPicTypeBitmap ' Type of Picture (bitmap). .hBmp = hBmp ' Handle to bitmap. .hPal = hPal ' Handle to palette (may be null). End With '建立Picture对象 r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic) '返回Picture对象 Set CreateBitmapPicture = IPic End Function
2)PictureBox.PaintPicture
Dim W As Integer
Dim H As Integer
W = Picture1.Width
H = Picture1.Height
If W > Thumb_max_size Or H > Thumb_max_size Then
If W >= H Then
'# Stretch to width:
Image_DB.Width = Thumb_max_size
Image_DB.Height = (H / W) * Thumb_max_size
Else
'# Stretch to height:
Image_DB.Height = Thumb_max_size
Image_DB.Width = (W / H) * Thumb_max_size
End If
'# Resize
StretchBlt Image_DB.hdc, _
0, 0, _
Image_DB.Width, Image_DB.Height, _
Picture1.hdc, _
0, 0, _
Picture1.Width, Picture1.Height, _
SRCCOPY
Else
'# No stretch
Image_DB = Picture1
End If
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 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 LongPublic Function SaveTohBmp(ByVal hdcSrc As Long, 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 hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
'
'建立一个内存图形设备句柄
hDCMemory = CreateCompatibleDC(hdcSrc)
'建立一个bitmap并保存到hDCMemory中
hBmp = CreateCompatibleBitmap(hdcSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp) ' Get screen properties.
RasterCapsScrn = GetDeviceCaps(hdcSrc, RASTERCAPS) ' Raster
' capabilities.
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
' support.
PaletteSizeScrn = GetDeviceCaps(hdcSrc, SIZEPALETTE) ' Size of
' palette. 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)
Debug.Print r
'调用CreateBitmapPicture函数从指定的bitmap对象和调色板中建立一个picture对象
Set SaveTohBmp = CreateBitmapPicture(hBmp, hPal)
End Function
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) ' Length of structure.
.Type = vbPicTypeBitmap ' Type of Picture (bitmap).
.hBmp = hBmp ' Handle to bitmap.
.hPal = hPal ' Handle to palette (may be null).
End With '建立Picture对象
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic) '返回Picture对象
Set CreateBitmapPicture = IPic
End Function