柔和边缘圆形图像

eeq64g8w  于 2022-10-16  发布在  其他
关注(0)|答案(1)|浏览(178)

我正试着用下一个来做一个圆形的图像:

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;

但效果并不令人满意,因为边缘看起来参差不齐,不光滑。

如何才能使这些边缘变得光滑?

5anewei6

5anewei61#

我找到了这样的方法来处理这个问题,希望它能帮助其他人:

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;

相关问题