在左侧工具栏右键打开部件,加载"Microsoft Common Dialog Control 6.0(SP6)",将其画到窗体上,默认名称为"CommonDialog1",再画一个Picture1和一个Command1,加入以下代码:
Option Explicit
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
Const UnitPixel As Long = &H2&
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Function LoagPNG(ByVal PNGurl As String, ByVal Controls As Object) 'PNG图片路径,图片控件
Controls.AutoRedraw = True
Dim Image As Long
Dim Graphics As Long
Dim Token As Long
Dim GdipInput As GdiplusStartupInput
GdipInput.GdiplusVersion = 1
GdiplusStartup Token, GdipInput
GdipLoadImageFromFile StrPtr(PNGurl), Image
GdipGetImageWidth Image, Controls.Width
GdipGetImageHeight Image, Controls.Height
GdipCreateFromHDC Controls.hdc, Graphics
GdipDrawImageRectRectI Graphics, Image, 0, 0, Controls.Width, Controls.Height, 0, 0, Controls.Width, Controls.Height, UnitPixel, 0, 0, 0 '显示位置
GdipDeleteGraphics Graphics
GdipDisposeImage Image
Controls.Refresh
Controls.Picture = Controls.Image
GdiplusShutdown Token
End Function
Private Sub Command1_Click()
CommonDialog1.Filter = “JPG|.jpg|BMP|.bmp|GIF|.gif|NPG|.png”
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> “” Then
Me.Picture = LoadPicture("")
Call LoagPNG(CommonDialog1.FileName, Picture1)
End If
End Sub