Working with images in VBA - Displaying PNG files

Dan_W

Well-known Member
Joined
Jul 11, 2018
Messages
1,878
Office Version
  1. 365
Platform
  1. Windows
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:
VBA Code:
Function LoadImage(ByVal Filename As String) As StdPicture
        With CreateObject("WIA.ImageFile")
                .LoadFile Filename
                Set LoadImage = .FileData.Picture
        End With
End Function
You can then load a PNG into, say a Userform, with something like:


VBA Code:
Private Sub UserForm_Initialize()
        Me.Picture = LoadImage("D:\SAMPLE.PNG")
End Sub
You could even rename the function as 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 Docs

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):
1670521283275.png


 
@Dan_W , great to see this all in 1 place! 😊 Thanks for publishing!

Just for future reference, here is an api method for converting the bytes of an image DIB into a StdPicture w/o needing to save the bitmap to disk.
Still useful having these implementations as they are likely easier to port to Mac 😊 At least I imagine Mac doesn't have WIA.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
@Dan_W , great to see this all in 1 place! 😊 Thanks for publishing!


Still useful having these implementations as they are likely easier to port to Mac 😊 At least I imagine Mac doesn't have WIA.
Thank you!
But... "all"? Oh no...no no no... this isn't 'all' of it... lol. I started (but didn't finish) Part 2 here, but got distracted on a related animated GIF issue. It's been fun exercise though. I've certainly been learning more about in the process of writing it all down.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top