VB图片框显示PNG图片的代码
(2019-02-19 09:36:55)
标签:
vb图片框显示png图片 |
分类: VB编程 |
VB图片框显示PNG图片的代码
VB图片框不能显示PNG图片,这就需要我们想点办法才行。办法如下:
在工程中添加一个类模块LoadPNG,添加一个窗体,在窗体上添加一个图片框、一个按纽、一个文本框。
窗体的代码:
-----------------------------------------------------
Option Explicit
Dim pngClass As New LoadPNG
Private Sub Command1_Click()
On Error GoTo 100
Dim Filename As String, dlg As Object
Set dlg = CreateObject("MSComDlg.CommonDialog")
With dlg
End With
If Len(Filename) > 5 Then
End If
100
End Sub
类模块的代码:
--------------------------------------------
Option Explicit
Private Type RGBTriple
End Type
Private Type BITMAPINFOHEADER 'BMP位图的信息头结构
End Type
Private Type
RGBQUAD
End Type
Private Type
BITMAPINFO_1
End Type
Private Type BITMAPINFO_2
End Type
Private Type
BITMAPINFO_4
End Type
Private Type
BITMAPINFO_8
End Type
Private Type
BITMAPINFO_24
End Type
Private Type
BITMAPINFO_24a
End Type
Private Type
IHDR
End Type
Private Type CodesType
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject
As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc
As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long)
As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As
Long, ByVal hObject As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long,
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal
nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal
YSrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long,
ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As
Long
Private Declare Function CreateDIBitmap_1 Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_1, ByVal wUsage As Long) As Long
Private Declare Function CreateDIBitmap_2 Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_2, ByVal wUsage As Long) As Long
Private Declare Function CreateDIBitmap_4 Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_4, ByVal wUsage As Long) As Long
Private Declare Function CreateDIBitmap_8 Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_8, ByVal wUsage As Long) As Long
Private Declare Function CreateDIBitmap_24 Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_24, ByVal wUsage As Long) As Long
Private Declare Function CreateDIBitmap_24a Lib "gdi32" Alias
"CreateDIBitmap" (ByVal hdc As Long, lpInfoHeader As
BITMAPINFOHEADER, ByVal dwUsage As Long, lpInitBits As Any,
lpInitInfo As BITMAPINFO_24a, ByVal wUsage As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long,
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal
nHeight As Long, ByVal hSrcDC As Long, ByVal XSrc As Long, ByVal
YSrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long,
ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As
Long, lpBits As Any, lpBI As Any, ByVal wUsage 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 Sub FillMemory Lib "kernel32.dll" Alias
"RtlFillMemory" (Destination As Any, ByVal length As Long, ByVal
Fill As Byte)
Private bm1 As BITMAPINFO_1
Private bm2 As BITMAPINFO_2
Private bm4 As BITMAPINFO_4
Private bm8 As BITMAPINFO_8
Private bm24 As BITMAPINFO_24
Private bm24a As BITMAPINFO_24a
Dim TempLit As CodesType, TempDist As CodesType, Dist As
CodesType
Dim LC As CodesType, dc As CodesType, LitLen As CodesType
Dim OutStream() As Byte, InStream() As Byte, Pow2(16) As Long,
BitMask(16) As Long, LenOrder(18) As Long
Dim hBmp As Long, OutPos As Long, Inpos As Long, ByteBuff As Long,
BitNum As Long
Dim MinLLenght As Long, MaxLLenght As Long, MinDLenght As Long,
MaxDLenght As Long
Dim BPPprivat As Long, Colorused As Long
Dim IDATData() As
Byte
Dim IdataLen As
Long
Dim Palettenbyte() As Byte
Dim IsStaticBuild As Boolean '创建静态结构标记
Dim m_Bgx As
Long
Dim m_Bgy As
Long
Dim m_multiple As
Long
Dim m_PicBox As
Object
'只读属性值
Dim m_width As
Long
Dim m_height As
Long
Dim m_bitdepht As
Long
Dim m_colortype As
Long
Dim m_text As
String
'方法(前2个参数必需,后3个参数可选):1-文件名,2-显示图像的对象,3、4-显示坐标,5-放大倍数
Public Sub OpenPNG(Filename As String, ByVal PicBox As Object,
Optional x As Long = 0, Optional y As Long = 0, Optional Multiple
As Single = 1)
On Error GoTo 100
Dim Stand As
Long
Dim Ende As
Boolean
Dim Filenumber As Long
Dim Signature(7) As Byte
Dim Test As Long
Dim Ltge As Long
Dim ChunkName As String * 4 '数据块符号
Dim ChunkInhalt() As Byte
Dim CRC32Inhalt As
Long
ReDim IDATData(0)
Set m_PicBox = PicBox: m_Bgx = x: m_Bgy = y: BPPprivat = 0:
IdataLen = 0: If Multiple > 0 Then m_multiple = Multiple
Filenumber = FreeFile
Open Filename For Binary As Filenumber
Get Filenumber, , Signature
Test = IsValidSignature(Signature) '读出前8个字节
If Not Test
Then
End If
Do While Ende = False
Loop
Close Filenumber
If IdataLen > 0 Then MakePicture '处理图形
100
End Sub
Private Function IsValidSignature(Signature() As Byte) As
Boolean '判断是否PNG标记
If Signature(0) <> 137 Then Exit Function
If Signature(1) <> 80 Then Exit Function
If Signature(2) <> 78 Then Exit Function
If Signature(3) <> 71 Then Exit Function
If Signature(4) <> 13 Then Exit Function
If Signature(5) <> 10 Then Exit Function
If Signature(6) <> 26 Then Exit Function
If Signature(7) <> 10 Then Exit Function
IsValidSignature = True
End Function
Private Sub SwapBytesLong(ByteValue As Long)
'转化长整形低位在前高位在后的数据
Dim Tergabe As Long, i As Long
For i = 0 To 3
Next i
ByteValue = Tergabe
End Sub
Private Sub ReadIHDR(Bytefeld() As Byte) '处理文件头数据块
Dim Header As IHDR
CopyMemory ByVal VarPtr(Header), Bytefeld(0), 13
SwapBytesLong Header.Width
SwapBytesLong Header.Height
m_width =
Header.Width
m_height =
Header.Height
m_bitdepht =
Header.BitDepth
m_colortype =
Header.ColorType
End Sub
Private Sub MakePicture() '处理图形
Dim Buffer() As
Byte
Dim BitCount As Integer, Bitdepht As Long, Drehen As Integer,
DataSize As Long
Drehen = 1
DataSize = DataPerRow * m_height '非隔行扫描方法
ReDim Buffer(UBound(IDATData) - 2)
CopyMemory Buffer(0), IDATData(2), UBound(IDATData) - 1
Decompress Buffer, DataSize '使用压缩
Buffer = DeFilter(Buffer)
Drehen = 1
BitCount =
m_bitdepht
Select Case
m_colortype