【问题标题】:Drawing with brush on UserControl在 UserControl 上使用画笔绘图
【发布时间】:2011-11-16 18:59:00
【问题描述】:

我正在尝试在 UserControl 控件上使用画笔进行绘制。我可以画线、圆和矩形。我不完全明白为什么我不能用画笔画画。下面的代码只让我指向 MouseDown,然后它移动到 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);
  }
}

正如代码所示,我试图在点状线条绘制之前和之后抓取位图图像。我应该用其他方式吗?

【问题讨论】:

    标签: c# winforms graphics


    【解决方案1】:

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

    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);
        }
    }
    

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

    【讨论】:

    • 我几乎可以肯定这是一个非常简单的错误。感谢您的意见,是的,这有效。抱歉我的代码粘贴不直观。
    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2012-12-30
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多