主题:用GDI+显示PNG图像所遇到的问题
我正在学 GDI+,遇到了一个问题,请教能者。
Option Explicit
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (ByRef token As Long, ByRef inputX As GdiplusStartupInput, ByVal Output As Long) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal hdc As Long, ByRef graphics As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "gdiplus.dll" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "GDIPlus" (ByVal FileName As Long, ByRef Image As Long) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus.dll" (ByVal token As Long) As GpStatus
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Sub Form_Load()
Dim udtData As GdiplusStartupInput
Dim lGDIP As Long
Dim m_lngGraphics As Long
Dim m_lngPic As Long
udtData.GdiplusVersion = 1 'GDI+初始化
If GdiplusStartup(lGDIP, udtData, 0) Then MsgBox "GDI+ 无法启动", 16: Exit Sub
If GdipCreateFromHDC(Me.hdc, m_lngGraphics) Then MsgBox "无法创建 Graphics 对象", 16: Exit Sub
GdipLoadImageFromFile StrPtr(App.Path & "\1.png"), m_lngPic
GdipDrawImage m_lngGraphics, m_lngPic, 0, 0
GdiplusShutdown lGDIP '销毁 GDI+
End Sub
以上代码可以正常打开png图片,但是,如果将 Form_Load 中的代码移动到按纽的单击事件中去,再点击按纽,窗体上却没有显示,不知什么原因?如何解决?
Option Explicit
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (ByRef token As Long, ByRef inputX As GdiplusStartupInput, ByVal Output As Long) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal hdc As Long, ByRef graphics As Long) As GpStatus
Private Declare Function GdipDrawImage Lib "gdiplus.dll" (ByVal graphics As Long, ByVal Image As Long, ByVal X As Single, ByVal Y As Single) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "GDIPlus" (ByVal FileName As Long, ByRef Image As Long) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus.dll" (ByVal token As Long) As GpStatus
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Sub Form_Load()
Dim udtData As GdiplusStartupInput
Dim lGDIP As Long
Dim m_lngGraphics As Long
Dim m_lngPic As Long
udtData.GdiplusVersion = 1 'GDI+初始化
If GdiplusStartup(lGDIP, udtData, 0) Then MsgBox "GDI+ 无法启动", 16: Exit Sub
If GdipCreateFromHDC(Me.hdc, m_lngGraphics) Then MsgBox "无法创建 Graphics 对象", 16: Exit Sub
GdipLoadImageFromFile StrPtr(App.Path & "\1.png"), m_lngPic
GdipDrawImage m_lngGraphics, m_lngPic, 0, 0
GdiplusShutdown lGDIP '销毁 GDI+
End Sub
以上代码可以正常打开png图片,但是,如果将 Form_Load 中的代码移动到按纽的单击事件中去,再点击按纽,窗体上却没有显示,不知什么原因?如何解决?