Tuesday, August 29, 2017

VB6 Support for PNG, TIF, GIF Animation

LaVolpe is literally a genius and a super-advanced programmer who has revolutionized VB6 graphics. It revolutionized the VB6 graphics so much that Microsoft with all their army pay close attention to his methods and style. Yes, that means VB6 always had (and has) better graphics than Microsoft's newer products. LaVolpe is one of the people to whom we (The VB6 community) owe our superiority over Microsoft in recent years.

Download from me

Download from source

Status: Updated 14 Dec 2015. Fix rendering bugs discussed in post #37
Updated 11 Nov 2015 to include support for non-placeable WMFs

We all know VB is old and doesn't support many of the common image formats, specifically: TIF, PNG and alpha-blended icons. The attached class enables VB to support those formats and a bit more. There is no usercontrol involved. Just a bit of subclassing (IDE safe) to enable the images to be rendered with APIs that do support the alpha channel: AlphaBlend and DrawIconEx. The class is not limited to just Image controls, can be applied to most (if not all) of VB's picture, icon, and other image properties.

Image formats supported. The 'includes' below are in addition to what VB supports:
:: BMP. Includes 32bpp alpha and/or premultiplied. Includes those created with v4 & v5 of the bitmap info header
:: GIF. Includes methods to navigate frames
:: JPG. Includes CMYK color space
:: ICO,CUR. Includes 32bpp alpha and/or PNG-encoded Vista-type icons
:: WMF. Includes non-placeable
:: EMF
:: PNG
:: TIF. Both single page & multi-page. Supported compression schemes depend on version of GDI+ installed
See post #8 below for tips regarding design-view usage

Important to remember. This is still a VB stdPicture object. All of its properties and methods are supported, including Width, Height, Type, Render, Handle, etc.

The class methods/properties can be categorized as follows:

I. VB Replacement Methods
- LoadPicture: Unicode supported. Has options to keep original image format when loading. This enables you to navigate animated GIF frames and multipage TIFs. Cached data allows you to save that original data to file
- SavePicture: Unicode supported. Has options to always save to bitmap or save original format if it exists
- PictureType: Returns the original image format, i.e., GIF, PNG, TIF, etc, if that format exists
note: LoadPicture & SavePicture both accept byte array as source/destination medium

II. Management Methods
- IsManaged: Return whether the class is rendering the image or VB is
- UnManage: Simply unsubclasses a managed image
- HasOriginalFormat: Return whether or not any Picture is caching original image format data

III. Navigation Methods
- SubImageCount: Returns the number of frames/pages contained within the managed image
- SubImageIndex: Returns the current frame/page displayed by the managed image
- SubImage: Returns a stdPicture object of the requested frame/page
- GetGIFAnimationInfo: Returns an array containing loop count and each frame's display interval for animated GIFs

Quickly. How does this class do it?

1. It creates thunks to subclass the VB picture objects. The source code of the thunks are provided in the package. These thunks draw the image using the APIs AlphaBlend or DrawIconEx. For you ASM gurus out there: I'm a noob with ASM. I'm sure others can rewrite that ASM code to be more efficient.

2. To support AlphaBlend API, images may be converted to a 32bit bitmap having the pixel data premultiplied against the alpha channel. These bitmaps will never display correctly outside of the class. Therefore the class' SavePicture function allows you to create a copy of the bitmap that can be displayed outside of the class. This copy can be passed to the clipboard and/or drag/drop methods of your project.

3. GDI+ is relied upon to parse/load TIF and PNG. It is also used to support JPG in CMYK format, non-placeable WMFs and multi-frame GIF rendering. GDI+ is a requirement for the class. If it fails to load or thunks fail to be created, the class will silently fall back to standard VB methods and VB restrictions.

The transparency displayed by the image control is not faked. It is true transparency and the attached test project should easily prove that. Important to understand. TIF and PNG support is not available at design-time. This is because the class code isn't activated until run-time. Some motivated individuals out there could easily create a windowless usercontrol that hosts an image control (and this class) that could support all formats at design-time. Just a thought and subtle prod.

The class can be expanded by those willing to put in the effort. Ideas would be to incorporate GDI+ high quality scaling, conversion from one image format to another, image effects like rotation, blur, and more. Other image formats can easily be supported from outside the class. If you can parse/render that new format to a 32bpp bitmap, then you can use the class' LoadPicture to display that image. Have fun.
Tip: If modifying the thunks or code, recommend identifying these new versions using a different key. Throughout the code, you can find a couple instances of this text: IPIC+Thunker. Change those instances to reflect something else, i.e., "IPIC+Thunker.v2" so you can distinguish between your versions.

When compiled, VB can behave differently vs when uncompiled. Some differences are subtle, others are not. Here's one that is key for animating GIFs. In the test project posted below, the animation works because VB caches the GIF format for the GIF that was loaded into Image1 during design-time. During run-time that info is still cached by VB so the class can extract the entire GIF. But when you compile the project, the GIF no longer animates. Why? Well, when compiled, the GIF information is lost. VB no longer caches it. This can be proven in a simple test project. Add an image control and button. Add a GIF or JPG to that image control. Add the following code behind the button. Click the button when project is running compiled and uncompiled. Different results. The workaround is simply to save GIFs you want to animate in a resource file and load the GIF from that.

Known Limitations:
1) VB-like scaling is in play. Did not spend the time to incorporate high-quality GDI+ scaling
2) Do not send any Picture object to the class if it was retrieved via Clipboard.GetData. Why? Well, on 32 bit screen displays, the alpha channel can be filled with garbage. This garbage will likely be misinterpreted as transparency information
3) If expecting to use the .Render method of the picture object (icons only), do not keep the icon picture object returned from this class as Managed. After loading the icon, call UnManage. See Post #37 for more


No comments:

Post a Comment