Save a TGradient to TBitmap

Post here your experiences, comments, suggestions, or bug reports regarding to the DELPHI AREA products. Not-matching topics will be moved or deleted.

Save a TGradient to TBitmap

by zizzo81 » 22/10/09 06:37

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
User avatar
zizzo81
Member
Member
 
Posts: 2
Joined: 22/10/09 06:28
Location: Italy

Re: Save a TGradient to TBitmap

by Kambiz » 22/10/09 07:32

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);
Kambiz

Donate a cup of food for free: Click to Give @ The Hunger Site

Kambiz
Administrator
Administrator
 
Posts: 1868
Joined: 07/03/03 19:10
Location: Tehran, Iran

Re: Save a TGradient to TBitmap

by zizzo81 » 22/10/09 07:47

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
User avatar
zizzo81
Member
Member
 
Posts: 2
Joined: 22/10/09 06:28
Location: Italy

Re: Save a TGradient to TBitmap

by Kambiz » 22/10/09 23:01

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;
Kambiz

Donate a cup of food for free: Click to Give @ The Hunger Site

Kambiz
Administrator
Administrator
 
Posts: 1868
Joined: 07/03/03 19:10
Location: Tehran, Iran


Return to DELPHI AREA Products

Who is online

Users browsing this forum: MSN [Bot], Yahoo [Bot] and 0 guests