Board index » delphi » A Fading hope for fading out a bitmap

A Fading hope for fading out a bitmap

Hello,

My hopes of getting a fade effect to work have faded away!  I'm trying
to alter the palette of a bitmap stored in a TImage container in order to
achieve a "Fade Out" effect.  The For loops gradually darken the original
RGB values and copy them to a Fade array (of TPallet{*word*249}try).  I then
want to use AnimatePalette() API Call to set the image's current palette
to the slightly darker Fade palette.  The code executes without compile
time errors or run time errors,
but it doesn't cause the bitmap image to fade!  What am I doing wrong
here??

Thanks in advance,

Dave Irizarry

***** Source Code *****

The form contains a TImage object with a picture loaded and a button which
begins
the Fade effect (or is supposed to do so!)

unit Fader;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Img1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    MyPalette, Fade: Array [0..256] of TPalet{*word*249}try;  //Pallete Structure
arrays
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  BmpPal: HPALETTE;
  i,j,k: integer;

begin

  BmpPal:=Img1.Picture.Bitmap.Palette;   //get palette handle from TImage
object
  i:=GetPalet{*word*249}tries(BmpPal,0,255,MyPalette);  //copy palette entries to
MyPalette

  //Fade out image by slightly darkening RGB values

  for k:=9 downto 0 do begin
     for j:=0 to i-1 do begin
        Fade[j].peRed:= ((MyPalette[j].peRed)*k) div 10;
        Fade[j].peGreen:= ((MyPalette[j].peGreen)*k) div 10;
        Fade[j].peBlue:= ((MyPalette[j].peBlue)*k) div 10;
        Fade[j].peFlags:= PC_RESERVED;
     end;

     AnimatePalette(BmpPal,0,i,@Fade[0]);   //Copy Fade palette structure to
Picture
     Img1.Invalidate;                                           //force
redraw of Img1

   end;
end;

end.

 

Re:A Fading hope for fading out a bitmap


Quote
David C. Irizarry wrote:

[snip]  The code executes without compile

Quote
> time errors or run time errors,
> but it doesn't cause the bitmap image to fade!  What am I doing wrong
> here??
[snip]
>   for k:=9 downto 0 do begin
>      for j:=0 to i-1 do begin
>         Fade[j].peRed:= ((MyPalette[j].peRed)*k) div 10;
>         Fade[j].peGreen:= ((MyPalette[j].peGreen)*k) div 10;
>         Fade[j].peBlue:= ((MyPalette[j].peBlue)*k) div 10;
>         Fade[j].peFlags:= PC_RESERVED;
>      end;

>      AnimatePalette(BmpPal,0,i,@Fade[0]);   //Copy Fade palette structure to
> Picture
>      Img1.Invalidate;                                           //force
> redraw of Img1

>    end;
> end;

> end.

Not sure, but since your .invalidate is within a tight loop, you may
need to also call application.processmessages within that loop to allow
your app. to perform the update *requested* by .invalidate ...
invalidate does not *force* a redraw but  rather requests it...
HTH
peter hill

Other Threads