2011-11-16 64 views
0

我想用UserControl控件上的畫筆繪製。我可以繪製線條,圓圈和矩形。我不完全明白爲什麼我不能用畫筆畫畫。下面的代碼讓我只指向MouseDown,然後它移動到MouseUp中設置的位置。在MouseMove中沒有繪製內容。我想我不明白這裏的一些基本規則。在UserControl上用畫筆繪圖

此代碼適用於行:

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

此代碼,我trygin爲刷適應:

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

由於代碼呈現我試圖在點狀線條繪製之前和之後抓取位圖圖像。我應該以其他方式做嗎?

回答

2

我真的不明白你的問題,但在你的第二個代碼中他們似乎是一個錯誤。也許你應該試試這個:

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

否則,你正在繪製位圖本身的位圖。希望這可以幫助。

+0

我幾乎可以肯定有一個非常簡單的錯誤。感謝您的意見,是的,工作。對不起,我非直觀的代碼粘貼。 – qlf00n