# VBForums UtilityBank > UtilityBank - Utilities >  Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

## dilettante

Version 1.1
02-December-2015
Robert Riemersma

This is a small GUI utility intended to help you pre-process small PNG format images that have alpha channel transparency and translucency.  Additional work may need to be done on the output saved by this program using another more generalized image editing tool.

Often programmers using classic Visual Basic need small bitmap images for use in making icons and images for use with common Visual Basic controls.  But many times they can find free images that might be useful except for being alpha-PNG images.  Instead what is needed is "maskcolor" transparency, or in some cases GIF-transparency will do.

This program can help iron out or "flatten" alpha-PNG images against a selected maskcolor and save this composited image to disk as a new PNG file.  Such a flat PNG can be easier to work with using other tools to make a final product.

But PngIron is a one-trick pony, not a general purpose graphics tool.  Most versions of Windows Paint can be valuable in finishing up, and other tools like the popular IrfanView can help with this stage too.


*Requrements, Installing PngIron*

PngIron consists of a single EXE.  You can copy this to any folder and create shortcuts as needed to run it.

There are no extra DLLs or OCXs to install.  However it does make use of WIA 2.0 and Shell32 4.71 or later.

This means it requires Windows Vista or later.  If you have a machine with Windows 2000 SP4 or Windows XP SP1 or later and it has WIA 2.0 installed then it should work there as well.  However Microsoft no longer provides the necessary DLL for installation into those unsupported systems.  Quote from MSDN:




> *Developer audience*
> 
> The WIA Automation Layer API is designed for use by Microsoft Visual Basic 6.0, Active Server Pages (ASP), and scripting programmers.
> 
> *Run-time requirements*
> 
> Applications that use the WIA Automation Layer API require Windows Vista or later. Earlier versions of Windows are not supported.



*Compiling PngIron*

Nothing special is required here.  You just need Visual Basic 6.0 (Professional Edition or better) installed on a Windows Vista (or later) PC.  It can also be compiled under Windows XP SP1 to SP3 and Windows 2000 SP4 as long as you have the WIA 2.0 SDK installed there.


*What PngIron Does*

First you open an existing alpha-PNG file, which Pngiron will display.  If the file has no alpha information PngIron will let you know and things end there.

Otherwise you can:

Set a maskcolor.Choose an alpha threshold level for flattening that eliminates fringing and shadows as desired."Knock out" stray pixels by replacing them with the maskcolor or selected paintcolors.Reduce the color count via a simple algorithm.

You can fiddle with the various settings until the image is clean enough for use.  Then you can save it as a new PNG file.


*Limitations*

PngIron does not save in BMP format because it is bulky.  It does not save as GIF because it doesn't have enough control of the image.  Save as GIF would often end up with "dithered" colors.

Many operations such as a change to the alpha threshold can be slow due to the way PngIron is working with these images.  You will want to avoid working with images much larger than 256x256 pixels - they work but things can slow down quite a bit as you make changes.

No attempt has been made to deal with DPI settings higher than the standard 96dpi display setting.

With more effort PngIron could be modified to reduce or eliminate these limitations.  If you need to do a lot of this you might want to consider more generalized and more powerful image editing tools.


*License*

Microsoft Public License (Ms-PL)

This is a very permissive license.  See the ReadMe.txt in the attachment for full details.


*Attachment*

This containts the precompiled binary PngIron.exe, ReadMe.txt file, and the entire VB6 source for compiling PngIron yourself.

----------


## dilettante

Example.

Here's an image that has two small pictures I might want to use to make +/- "zoom" buttons.  Sadly there is some "fringing" we need to clean up before these will be very useful:



Setting the alpha flattening threshold to D0 cleans things up a little bit, though less so in this case than perhaps for other images:



We can use the very primitive pixel-editing feature in PngIron to "knock out" a few ugly pixels and "bang in" a few others here and there:

----------


## dilettante

We can take that a step further.

Here I have knocked out a couple more pixels and then used the color reduction feature of PngIron:



In this case we have dropped the color-count to merely 46 unique colors.

Once we save that file, we find we have a PNG that still has alpha channel information but for all pixels it is set to "solid" (FF).  This makes it easier to work with in another graphics tool to cut out what you need, reduce the color depth, and save as a GIF or small BMP.

----------


## dilettante

Here's another example that has a shadow effect:



We can set the threshold to 01 and apply color reduction to get a decent flat image.  Here we only have 223 unique colors left, which makes for a nice 8-bit GIF or BMP you can create from it using another tool:




You may note that the bottom "status" area has changed since the previous example's screenshots.  PngIron 1.1 added more information, namely when you "save" it shows the save file's name below the "open" file name now.

----------


## dilettante

It would be possible to make another command-line or batch program based on the part of PngIron that does the heavy lifting: the Masker class.  You could create a simple batch script language to specify the open and save file names or even folders and set things like the maskcolor, flattening threshold, and color reduction level.

In case it is helpful here's a block diagram showing the high level flow within Masker:

Attachment 132811

----------


## dilettante

Here is the compiled PngIron 1.1 binary packaged using a reg-free COM manifest along with the redist version of wiaaut.dll.  No changes in the program source at all.


While deployment now means copying both the EXE and its accompanying "bin" folder to a destination folder, PngIron can thus be used on WinXP SP1 or later and probably works on Win2K SP4 as well.

Oddly enough... the slow operations seem to run _significantly faster_ than when using the system copy of wiaaut.dll on Vista and later!

Tested on WinXP SP3, Vista SP2, and Win7 SP1.


_Edit:_

In fact it runs _incredibly faster_ using the redist DLL.  _Amazingly_ faster!

I opened a 1000x1000 PNG with transparency and working with it was nearly impossibly slow using the system DLL under Vista SP2.  On the same PC running this reg-free COM version using the redist DLL barely shows the hourglass cursor at all.

----------

