How do I draw a shape on a Canvas with Opacity?

Hi all,

I am trying to draw a Polygon on a Form using the following:
Canvas.Polygon(Poly);  // Where Poly is an array of TPoint

and I would like to set the opacity level (there is an image on the background)

Any ideas?
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Emmanuel PASQUIERFreelance Project ManagerCommented:
You can do that with GDI+. I searched a bit I don't think there is another way.

=> so basically it's just a matter of setting the Alpha value of the color (fourth byte in a 32 bits color value) you will use to fill your polygon

Now, these are not Delphi Examples... That's a bit tough to use GDI+ with Delphi.
There are some libraries to deal with it, depending on your Delphi versions.

for D2009, 2010 you can try this one :

you can also try this one that should work with any Delphi version. I have not tested this one much but I think it's used by some people around :

With both, you will use TGPGraphics.FillPolygon Method. For example :
GPGraphics:=TGPGraphics.Create(Canvas); // there are many different ways to create this object : from a canvas, a handle, an image etc...

aColor:=AlphaTransp SHR 24+ColorToRGB(DelphiColor);
 // AlphaTransp = 255=> not transparent , =128 => 50% transparent fill etc...
aBrush:=TGPSolidBrush.Create (aColor);
GBGraphics. FillPolygon ( aBrush, Polygon, FillModeAlternate);
Emmanuel PASQUIERFreelance Project ManagerCommented:
oups, mistake on the ARGB color calculation :

aColor:=AlphaTransp SHL 24+ColorToRGB(DelphiColor);

SHL, not SHR
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks for the info...
I am using Delphi 2009, so the GDIPlus looks ideal!
I have downloaded the files and am trying to create a simple form that draws a semi-transparent ellipse, but I have a problem! I have the following code, but it fails to draw anything - I assume there is something wrong with the assignment of Canvas -> IGPGraphics
Any idea what I am doing wrong?
Thanks, James
procedure TForm1.FormCreate(Sender: TObject);
    Grap            : IGPGraphics;
    SemiTransBrush  : IGPSolidBrush;

    Grap := TGPGraphics.Create(Canvas.Handle);
    SemiTransBrush := TGPSolidBrush.Create(TGPColor.Create(128, 0, 0, 255));

    Grap.FillEllipse(SemiTransBrush, 10, 10, 100, 100);
    Grap.FillEllipse(SemiTransBrush, 50, 50, 150, 150);

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

You should draw in the onpaint event, and not in the oncreate

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Emmanuel PASQUIERFreelance Project ManagerCommented:
it's not a good place to draw something on your form canvas at its creation. It is not yet shown.
You should always try to avoid drawing on the form's canvas, everything you draw directly will be cleared if you hide your window and redisplay it, because neither Windows nor your application know how to re-draw what you did.

First , just to see that GDI+ is working as you wish, put that code on an onClick event of some button.

Then : Use TImage, set a bitmap to it with the needed size, and start drawing on this bitmap canvas.

procedure TForm1.FormCreate(Sender: TObject);
  b: TBitmap;
 b := TBitmap.Create;
 img1.Picture.Assign( b );
 FreeAndNil( b );
 Grap := TGPGraphics.Create(img1.Picture.Bitmap.Canvas.Handle);
 SemiTransBrush := TGPSolidBrush.Create(TGPColor.Create(128, 0, 0, 255));
 Grap.FillEllipse(SemiTransBrush, 10, 10, 100, 100);
 Grap.FillEllipse(SemiTransBrush, 50, 50, 150, 150);

Open in new window

Emmanuel PASQUIERFreelance Project ManagerCommented:
@MvanderKooij: yes, onPaint would work as well
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.