1

I am trying to make a circular image by using the next:

var
  bmp: TBitmap;
  Rgn: HRGN;
  R: TRect;
  ...
    
  R := Image1.ClientRect;
  bmp := TBitmap.Create;
  bmp.Assign(Graph);

  bmp.Canvas.StretchDraw(Image1.ClientRect, Image1.Picture.Graphic);

  Rgn := CreateRoundRectRgn(0, 0, Image1.Width, Image1.Height, Image1.Width, Image1.Height);

  Image1.Picture.Assign(nil);
  Image1.AutoSize := false;
  Image1.Stretch := false;
  Image1.Height := R.Bottom - R.Top;
  Image1.Width := R.Right - R.Left;
    
  Image1.Canvas.Brush.Color := clRed;
  Image1.Canvas.FillRect(Image1.ClientRect);
    
  SelectClipRgn(Image1.Canvas.Handle, Rgn);
  Image1.Canvas.Draw(0, 0, bmp);
  DeleteObject(rgn);
  Image1.Canvas.Brush.Style := bsClear;

  Image1.Picture.Bitmap.TransparentColor := clRed;
  Image1.Picture.Bitmap.Transparent := True;
  Image1.Transparent := True;
  bmp.Free;

But the result is not satisfactory, as the edges appear jagged and not smooth.

image

How can make these edges smooth?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Issam
  • 133
  • 8
  • 1
    What you are looking for is anti-aliasing, but clipping a drawing with a region doesn't support that. – Remy Lebeau Sep 27 '22 at 00:34
  • I found a way to acheive the result by drawing an arc around the cliped area using the TGPGraphics with SmoothingModeAntiAlias as SmoothingMode .but not sure that is the right way. – Issam Sep 27 '22 at 01:01
  • 1
    The "best" or "right" way depends entirely on your context. But generally speaking, if you want to do nice, modern graphics in a VCL application, you should abandon GDI (and GDI+) and instead use Direct2D. For instance, I used Direct2D [in this answer](https://stackoverflow.com/a/64867049/282848) precisely because I couldn't stand the non-AA circles that GDI produced. – Andreas Rejbrand Sep 27 '22 at 08:39

1 Answers1

1

I found this way to handle the issue , wish it will helps the others :

var
GPGraph: TGPGraphics;
GPen: TGPPen;
GRect: TGPRect;
...

GRect := MakeRect(0, 0, Image1.Width-2, image1.Height-2);
              GPGraph := TGPGraphics.Create(image1.Canvas.Handle);
              GPGraph.SetSmoothingMode(SmoothingModeAntiAlias);

              GPen := TGPPen.Create(aclWhite, 2);
              GPen.SetStartCap(LineCapRound);
              GPen.SetEndCap(LineCapRound);
              GPGraph.DrawArc(GPen, 0, 0, Image1.Width-2, image1.Height-2, 0, 360)  ;
              GPen.Free;
              GPGraph.Free;

enter image description here

Issam
  • 133
  • 8