Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1475

[vb6] Extending VB's Image Control for PNG, TIFF, and more [v1.1]

$
0
0
UPDATED on 11 Oct 2015. Major revision. Added animated GIF and mutlipage TIFF support.
Any previous version must be thrown away. You do not want to run both in the same project, trust me!
12 Oct 2015: Added more robust method of activating images at runtime from controls loaded at design-time. See post #11 for more

The VB Image control is now more flexible. Want to display PNG and TIFF without using a 3rd party control? It can do that. Want to display 32 bit alpha-blended bitmaps? It can do that. Want to animate GIFs and display nice 32 bit alpha-blended icons? Now it can ;)

What's even more amazing is that this isn't limited to the image control. These images can be displayed in most things with a Picture property -- not every Picture property, but most. Want to add a nice PNG to a button, yep it can be done. Regardless, true transparency is maintained, nothing faked. Change the backcolor of some container, and you don't have to reload the image to fake transparency again. Someone changes themes on the pc & it doesn't effect the transparency.

Major Limitations:
1. Cannot see transparency in bitmaps while in design-view.
2. Cannot load PNG,TIFF while in design-view

How this class works.
1. Converts non-icon/gif/metafile formats to 32bpp premultiplied RGB bitmap if image contains transparency. AlphaBlend API is used to render the image in that case
2. Icons/Cursors are loaded as-is and rendered with DrawIconEx API if icon/cursor is 32bpp
3. GDI+ is used to load PNG/TIF/Animated GIF and convert to 32bpp or 24bpp bitmaps for display
4. Only images that need special rendering are custom drawn
5. Subclassing of the picture is performed for two reasons: a) handle rendering as just described and b) tell VB that a picture contains transparency so VB will repaint behind the image before it's drawn.

This version of the class has a few active methods.

1. SetImage. This is a replacement for VB's LoadImage and Set methods that pertain to images. It allows unicode filenames and can support TIF, PNG, 32bpp bitmaps & icons/cursors, PNG-encoded Vista-style icons, plus what VB supports.

2. SetSubImage. If a multipage TIFF or animated GIF has been loaded with option chosen for navigation, this method changes the page/frame.

3. WantEvents. Method allows you to receive an event to enable you to draw behind or on top of any image

4. EnableSubclassing. Allows you to turn off subclassing. By default, subclassing is enabled. But since subclassing in IDE is not safe, it is recommended you disable subclassing when working on your project. Turn it on when you want to view the images with transparency for a quick look-see. Then close your project normally and disable subclassing as desired. Subclassing cannot be disabled when project is compiled. Once disabled during IDE run-time, cannot be re-enabled until project closes -- safety measure.

5. SubImageCount. Returns number of frames/pages if animated GIF or multipage TIFF loaded

6. GetGIFAnimationInfo. Returns the frame duration and loop count for animated GIFs

I have included a very simple sample project. The project's images you see in design view are one that is premultiplied and one that is not. Both are 32 bit bitmaps that use the alpha channel.

Screenshot below are two VB image controls. You can clearly see they are drawing blended alpha channels. Background is transparent, soft edges and shadows. The updated zip below does contain an animated GIF example.

Name:  Untitled.jpg
Views: 69
Size:  36.6 KB

Suggestion: GIF/TIFF animation/navigation is not disabled when EnableSubclassing = False. I feel it should be. GDI+ can cause crashes too in IDE if it is not shut down properly and hitting END prevents proper shutdown. If you agree, you will want to make this modification:

Routine: SetImage
Add the following before the line: Select Case PropertyName
Code:

If Me.EnableSubclassing = False Then RequiredImageType = RequiredImageType And Not lifAllowNavigation
Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 1475

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>