Home > Software engineering >  Soft edges Circular image
Soft edges Circular image

Time:10-01

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?

CodePudding user response:

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

  • Related