Delphi: image with alpha blending capabilities - delphi

Is there some simple component that could display PNG 32-bit images and alpha-blend it to another image on mouse enter and on mouse leave? Or even just a simple image that can load 32-bit PNG and additionally would have Alpha parameter...
I suppose I could use some skin library (alphacontrols?) or some graphics library, but for some simple purely visual effects that would be quite an overkill and I can't seem to find a simple one that does it for me and would work on Glass surface as well...

It is not quite a component, but here is a link to a good article on how to do it yourself. Its just a few lines of code:
http://melander.dk/articles/alphasplash/

The basic TImage is capable of displaying a PNG image and alpha-blend-it to whatever is behind it. You just need to add the "pngimage" unit to the uses clause of your form. You didn't mention the version of Delphi you're on: Delphi 2010 has this unit, but I have no idea with what version of Delphi they started shipping it. For Delphi 7 I know for sure you'll need to find the open source "pngimage".
If you need to do this when the user moves in and out of your control, consider caching the state images in bitmaps: it will be faster at runtime and you can use a single TImage that shows the current image, you don't need two overlapping TIMage controls.

Related

Windows Preview Handler using FMX drawing functions?

I have created an FMX graphics application that draws various graphics from a custom binary vector file format. I'd now like to create a Windows Preview Handler to support that file format. I have already created a basic VCL preview handler based on the code found here. Some minor fixes were needed to get it to build in Delphi 10.4. I have also added a TPaintBox that I can draw to using the VCL TCanvas drawing functions.
The problem is that all the drawing for my file format is using FMX and uses several functions that don't exist in VCL, such as colour gradients and opacity. Is it possible to somehow mix in FMX TCanvas code for the preview handler?
If not then I have another option, but I'm not sure how well it would work. I would add functionality to the FMX application such that executing it with the appropriate input parameters (file path and dimensions) would generate an image file. The preview handler would just need to call it and load the image to display it. This would mean that the preview handler could be VCL only and would also reduce code size by not replicating the drawing code, but there may be latency issues.
I could also potentially use GDI+ or Direct2D in a VCL application since it would also support the advanced drawing functions, but it would mean rewriting all the drawing functions and I'm not sure if it would look exactly the same.
Any ideas are welcome. Thanks.

GIF to AVI with windows Animate Control

