VB6之GDI+加载PNG图片

原生的VB6不支持PNG个图片,因为刚有VB的时候还没有PNG的概念呢。不过,利用GDI+加载解析个PNG简直是砍瓜切菜般简单。

GDI+的模块是我在网上下载的,地址应该是:http://vistaswx.com/blog/article/gdip-tutorial-6-image

上代码:

 'code by lichmama from cnblogs.com
'@vb6.0 gdi+ png
Private Sub DrawPng(ByVal pngfile As String, _
Optional Left As Long = &, _
Optional Top As Long = &, _
Optional zoom As Single = #) Dim Graphic As Long
Dim Image As Long
Dim imgWidth As Long
Dim imgHeight As Long Call GdipCreateFromHDC(Me.hDC, Graphic)
Call GdipSetSmoothingMode(Graphic, SmoothingModeAntiAlias)
Call GdipLoadImageFromFile(StrPtr(pngfile), Image)
Call GdipGetImageWidth(Image, imgWidth)
Call GdipGetImageHeight(Image, imgHeight)
Call GdipDrawImageRect(Graphic, Image, & + Left, & + Top, imgWidth * zoom, imgHeight * zoom) Call GdipDisposeImage(Image)
Call GdipDeleteGraphics(Graphic)
End Sub Private Sub Command1_Click()
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-tulio-icon-2.png", -&, -&)
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-linda-icon-2.png", &)
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-logo.png", , &, 0.5)
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-kids-icon.png", &, &, 0.25)
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-nico-&-pedro-icon.png", &, &, 0.25)
Call DrawPng("D:\迅雷下载\rio-2-icons\rio-2-luiz-icon.png", &, &, 0.25)
End Sub Private Sub Form_Load()
Call InitGDIPlus
End Sub Private Sub Form_Unload(Cancel As Integer)
Call TerminateGDIPlus
End Sub

贴张图:

VB6之GDI+加载PNG图片

从资源文件加载PNG:

Private Function GdipCreateImageFromStream(ByVal resid As Integer, _
ByVal restype As String) As Long Dim Image As Long
Dim ResData() As Byte
Dim IStream As Object
Dim hGlobal As Long
Dim pMem As Long ResData = LoadResData(resid, restype)
hGlobal = GlobalAlloc(GMEM_MOVEABLE, UBound(ResData) + )
pMem = GlobalLock(hGlobal)
If pMem = Then
Debug.Print "Global Alloc Failed."
Erase ResData
Exit Function
End If
Call RtlMoveMemory(ByVal pMem, ResData(), UBound(ResData) + )
Call GlobalUnlock(hGlobal)
Call CreateStreamOnHGlobal(hGlobal, False, IStream)
Call GdipLoadImageFromStream(IStream, Image) Set IStream = Nothing
Call GlobalFree(hGlobal)
GdipCreateImageFromStream = Image
End Function
Private Sub Command1_Click()
Dim Graphics As Long
Dim Image As Long Call GdipCreateFromHDC(Me.hDC, Graphics)
Call GdipSetSmoothingMode(Graphics, SmoothingModeAntiAlias)
'调用方式如下
Image = GdipCreateImageFromStream(, "PNG")
Call GdipDrawImage(Graphics, Image, &, &) Call GdipDisposeImage(Image)
Call GdipDeleteGraphics(Graphics)
End Sub

贴张图:

VB6之GDI+加载PNG图片

上一篇:HDU1050(Moving Tables:贪心算法)


下一篇:Fix The thread xxx has exited with code 259 (0×103)