DrawEllipse on Bitmap 和 return 带有椭圆的位图

DrawEllipse on Bitmap and return the Bitmap with the ellipse on it

我正在使用 Graphics 绘制一个椭圆,我想 return 一个包含该椭圆的位图。 我的代码有意义吗?

    private static Graphics ChipCanvas = Graphics.FromImage(new Bitmap(60, 60));
    
    public override Bitmap GetImage()
            {
              
                if (Color == PieceColors.Black)
                    ChipCanvas.FillEllipse(Brushes.Black, 0, 0, 5, 5);
                else
                    ChipCanvas.FillEllipse(Brushes.White, 0, 0, 5, 5);
    
                

                return new Bitmap(60, 60, ChipCanvas);
            }

不,不是。图形对象不是持久的 canvases。他们画了就忘记了。位图是 canvas.

这样试试:

private Bitmap bitmap = new Bitmap(60, 60);

public Bitmap GetImage()
{
  using (var g = Graphics.FromImage(bitmap))
  {
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillEllipse(Brushes.Black, 0, 0, 5, 5);
  }
  return bitmap;
}