关于VB6中代码显示PNG图片的问题

前端之家收集整理的这篇文章主要介绍了关于VB6中代码显示PNG图片的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

vb6中显示png图片,主要代码来自于帖子

http://www.vbforums.com/showthread.php?509292-RESOLVED-png-files-in-Visual-Basic中的【Using_Ping_In_VB.ZIP】例子。

本人对【Using_Ping_In_VB.ZIP】的代码做了相关修改以适应本人要求。具体代码见下面

1.modGDIPlusResize.bas

Option Explicit

Private Type GUID
   Data1    As Long
   Data2    As Integer
   Data3    As Integer
   Data4(7) As Byte
End Type

Private Type PICTDESC
   size     As Long
   Type     As Long
   hBmp     As Long
   hPal     As Long
   Reserved As Long
End Type

Private Type GdiplusStartupInput
    GdiplusVersion           As Long
    DebugEventCallback       As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs   As Long
End Type

Private Type PWMFRect16
    left   As Integer
    top    As Integer
    Right  As Integer
    Bottom As Integer
End Type

Private Type wmfPlaceableFileHeader
    Key         As Long
    hMf         As Integer
    BoundingBox As PWMFRect16
    Inch        As Integer
    Reserved    As Long
    CheckSum    As Integer
End Type

' GDI Functions
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PICTDESC,RefIID As GUID,ByVal fPictureOwnsHandle As Long,IPic As IPicture) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long,ByVal nWidth As Long,ByVal nHeight As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long,ByVal nIndex As Long) As Long
Private Declare Function PatBlt Lib "gdi32" (ByVal hDC As Long,ByVal x As Long,ByVal y As Long,ByVal nHeight As Long,ByVal dwRop As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long,ByVal nPlanes As Long,ByVal nBitCount As Long,lpBits As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long,ByVal hObject As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long

' GDI+ functions
Private Declare Function GdipLoadImageFromFile Lib "gdiplus.dll" (ByVal FileName As Long,GpImage As Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (Token As Long,gdipInput As GdiplusStartupInput,GdiplusStartupOutput As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal hDC As Long,GpGraphics As Long) As Long
Private Declare Function GdipSetInterpolationMode Lib "gdiplus.dll" (ByVal Graphics As Long,ByVal InterMode As Long) As Long
Private Declare Function GdipDrawImageRectI Lib "gdiplus.dll" (ByVal Graphics As Long,ByVal Img As Long,ByVal Width As Long,ByVal Height As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus.dll" (ByVal Graphics As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus.dll" (ByVal Image As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus.dll" (ByVal hBmp As Long,ByVal hPal As Long,GpBitmap As Long) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus.dll" (ByVal Image As Long,Width As Long) As Long
Private Declare Function GdipGetImageHeight Lib "gdiplus.dll" (ByVal Image As Long,Height As Long) As Long
Private Declare Function GdipCreateMetafileFromWmf Lib "gdiplus.dll" (ByVal hWmf As Long,ByVal deleteWmf As Long,WmfHeader As wmfPlaceableFileHeader,Metafile As Long) As Long
Private Declare Function GdipCreateMetafileFromEmf Lib "gdiplus.dll" (ByVal hEmf As Long,ByVal deleteEmf As Long,Metafile As Long) As Long
Private Declare Function GdipCreateBitmapFromHICON Lib "gdiplus.dll" (ByVal hIcon As Long,GpBitmap As Long) As Long
Private Declare Function GdipDrawImageRectRectI Lib "gdiplus.dll" (ByVal Graphics As Long,ByVal GpImage As Long,ByVal dstx As Long,ByVal dsty As Long,ByVal dstwidth As Long,ByVal dstheight As Long,ByVal srcx As Long,ByVal srcy As Long,ByVal srcwidth As Long,ByVal srcheight As Long,ByVal srcUnit As Long,ByVal imageAttributes As Long,ByVal callback As Long,ByVal callbackData As Long) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus.dll" (ByVal Token As Long)

' GDI and GDI+ constants
Private Const PLANES = 14            '  Number of planes
Private Const BITSPIXEL = 12         '  Number of bits per pixel
Private Const PATCOPY = &HF00021     ' (DWORD) dest = pattern
Private Const PICTYPE_BITMAP = 1     ' Bitmap type
Private Const InterpolationModeHighQualityBicubic = 7
Private Const GDIP_WMF_PLACEABLEKEY = &H9AC6CDD7
Private Const UnitPixel = 2

' Initialises GDI Plus
Public Function InitGDIPlus() As Long
    Dim Token    As Long
    Dim gdipInit As GdiplusStartupInput
    
    gdipInit.GdiplusVersion = 1
    GdiplusStartup Token,gdipInit,ByVal 0&
    InitGDIPlus = Token
End Function

' Frees GDI Plus
Public Sub FreeGDIPlus(Token As Long)
    GdiplusShutdown Token
End Sub

' Loads the picture (optionally resized)
Public Function LoadPictureGDIPlus(PicFile As String,Optional Width As Long = -1,Optional Height As Long = -1,Optional ByVal BackColor As Long = vbWhite,Optional RetainRatio As Boolean = False) As IPicture
    Dim hDC     As Long
    Dim hBitmap As Long
    Dim Img     As Long
        
    ' Load the image
    If GdipLoadImageFromFile(StrPtr(PicFile),Img) <> 0 Then
        Err.Raise 999,"GDI+ Module","Error loading picture " & PicFile
        Exit Function
    End If
    
    ' Calculate picture's width and height if not specified
    If Width = -1 Or Height = -1 Then
        GdipGetImageWidth Img,Width
        GdipGetImageHeight Img,Height
    End If
    
    ' Initialise the hDC
    InitDC hDC,hBitmap,BackColor,Width,Height

    ' Resize the picture
    gdipResize Img,hDC,Height,RetainRatio
    GdipDisposeImage Img
    
    ' Get the bitmap back
    GetBitmap hDC,hBitmap

    ' Create the picture
    Set LoadPictureGDIPlus = CreatePicture(hBitmap)
End Function

' Initialises the hDC to draw
Private Sub InitDC(hDC As Long,hBitmap As Long,BackColor As Long,Width As Long,Height As Long)
    Dim hBrush As Long
        
    ' Create a memory DC and select a bitmap into it,fill it in with the backcolor
    hDC = CreateCompatibleDC(ByVal 0&)
    hBitmap = CreateBitmap(Width,GetDeviceCaps(hDC,PLANES),BITSPIXEL),ByVal 0&)
    hBitmap = SelectObject(hDC,hBitmap)
    hBrush = CreateSolidBrush(BackColor)
    hBrush = SelectObject(hDC,hBrush)
    PatBlt hDC,PATCOPY
    DeleteObject SelectObject(hDC,hBrush)
End Sub

' Resize the picture using GDI plus
Private Sub gdipResize(Img As Long,hDC As Long,Height As Long,Optional RetainRatio As Boolean = False)
    Dim Graphics   As Long      ' Graphics Object Pointer
    Dim OrWidth    As Long      ' Original Image Width
    Dim OrHeight   As Long      ' Original Image Height
    Dim OrRatio    As Double    ' Original Image Ratio
    Dim DesRatio   As Double    ' Destination rect Ratio
    Dim DestX      As Long      ' Destination image X
    Dim DestY      As Long      ' Destination image Y
    Dim DestWidth  As Long      ' Destination image Width
    Dim DestHeight As Long      ' Destination image Height
    
    GdipCreateFromHDC hDC,Graphics
    GdipSetInterpolationMode Graphics,InterpolationModeHighQualityBicubic
    
    If RetainRatio Then
        GdipGetImageWidth Img,OrWidth
        GdipGetImageHeight Img,OrHeight
        
        OrRatio = OrWidth / OrHeight
        DesRatio = Width / Height
        
        ' Calculate destination coordinates
        DestWidth = IIf(DesRatio < OrRatio,Height * OrRatio)
        DestHeight = IIf(DesRatio < OrRatio,Width / OrRatio,Height)
'        DestX = (Width - DestWidth) / 2
'        DestY = (Height - DestHeight) / 2

        DestX = 0
        DestY = 0

        GdipDrawImageRectRectI Graphics,Img,DestX,DestY,DestWidth,DestHeight,OrWidth,OrHeight,UnitPixel,0
    Else
        GdipDrawImageRectI Graphics,Height
    End If
    GdipDeleteGraphics Graphics
End Sub

' Replaces the old bitmap of the hDC,Returns the bitmap and Deletes the hDC
Private Sub GetBitmap(hDC As Long,hBitmap As Long)
    hBitmap = SelectObject(hDC,hBitmap)
    DeleteDC hDC
End Sub

' Creates a Picture Object from a handle to a bitmap
Private Function CreatePicture(hBitmap As Long) As IPicture
    Dim IID_IDispatch As GUID
    Dim Pic           As PICTDESC
    Dim IPic          As IPicture
    
    ' Fill in OLE IDispatch Interface ID
    IID_IDispatch.Data1 = &H20400
    IID_IDispatch.Data4(0) = &HC0
    IID_IDispatch.Data4(7) = &H46
        
    ' Fill Pic with necessary parts
    Pic.size = Len(Pic)        ' Length of structure
    Pic.Type = PICTYPE_BITMAP  ' Type of Picture (bitmap)
    Pic.hBmp = hBitmap         ' Handle to bitmap

    ' Create the picture
    OleCreatePictureIndirect Pic,IID_IDispatch,True,IPic
    Set CreatePicture = IPic
End Function

' Returns a resized version of the picture
Public Function Resize(Handle As Long,PicType As PictureTypeConstants,Optional BackColor As Long = vbWhite,Optional RetainRatio As Boolean = False) As IPicture
    Dim Img       As Long
    Dim hDC       As Long
    Dim hBitmap   As Long
    Dim WmfHeader As wmfPlaceableFileHeader
    
    ' Determine pictyre type
    Select Case PicType
    Case vbPicTypeBitmap
         GdipCreateBitmapFromHBITMAP Handle,ByVal 0&,Img
    Case vbPicTypeMetafile
         FillInWmfHeader WmfHeader,Height
         GdipCreateMetafileFromWmf Handle,False,WmfHeader,Img
    Case vbPicTypeEMetafile
         GdipCreateMetafileFromEmf Handle,Img
    Case vbPicTypeIcon
         ' Does not return a valid Image object
         GdipCreateBitmapFromHICON Handle,Img
    End Select
    
    ' Continue with resizing only if we have a valid image object
    If Img Then
        InitDC hDC,Height
        gdipResize Img,RetainRatio
        GdipDisposeImage Img
        GetBitmap hDC,hBitmap
        Set Resize = CreatePicture(hBitmap)
    End If
End Function

' Fills in the wmfPlacable header
Private Sub FillInWmfHeader(WmfHeader As wmfPlaceableFileHeader,Height As Long)
    WmfHeader.BoundingBox.Right = Width
    WmfHeader.BoundingBox.Bottom = Height
    WmfHeader.Inch = 1440
    WmfHeader.Key = GDIP_WMF_PLACEABLEKEY
End Sub

2. 调用Form1.frm

Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Private Sub Command1_Click()
 Dim Token As Long
 Dim C As Double 
 Dim i As Integer
    
 C = Me.BackColor
 If C < 0 Then C = GetSysColor(C - &H80000000)

 Token = InitGDIPlus
 
 Picture1(0).Picture = LoadPictureGDIPlus(App.Path & "\1.png",vbWhite)
 Picture1(1).Picture = LoadPictureGDIPlus(App.Path & "\1.png",vbCyan)
 Picture1(2).Picture = LoadPictureGDIPlus(App.Path & "\1.png",vbGreen)
 Picture1(3).Picture = LoadPictureGDIPlus(App.Path & "\1.png",C)

 FreeGDIPlus Token
End Sub

主要代码改动说明

1.Dim C As Long 修改为Dim C As Double (因Long类型数据范围不能满足存储color数据的需要,所以将变量C的数据类型改为Double,以便于存储color数据,如果不做修改,程序在调试时可正常运行,但在编译后运行会出现数据溢出的问题),long类型与double类型的数据范围可自行查找vb数据类型资料来进行相关比较。

2. Picture1(0).AutoSize 属性设计时改为True,当然也可在运行时通过代码实现。(因LoadPictureGDIPlus函数根据png图片的大小来进行透明处理,如果png图片大小比Picture1控件小,那么png图片Picture1控件之间的区域将不能被透明处理)。有兴趣的朋友可进行相关测试查看效果


下面为效果

原文链接:https://www.f2er.com/vb/258389.html

猜你在找的VB相关文章