Page 1 of 1

Save a TGradient to TBitmap

PostPosted: October 22nd, 2009, 6:37 am
by zizzo81
Hi guys,
your TGradient is really cool, but it misses a function I think, the ability so save the actual bitmap of the gradient.
Is there any way to get it? Personally I tried a CopyRect, but it has a lot of problems.

Thank you
Christian Cristofori

Re: Save a TGradient to TBitmap

PostPosted: October 22nd, 2009, 7:32 am
by Kambiz
There is a protected property named Pattern that holds the gradient bitmap.

To have access to the Pattern property, use the following trick:

Code: Select all
TGradientHack = class(TGradient);

Image1.Picture.Assign(TGradientHack(Gradient1).Pattern);

Re: Save a TGradient to TBitmap

PostPosted: October 22nd, 2009, 7:47 am
by zizzo81
Thnk you, but all it gives me out is a single line bitmap (i'm using gsDiagonalLF as Style), if I try a radial one it gives me a square.

My goal is to make a big TGradient (for example 1024 x 768), to save it to a bitmap file, then set it as my desktop wallpaper... and I can't get this out even with your *hack* solution. :P

Thank you again
Christian

Re: Save a TGradient to TBitmap

PostPosted: October 22nd, 2009, 11:01 pm
by Kambiz
Actually there is no need to hack TGradient to get pattern. The CopyPatternTo method pt TGradient copies pattern in to a bitmap.

What you need is to stretch draw the pattern on your own bitmap. Do not pay attention to dimension of the pattern. The pattern's dimension is the minimum dimension required to hold the pattern information for 256 color level, and it depends on the selected gradient style.

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
var
  Pattern: TBitmap;
  Rect: TRect;
begin
  Pattern := TBitmap.Create;
  try
    Gradient1.CopyPatternTo(Pattern);
    SetRect(Rect, 0, 0, MyBitmap.Width, MyBitmap.Height);
    MyBitmap.Canvas.StretchDraw(Rect, Pattern);
  finally
    Pattern.Free;
  end;
end;