Option Explicit
Private Type GdiplusStartupInput
GdiplusVersion As Long ' Must be 1 for GDI+ v1.0, the current version as of this writing.
DebugEventCallback As Long ' Ignored on free builds
SuppressBackgroundThread As Long ' FALSE unless you 're prepared to call
' the hook/unhook functions properly
SuppressExternalCodecs As Long ' FALSE unless you want GDI+ only to use
' its internal image codecs.
End Type
Private Enum GpStatus ' aka Status
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "GDIPlus" (ByVal graphics As Long, ByVal Image As Long, ByVal x As Single, ByVal y As Single) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "GDIPlus" (ByVal hDC As Long, graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "GDIPlus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "GDIPlus" (ByVal filename As String, Image As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As GpStatus
Dim gdip_Token As Long
Dim gdip_pngImage As Long
Dim gdip_Graphics As Long
Private Sub Form_Activate()
If GdipCreateFromHDC(Me.hDC, gdip_Graphics) <> Ok Then
MsgBox "出现错误!", vbCritical, "错误"
GdiplusShutdown gdip_Token
End
End If
Dim oStream As IStream
Dim ImageBytes() As Byte
Dim filename As String
filename = App.Path & "\test.png"
Open filename For Binary Access Read As #1
ReDim ImageBytes(LOF(1) - 1)
Get #1, , ImageBytes
Close #1
CreateStreamOnHGlobal ImageBytes(0), True, oStream
Dim iStatus As GpStatus '
iStatus = GdipLoadImageFromStream(oStream, gdip_pngImage) '加载文件
Set oStream = Nothing
'GdipLoadImageFromFile StrConv(App.Path & "\test.png", vbUnicode), gdip_pngImage '加载文件
End Sub
Private Sub Form_Load()
Dim GpInput As GdiplusStartupInput
GpInput.GdiplusVersion = 1
If GdiplusStartup(gdip_Token, GpInput) <> Ok Then
MsgBox "加载GDI+失败!", vbCritical, "加载错误"
End
End If
End Sub
Private Sub Form_Paint()
If GdipDrawImage(gdip_Graphics, gdip_pngImage, 0, 0) <> Ok Then Debug.Print "显示失败。。。"
End Sub
Private Sub Form_Unload(Cancel As Integer)
GdipDisposeImage gdip_pngImage
GdipDeleteGraphics gdip_Graphics
GdiplusShutdown gdip_Token
End Sub
vb 如何使用 PNG 透明格式的图片
Private Declare Function GdiplusStartup Lib "gdiplus.dll" ( _
ByRef token As Long, _
ByRef inputX As GdiplusStartupInput, _
ByVal Output As Long _
) As Status
Private Declare Sub GdiplusShutdown Lib "gdiplus.dll" (ByVal token As Long)
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" ( _
ByVal hdc As Long, ByRef graphics As Long _
) As Status
Private Declare Function GdipDrawImage Lib "gdiplus.dll" ( _
ByVal graphics As Long, ByVal Image As Long, _
ByVal X As Single, ByVal Y As Single _
) As Status
Private Declare Function GdipLoadImageFromFile Lib "gdiplus.dll" ( _
ByVal FileName As Long, ByRef Image As Long _
) As Status
Private Declare Function GdipDisposeImage Lib "gdiplus.dll" _
(ByVal Image As Long) As Status
需要声明的结构:
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
具体做法如下:
Dim m_lngGraphics as long
Dim m_lngInstance as long
Dim m_lngPic as long
Private Sub Form_Load() 'GDI+初始化
Dim udtData As GdiplusStartupInput
Randomize
udtData.GdiplusVersion = 1
If GdiplusStartup(app.hInstance, udtData, 0) Then
MsgBox "GDI+ could not be initialized", vbCritical
Exit Sub
End If
If GdipCreateFromHDC(Me.hdc, m_lngGraphics) Then
MsgBox "Graphics object could not be created", vbCritical
Exit Sub
End If
GdipLoadimagefromfile "c:\1.png" , m_lngPic
GdipImageDraw m_lngGraphics,m_lngPic
End Sub
Option Explicit
Private Type GdiplusStartupInput
GdiplusVersion As Long ' Must be 1 for GDI+ v1.0, the current version as of this writing.
DebugEventCallback As Long ' Ignored on free builds
SuppressBackgroundThread As Long ' FALSE unless you're prepared to call
' the hook/unhook functions properly
SuppressExternalCodecs As Long ' FALSE unless you want GDI+ only to use
' its internal image codecs.
End Type
Private Enum GpStatus ' aka Status
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal x As Single, ByVal y As Single) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As String, Image As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
Dim gdip_Token As Long
Dim gdip_pngImage As Long
Dim gdip_Graphics As Long
Private Sub Form_Activate()
If GdipCreateFromHDC(Me.hDC, gdip_Graphics) <> Ok Then
MsgBox "出现错误!", vbCritical, "错误"
GdiplusShutdown gdip_Token
End
End If
GdipLoadImageFromFile StrConv("C:\Show.png", vbUnicode), gdip_pngImage '加载文件
End Sub
Private Sub Form_Load()
Dim GpInput As GdiplusStartupInput
GpInput.GdiplusVersion = 1
If GdiplusStartup(gdip_Token, GpInput) <> Ok Then
MsgBox "加载GDI+失败!", vbCritical, "加载错误"
End
End If
End Sub
Private Sub Form_Paint()
If GdipDrawImage(gdip_Graphics, gdip_pngImage, 0, 0) <> Ok Then Debug.Print "显示失败。。。"
End Sub
Private Sub Form_Unload(Cancel As Integer)
GdipDisposeImage gdip_pngImage
GdipDeleteGraphics gdip_Graphics
GdiplusShutdown gdip_Token
End Sub
将StdPicture写入Byte数组及读出
'--------------------------------------------------------
' Procedure : SaveImage
' Purpose : Saves a StdPicture object in a byte array.
'--------------------------------------------------------
'
Public Function SaveImage( _
ByVal image As StdPicture) As Byte()
Dim abData() As Byte
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
Dim tStat As STATSTG
' Get the image IPersistStream interface
Set oPersist = image
' Create a stream on global memory
Set oStream = CreateStreamOnHGlobal(0, True)
' Save the picture in the stream
oPersist.Save oStream, True
' Get the stream info
oStream.Stat tStat, STATFLAG_NONAME
' Get the stream size
lSize = tStat.cbSize * 10000
' Initialize the array
ReDim abData(0 To lSize - 1)
' Move the stream position to
' the start of the stream
oStream.Seek 0, STREAM_SEEK_SET
' Read all the stream in the array
oStream.Read abData(0), lSize
' Return the array
SaveImage = abData
' Release the stream object
Set oStream = Nothing
End Function
'--------------------------------------------------------
' Procedure : LoadImage
' Purpose : Creates a StdPicture object from a byte array.
'--------------------------------------------------------
'
Public Function LoadImage( _
ImageBytes() As Byte) As StdPicture
Dim oPersist As IPersistStream
Dim oStream As IStream
Dim lSize As Long
1.引用olelib.tlb
2.模块中贴入下面代码:
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Const PictureID = &H746C&
Private Type PictureHeader
Magic As Long
Size As Long
End Type
Public Function Array2Picture(aBytes() As Byte) As IPicture
Dim oIPS As IPersistStream
Dim oStream As IStream
Dim hGlobal As Long
Dim LPTR As Long
Dim lSize As Long
Dim Hdr As PictureHeader