I'm looking into GDI+ in Delphi, and found the demo below. (Can't remember where.)
It's quite straightforward, and works fine on the first click: a series of circles is drawn side by side on a classical Delphi TImage and a GDI+ controlled TImage.
Subsequent clicks, however, do produce new circles in Image1, but not in Image2 (the GDI+ image). What's happening here?
procedure TFormMain.Button1Click(Sender: TObject);
var
I, X, Y, R: Integer;
graphics: TGPGraphics;
SolidPen: TGPPen;
SolidBrush: TGPBrush;
begin
graphics := TGPGraphics.Create(Image2.Canvas.Handle);
SolidPen := TGPPen.Create(MakeColor(255, 0, 0, 0), 3);
SolidBrush := TGPSolidBrush.Create(MakeColor(255, 0, 0, 0));
try
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SolidPen.SetWidth(3);
Image1.Canvas.Pen.Width := 3;
for I := 1 to 1000 do
begin
R := Random(100);
X := Random(700);
Y := Random(1000);
Image1.Canvas.Ellipse(X-R, Y-R, X+R, Y+R);
(SolidBrush as TGPSolidBrush).SetColor($80000000 or Random($1000000));
graphics.FillEllipse(SolidBrush, X-R, Y-R, 2*R, 2*R);
graphics.DrawEllipse(SolidPen, X-R, Y-R, 2*R, 2*R);
end;
finally
SolidBrush.Free;
SolidPen.Free;
graphics.Free;
end;
end;