Save a TGradient to TBitmap

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

Save a TGradient to TBitmap

Postby zizzo81 » October 22nd, 2009, 6:37 am

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: 3
Joined: October 22nd, 2009, 6:28 am
Location: Italy

Re: Save a TGradient to TBitmap

Postby Kambiz » October 22nd, 2009, 7:32 am

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Save a TGradient to TBitmap

Postby zizzo81 » October 22nd, 2009, 7:47 am

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: 3
Joined: October 22nd, 2009, 6:28 am
Location: Italy

Re: Save a TGradient to TBitmap

Postby Kambiz » October 22nd, 2009, 11:01 pm

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 2 guests

cron