I was trying to convert a GIF image (Ajax waiting like):
to AVI that will be used with TAnimate control (win32 animation control).
I have tried a few tools, but the problem is that the output AVI is either not suitable for the Animation control or not transparent.
Notes:
I don't use windows XP Themes manifest (D5).
the Common Avi's are transparent even if my program is not themed.
Using the GIF itself to show animation is not an option.
Do you know any tool that can help me with this conversion?
EDIT:
I have tried using ImageMagic with ffmpeg as described here. but I can't seem to set ffmpeg to encode to msrle.
Jasc Animation Shop is able to directly read an animated GIF and write out a compressed AVI. This is an old program that appears to be no longer offered by Corel (who acquired Jasc), but you may still be able to find the free trial version for example.
You could try the TGIFImage component (written for Delphi 5), which claims not only to support Animated GIFs, but also Transparency, as well as a means of converting a GIF to an AVI (and vice-versa).
Whether it does all that or not I cannot say as I haven't personally tested it. Like TLama mentioned in the OP comments above, I use my own means of playing each frame in an animated fashion (only mine uses a RES file and a TImage, where each frame is stored as a PNG for full semi-transparency support).
Many commercial tools do this conversion, including Corel Photo Paint, and others. A free trial is available for Corel Photo Paint, which comes included in the Corel Draw package. (Warning: It's huge, and expensive.) I don't think the transparency will be solved any more by corel photo paint's conversion than by anybody else.
Typing some words into Google, also reveals that DSPack from ProgDigy might help you.
The last time I did this, I abandoned the attempt to convert GIF transparency into AVI-with-transparency, because no AVI-encoding tool that I could find would preserve transparency. Thus the solution of leaving it as a gif, and putting a new control into your app (as LaKraven suggests) may be superior in the end, to fighting it.
I would suggest using GDIPlus native image renderer which can easily handle animated GIFs as well as many other image types, including multi-frame TIFFs (no MNG so far). I was able to find an example component which animates TImage instance:
http://www.progdigy.com/forums/viewtopic.php?p=13156
Please note I am aware that you are looking for a solution to AVI transparency, this is just an alternative way of presenting user with a "progress", which seems to be more lightweight (no dependency on windows media playback facilities) and flexible (such animation controller can be used on any image).

Options for pure-graphics display in Delphi 2009

My whole experience with direct use of graphics consists of drawing shapes using Turbo Pascal’s gdi unit on a 386 machine ages ago, finding it unbearably slow and never giving it a second thought. In other words, I have next to no idea where to begin.
For a simple Internet radio player application, I would like to design a graphic display somewhat similar to Winamp (but a bit larger and easier on the eyes, since the illegibility of such displays is one reason I’m trying to do my own).
Simple graphic components like those imitating LED displays are nowhere near sufficient, of course. I don’t expect to be drawing clickable UI controls, and certainly not skins – just the readout, with text, digits and a few symbols. I understand Delphi 2010 supports Direct2D, but I only have D2009.
What are my options? Are there any 3rd party components that would help?
On edit two small points. I need the drawing to be flicker-free (i.e,, unlike what I experienced all those years ago in Turbo Pascal :-). Is TCanvas going to be fast enough for that? Also, I would probably want to use alphablending, which I don't think I can get with the basic TextOut, LineTo etc. graphics API. (I just don't know what's possible). What about GDI+?
A library for fast 2D graphics for Delphi is available as open source on Sourceforge:
Graphics32 (home page: http://www.graphics32.org/)
Graphics32 is a graphics library for
Delphi and Kylix/CLX. Optimized for
32-bit pixel formats, it provides fast
operations with pixels and graphic
primitives. In most cases Graphics32
considerably outperforms the standard
TBitmap/TCanvas methods.
Features Some of Graphics32 features include:
Fast per-pixel access up to 100 times faster compared to standard TBitmap;
High-performance Bitmap alpha blending (including per-pixel alpha blending);
Pixel, line and polygon antialiasing with sub-pixel accuracy (combined with alpha blending);
Arbitrary polygon transformations and custom fillings;
Bitmap resampling with high quality reconstruction filters (e.g. Lanczos, Cubic, Mitchell);
A unique state-of-the-art rasterization system;
Affine transformations of bitmaps: rotations, scaling, etc with sub-pixel accuracy;
Arbitrary projective transformations of bitmaps;
Arbitrary remapping transformations of bitmaps (e.g. for Warping, Morphing);
Flexible supersampling implementation for maximum sampling quality;
Flicker-free image displaying components with optimized double buffering via advanced MicroTiles based repaint optimizer;
Multiple customizible easy-to-use overlay layers;
Locking of bitmaps for safe multithreading;
A property editor for RGB and alpha channel loading;
Design-time loading of image formats supported by standard TPicture;
Works on Borland Delphi, C++ Builder and Kylix (The last version that supported Kylix was 1.8.3).
As of version 1.5.1b Graphics32 is licensed under the terms of the Mozilla Public License.
Take a look at TCanvas. It's built into most visual controls and contains easy methods for the sort of simple drawing you're looking at, without the need for mucking around with DirectX or OpenGL. If you want a really simple surface you can draw on, place a blank TImage TPaintBox on the form and use its Canvas as your drawing surface.
Mostly I agree with Mason and the comments. TPaintBox is a very simple component really. You implement the OnPaint event and are in complete control. It is fast enough on a fast-enough machine.
Put it on a TScrollBox and you don't even need to worry about where the user has scrolled to :)
Make sure you check out the DoubleBufferred property of the parent control(panel/ScrollBox).
If it is a complex drawing/graphic also check out InvalidateRect - this controls which area of a canvas is redrawn.
Of course like all graphics/drawing it gets pretty complicated pretty quickly :(
Just saw your edit. If you need alpha blending and flicker-free drawing, TCanvas is probably not going to be good enough for you. You should look at components that wrap OpenGL or Direct3D and provide you a high-power graphics canvas.
One such package is Asphyre, though I'm not sure if they have a version that works on Delphi 2009+ yet. I've also got a custom-built library that wraps SDL 1.3 and gives you a drawing frame component you can place on a Delphi form. It does work on Delphi 2009 and later, but I haven't released it as its own package yet. You could find it if you dig around in the SVN archive of my game engine, but if you can wait a day or two I could try to get it made into an easily-usable BPL component package and available for download.

Ugly "disabled" images of TMainMenu

Delphi 2010, Win32 VCL. I need to create a main menu for my application with the most standard look. TMainMenu looks nice in all Windows versions, the only bad thing is when it displaying images for items with Enabled=False.
Delphi has some strange method for building that disabled images (b/w extrusion with bevels, for me it looks like spew). I want to make a patch for that procedure in VCL sources and let menu to select disabled images from the same TImageList, but I can't find it.
Is it exists at all or it is some standard windows API call? Do you know where is that procedure located?
Sure OwnerDraw style can fix all this but I want a bit faster solution.
There's a great post on Galfar's Lair blog which describes the problem, and has code for a workaround, overriding the TImageList DoDraw method.
Here's the pic (linked) showing the 'before' and 'after'.
I think this relates to pretty much all Delphi versions.
If you can provide your images as PNG you can use TPngImageList from PngComponents. It has different options to make a blended or grayscaled disabled image. The PNG format allows you to make use of alphablending.

How to make the form into fully transparent 32bit alpha?

i use to create a full transparent form, but the png and the 32bit image will not blend to the form and images is not properly displayed its trnsparency. i get the concept here but i cannot put button on it. all abject are invisible except the image backgound. by the way my compiler is delphi7 and D2009
Seems that Andreas Hausladen stumbled upon a similar problem and has already implemented a solution: he rewrote TJvTransparentForm in the JVCL library.
Even if you are not going to use that component directly you may gain some insight by studying the source.
Here is the link to Andreas' article.
If you want to use a PNG to control transparency you might want to look into "layered windows":
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
UpdateLayeredWindow
This allows you to have variable transparency accross the window, just as PNG allows! The problem is, controls on layered windows don't really work, I guess it's a Windows issue. My solution was to actually brake up the big window into multiple smaller windows, with all the controls on non-layered windows. This creates yet an other problem, because you now have multiple windows and you want them to move like a single window. The solution: implement your own algorithm for moving the window accross the screen and use:
BeginDeferWindowPos
DeferWindowPos
EndDeferWindowPos
... to move all windows at once, so the user has no idea she's looking at multiple windows! Finally, if you want to get fancy, you might want to look into SetWindowRgn: this allows you to create a window with a non-rectangular shape.
P.S: Reading Andrea's link from Uwe Raabe, he's using the same technique, only he packaged it ready for action!

Resources