|
楼主 |
发表于 2018-2-8 20:36
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 chenjyjustin 于 2018-2-8 20:41 编辑
vb按照以下步骤能轻松压缩图片,显示到pict图片框,然后存储
GdipCreateFromHDC
GdipLoadImageFromFile
GdipDrawImageRect
GdipSaveImageToFile pict.handle
VBA实现不了,没有handle可以调用。
于是转而考虑直接用api调用桌面 的HDC,在桌面输出压缩后的图片,
然后用CreateCompatibleDC CreateCompatibleBitmap截屏的方式去生成图片
这样就能得到压缩后图片的句柄
调用GdipSaveImageToFile
废话不多说,直接上源码
Public Sub CreateTNImg(SrcImgPath As String, TNImgpath As String, WidthMax As Long, HeightMax As Long)
LoadGDIP
Dim hDC As Long
hDC = GetDC(0) 'GetDC(FindWindow(vbNullString, Application.ActiveWindow.Caption)) ' 选定桌面作为设备场景
'GdipCreateFromHDC ( hDC, graphics )
' hDC 设备场景的句柄 graphics 是函数创建的画板成功后的句柄 ,可用GdipDeleteGraphics函数删除画板以释放内存空间。
If GdipCreateFromHDC(hDC, gdip_Graphics) <> 0 Then '
MsgBox "出现错误!", vbCritical, "错误"
GdiplusShutdown gdip_Token
End
End If
'载入图片到内存中
GdipLoadImageFromFile StrPtr(SrcImgPath), gdip_Image 'StrConv(SrcImgPath, vbUnicode), gdip_Image 'StrPtr
'使用GDI+直接从内存中缩略并绘图,GDI+有很好的反锯齿能力
'GdipDrawImageRect(Graphics As Long, Image As Long
' 向Graphics对象输出Image
'Graphics对象,可调用GdipCreateFromHDC函数创建
'image对象,可由GdipLoadImageFromFile加载的图像文件
Dim t As GpStatus, BmpInfo As BITMAPINFO
t = GdipDrawImageRect(gdip_Graphics, gdip_Image, 0, 0, WidthMax, HeightMax)
If t = Ok Then
Dim hDCdesk As Long
Dim hDCmem As Long
Dim hBmp As Long
Dim hBmpPrev As Long
hDCdesk = GetDC(0)
hDCmem = CreateCompatibleDC(hDCdesk)
hBmp = CreateCompatibleBitmap(hDCdesk, WidthMax, HeightMax)
hBmpPrev = SelectObject(hDCmem, hBmp)
BitBlt hDCmem, 0, 0, WidthMax, HeightMax, hDCdesk, 0, 0, &HCC0020 'vbSrcCopy
SelectObject hDCmem, hBmpPrev
DeleteDC hDCmem
ReleaseDC 0, hDCdesk
Dim lBitmap As Long '从句柄创建GDI+图像
lRes = GdipCreateBitmapFromHBITMAP(hBmp, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
SaveImageToJPG lBitmap, TNImgpath, 80
'销毁GDI+图像
GdipDisposeImage lBitmap
End If
DeleteObject hBmp
End If
DisposeGDIP
End Sub
Public Sub LoadGDIP()
Dim GpInput As GdiplusStartupInput
GpInput.GdiplusVersion = 1
If GdiplusStartup(gdips_Token, GpInput) <> 0 Then
MsgBox "加载GDI+失败!", vbCritical, "加载错误"
End
End If
End Sub
Public Sub DisposeGDIP()
GdipDisposeImage gdip_Image
GdipDeleteGraphics gdip_Graphics
GdiplusShutdown gdip_Token
End Sub |
评分
-
1
查看全部评分
-
|