当前位置:编程文档 >> VB >> VB利用GDI+显示PNG图像
首页

VB利用GDI+显示PNG图像

所属类别:VB
推荐指数:★★★☆
文档人气:516
本周人气:3
发布日期:2007-7-20

在VB使用PNG透明格式的图片其实是非常容易的,那就是使用XP以上操作系统中的GDI+库:

需要声明的GDI+的函数:
    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

文档说明:

     

相关文档