7,763
社区成员
发帖
与我相关
我的任务
分享
pp="d:\美图\1.jpg"
ww="108"
hh="90"
ShowTNImg Picture1, pp, ww, hh
'-------------缩略图函数-----------
Public Function ShowTNImg(pbox As Object, ImagePath As String, WMax As Long, HMax As Long) As ImageInfo
'WMax为最大宽度,HMax为最大高度,这段代码会根据最大宽度和高度调整图片大小。
Dim Wid As Long, Hgt As Long, Top As Long, Left As Long
LoadGDIP
If GdipCreateFromHDC(pbox.hDC, gdip_Graphics) <> 0 Then
MsgBox "出现错误!", vbCritical, "错误"
GdiplusShutdown gdip_Token
End
End If
'载入图片到内存中
GdipLoadImageFromFile StrConv(ImagePath, vbUnicode), gdip_Image
'获取图片长和宽
GdipGetImageWidth gdip_Image, Wid
GdipGetImageHeight gdip_Image, Hgt
With ShowTNImg
.Width = Wid
.Height = Hgt
.FilePath = ImagePath
.FileSize = FileLen(ImagePath) / 1024
.ImageName = Right(ImagePath, Len(ImagePath) - InStrRev(ImagePath, "\"))
.type = Right(ImagePath, Len(ImagePath) - InStrRev(ImagePath, "."))
End With
'智能调整图片大小和留空处理,根据最长边调整
If (Wid > WMax) Or (Hgt > HMax) Then
If Wid > Hgt Then
Hgt = Hgt / Wid * WMax
Wid = WMax
Top = (HMax - Hgt) / 2
Else
Wid = Wid / Hgt * HMax
Hgt = HMax
Left = (WMax - Wid) / 2
End If
Else
Top = (HMax - Hgt) / 2
Left = (WMax - Wid) / 2
End If
'使用GDI+直接从内存中缩略并绘图,GDI+有很好的抗锯齿能力
If GdipDrawImageRect(gdip_Graphics, gdip_Image, Left, Top, Wid, Hgt) <> ok Then
Debug.Print "显示失败。。。"
pbox.ForeColor = vbRed
pbox.DrawWidth = 5
pbox.Line (0, 0)-(pbox.Width, pbox.Height)
pbox.Line (pbox.Width, 0)-(0, pbox.Height)
End If
DisposeGDIP
End Function
Option Explicit
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As Long, hImage As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As Long, graphics As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As Long
Private Declare Function GdipDrawImageRectRectI Lib "gdiplus" (ByVal graphics As Long, ByVal hImage As Long, ByVal dstX As Long, ByVal dstY As Long, ByVal dstWidth As Long, ByVal dstHeight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcWidth As Long, ByVal SrcHeight As Long, ByVal srcUnit As Long, Optional ByVal imageAttributes As Long = 0, Optional ByVal callback As Long = 0, Optional ByVal callbackData As Long = 0) As Long
Private Declare Function GdipDrawImageRect Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As Long
Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As Long
Private Const UnitPixel As Long = &H2&
Private Sub Form_Load()
Me.AutoRedraw = True
DrawPicture Me.hdc, "d:\2.gif", 0, 0, 2.5 '显示比例为2.5倍
End Sub
Private Sub DrawPicture(ByVal hdcDraw As Long, ByVal FileName As String, Optional ByVal nLeft As Long = 0, Optional ByVal nTop As Long = 0, Optional nScale As Double = 1)
Dim hImage As Long
Dim graphics As Long
Dim Token As Long
Dim GdipInput As GdiplusStartupInput
Dim nWidth As Long
Dim nHeight As Long
GdipInput.GdiplusVersion = 1
GdiplusStartup Token, GdipInput
GdipLoadImageFromFile StrPtr(FileName), hImage
GdipGetImageWidth hImage, nWidth
nWidth = nWidth * nScale
GdipGetImageHeight hImage, nHeight
nHeight = nHeight * nScale
GdipCreateFromHDC hdcDraw, graphics
GdipDrawImageRect graphics, hImage, nLeft, nTop, nWidth, nHeight
GdipDeleteGraphics graphics
GdipDisposeImage hImage
GdiplusShutdown Token
End Sub
Option Explicit
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As Long, hImage As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As Long, Graphics As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As Long
Private Declare Function GdipDrawImageRectRectI Lib "gdiplus" (ByVal Graphics As Long, ByVal hImage As Long, ByVal dstX As Long, ByVal dstY As Long, ByVal dstWidth As Long, ByVal dstHeight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcWidth As Long, ByVal SrcHeight As Long, ByVal srcUnit As Long, Optional ByVal imageAttributes As Long = 0, Optional ByVal callback As Long = 0, Optional ByVal callbackData As Long = 0) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As Long
Private Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As Long
Private Const UnitPixel As Long = &H2&
Private Sub Form_Load()
Me.AutoRedraw = True
DrawPicture Me.hdc, "d:\2.gif"
End Sub
Private Sub DrawPicture(ByVal hdcDraw As Long, ByVal FileName As String, Optional ByVal nLeft As Long = 0, Optional ByVal nTop As Long = 0, Optional ByVal vWidth As Variant, Optional ByVal vHeight As Variant)
Dim hImage As Long
Dim Graphics As Long
Dim Token As Long
Dim GdipInput As GdiplusStartupInput
Dim nWidth As Long
Dim nHeight As Long
GdipInput.GdiplusVersion = 1
GdiplusStartup Token, GdipInput
GdipLoadImageFromFile StrPtr(FileName), hImage
If IsNumeric(vWidth) Then
nWidth = vWidth
Else
GdipGetImageWidth hImage, nWidth
End If
If IsNumeric(vHeight) Then
nHeight = vHeight
Else
GdipGetImageHeight hImage, nHeight
End If
GdipCreateFromHDC hdcDraw, Graphics
GdipDrawImageRectRectI Graphics, hImage, nLeft, nTop, nWidth, nHeight, 0, 0, nWidth, nHeight, UnitPixel, 0, 0, 0
GdipDeleteGraphics Graphics
GdipDisposeImage hImage
GdiplusShutdown Token
End Sub