在UserControl上使用画笔绘图 [英] Drawing with brush on UserControl

查看:134
本文介绍了在UserControl上使用画笔绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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);




$ b $ p
$ b

由于代码提供了我想抓取位图图像点状线条画前后。我应该这样做吗?

解决方案

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

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






$ b

否则,你在位图本身上绘制位图。希望这有助于。


I am trying to draw with brush on UserControl control. I can draw lines, circles and rectangles. I don't exactly understand why I cannot draw with brush. The code below gives me just point on MouseDown and then it moves to the position set in MouseUp. There is no content drawn during MouseMove. I suppose that I do not understand some basic rule here.

This code works for lines:

public override void Draw(Graphics graphics) {
  graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  graphics.DrawLine(new Pen(this.Color, this.PenSize), startPoint, endPoint);
}

This code I am trygin to adapt for brush:

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

This code repaints list of objects:

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

As the code presents I am trying to grab bitmap image before and after point-like line drawing. Should I do it other way?

解决方案

I don't really understand your question, but in your second code their seems to be a mistake. Maybe you should try this one:

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

Otherwise, you are drawing the bitmap on the bitmap itself. Hope this helps.

这篇关于在UserControl上使用画笔绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