Consulting

Results 1 to 4 of 4

Thread: Solved: workaround for using PNG files

  1. #1
    VBAX Regular
    Joined
    Aug 2007
    Posts
    15
    Location

    Solved: workaround for using PNG files

    Hello all

    Is there a workaround for loading a PNG file on the userform, as loadpicture function doesn't support PNG files , and Gif files are really blurred. i am using excel 2003.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Put this code in a separate module

    [vba]

    Option Explicit
    Option Private Module

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

    Private Type PICTDESC
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
    End Type

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

    Private Declare Function GdiplusStartup Lib "GDIPlus" ( _
    token As Long, _
    inputbuf As GdiplusStartupInput, _
    Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" ( _
    ByVal FileName As Long, _
    bitmap As Long) As Long
    Private Declare Function GdipCreateHBITMAPFromBitmap Lib "GDIPlus" ( _
    ByVal bitmap As Long, _
    hbmReturn As Long, _
    ByVal background As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GDIPlus" ( _
    ByVal Image As Long) As Long
    Private Declare Function GdiplusShutdown Lib "GDIPlus" ( _
    ByVal token 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

    Public Function LoadImage(ByVal strFName As String) As IPicture
    Dim uGdiInput As GdiplusStartupInput
    Dim hGdiPlus As Long
    Dim hGdiImage As Long
    Dim hBitmap As Long

    uGdiInput.GdiplusVersion = 1

    If GdiplusStartup(hGdiPlus, uGdiInput) = 0 Then
    If GdipCreateBitmapFromFile(StrPtr(strFName), hGdiImage) = 0 Then
    GdipCreateHBITMAPFromBitmap hGdiImage, hBitmap, 0
    Set LoadImage = ConvertToIPicture(hBitmap)
    GdipDisposeImage hGdiImage
    End If
    GdiplusShutdown hGdiPlus
    End If

    End Function

    Public Function ConvertToIPicture(ByVal hPic As Long) As IPicture

    Dim uPicInfo As PICTDESC
    Dim IID_IDispatch As GUID
    Dim IPic As IPicture

    Const PICTYPE_BITMAP = 1

    With IID_IDispatch
    .Data1 = &H7BF80980
    .Data2 = &HBF32
    .Data3 = &H101A
    .Data4(0) = &H8B
    .Data4(1) = &HBB
    .Data4(2) = &H0
    .Data4(3) = &HAA
    .Data4(4) = &H0
    .Data4(5) = &H30
    .Data4(6) = &HC
    .Data4(7) = &HAB
    End With

    With uPicInfo
    .Size = Len(uPicInfo)
    .Type = PICTYPE_BITMAP
    .hPic = hPic
    .hPal = 0
    End With

    OleCreatePictureIndirect uPicInfo, IID_IDispatch, True, IPic

    Set ConvertToIPicture = IPic
    End Function
    [/vba]

    and load your picture like so

    [vba]

    Const PIC_PATH As String = "C:\Documents and Settings\xld\Desktop\"

    With UserForm1

    Set .Image1.Picture = LoadImage(PIC_PATH & "32x32 Insight.png")
    .Show
    End With
    [/vba]
    Last edited by Bob Phillips; 02-11-2010 at 06:29 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2007
    Posts
    15
    Location
    Thanks you so much , but i could do it with the wmf format , and it works really good , no matter what background color your user has its working with really good clarity.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    All that good work XLD, and.....
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •