C# 在用户控件上使用画笔绘制

C# 在用户控件上使用画笔绘制,c#,winforms,graphics,C#,Winforms,Graphics,我试图用画笔在UserControl上画画。我会画线、圆和矩形。我不太明白为什么我不能用画笔画画。下面的代码给了我鼠标向下的点,然后它移动到MouseUp中设置的位置。在MouseMove期间没有绘制内容。我想我不明白这里的一些基本规则 此代码适用于以下行: public override void Draw(Graphics graphics) { graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAli

我试图用画笔在UserControl上画画。我会画线、圆和矩形。我不太明白为什么我不能用画笔画画。下面的代码给了我鼠标向下的点,然后它移动到MouseUp中设置的位置。在MouseMove期间没有绘制内容。我想我不明白这里的一些基本规则

此代码适用于以下行:

public override void Draw(Graphics graphics) {
  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  graphics.DrawLine(new Pen(this.Color, this.PenSize), startPoint, endPoint);
}
我正在尝试为笔刷调整以下代码:

public override void Draw(Graphics graphics) {
  if (this.bitmap != null) {
    graphics = Graphics.FromImage(this.bitmap);
    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    graphics.DrawEllipse(new Pen(this.Color, this.PenSize), startPoint.X, startPoint.Y,
                         this.PenSize, this.PenSize);
    graphics.DrawImage(this.bitmap, 0, 0);
  }
}
此代码重新绘制对象列表:

private void UserControl_Paint(object sender, PaintEventArgs e) {
  if (ObjectsList != null) {
    ObjectsList.Draw(e.Graphics);
  }
}

正如代码所示,我正在尝试抓取点之前和之后的位图图像,如线条绘制。我应该换一种方式吗?

我不太理解你的问题,但在你的第二段代码中,它们似乎是一个错误。也许你应该试试这个:

public override void Draw(Graphics graphics)
{
    if (this.bitmap != null)
    {
        Graphics g = Graphics.FromImage(this.bitmap);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        g.DrawEllipse(new Pen(this.Color, this.PenSize), startPoint.X, startPoint.Y, this.PenSize, this.PenSize);
        graphics.DrawImage(this.bitmap, 0, 0);
    }
}

否则,将在位图本身上绘制位图。希望这能有所帮助。

我几乎可以肯定有一个非常简单的错误。谢谢你的投入,是的,这很有效。很抱歉我的非直观代码粘贴。