Although Excel supports the importing and exporting of PNG files, the image file format is not easy to work with in native VBA, especially when it comes to display PNG images in Userforms/Userform Controls. One option that you may already be aware of is Steve Bullen's PNG loading function using GDI+ (function load transparent PNG picture into userform) - which I reproduce at the linked thread (compatible here in both 32bit and 64bit Office), with further important corrections/input by Jaafar.
Another, less code-intensive option, however, is the WIA COM Object (Windows Image Acquisition). The WIA COM Object has some useful functionality that is accessible from VBA. For some introductory infromation about WIA, see the MS website: Windows Image Acquisition (WIA) - Win32 apps
One very handy use of WIA is the following - it can be used to load any of the standard VBA-compatible image files, but also PNG files:
You can then load a PNG into, say a Userform, with something like:
You could even rename the function as
In short, you can display PNG files with transparency with the CheckBox, CommandButton, Label, OptionButton and the ToggleButton.
Here's an example of transparency support for bitmaps. Basically, the two images on the left are the original images. Upon transferring them through a simple assignment between the Image control on the left and the label control on the right, part of each image disappears (or, rather, becomes transparent):
Another, less code-intensive option, however, is the WIA COM Object (Windows Image Acquisition). The WIA COM Object has some useful functionality that is accessible from VBA. For some introductory infromation about WIA, see the MS website: Windows Image Acquisition (WIA) - Win32 apps
One very handy use of WIA is the following - it can be used to load any of the standard VBA-compatible image files, but also PNG files:
VBA Code:
Function LoadImage(ByVal Filename As String) As StdPicture
With CreateObject("WIA.ImageFile")
.LoadFile Filename
Set LoadImage = .FileData.Picture
End With
End Function
VBA Code:
Private Sub UserForm_Initialize()
Me.Picture = LoadImage("D:\SAMPLE.PNG")
End Sub
LoadPicture
, and force VBA to use this custom function as the go-to routine rather than the inbuilt (limited) routine without breaking existing code. The other benefit is that it retains alpha channel transparency, depending on the control you're loading it into.Transparency in Microsoft Forms
It may be that you're'already be aware of this, but certain userform controls support bitmaps transparency. You may have noticed that sometimes, when you load an image in the picture property of a label control, the background colour simply disappears - even if there is no alpha channel. Here's an article setting it out: Transparency in Microsoft Forms | Microsoft DocsIn short, you can display PNG files with transparency with the CheckBox, CommandButton, Label, OptionButton and the ToggleButton.
Here's an example of transparency support for bitmaps. Basically, the two images on the left are the original images. Upon transferring them through a simple assignment between the Image control on the left and the label control on the right, part of each image disappears (or, rather, becomes transparent):