2009-12-02 42 views
14

tôi có mã này mà vẽ một hình chữ nhật (Im cố gắng để làm lại MS Paint)vòng tròn Vẽ với System.Drawing

case "Rectangle": 
       if (tempDraw != null) 
       { 
        tempDraw = (Bitmap)snapshot.Clone(); 
        Graphics g = Graphics.FromImage(tempDraw); 
        Pen myPen = new Pen(foreColor, lineWidth); 
        g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1); 
        myPen.Dispose(); 
        e.Graphics.DrawImageUnscaled(tempDraw, 0, 0); 
        g.Dispose(); 
       } 

Nhưng nếu tôi muốn vẽ một vòng tròn, những gì sẽ thay đổi?

g.DrawRectangle(myPen, x1, y1, x2-x1, y2-y1); 

Trả lời

3

Bạn nên sử dụng DrawEllipse:

// 
// Summary: 
//  Draws an ellipse defined by a bounding rectangle specified by coordinates 
//  for the upper-left corner of the rectangle, a height, and a width. 
// 
// Parameters: 
// pen: 
//  System.Drawing.Pen that determines the color, width, 
//  and style of the ellipse. 
// 
// x: 
//  The x-coordinate of the upper-left corner of the bounding rectangle that 
//  defines the ellipse. 
// 
// y: 
//  The y-coordinate of the upper-left corner of the bounding rectangle that 
//  defines the ellipse. 
// 
// width: 
//  Width of the bounding rectangle that defines the ellipse. 
// 
// height: 
//  Height of the bounding rectangle that defines the ellipse. 
// 
// Exceptions: 
// System.ArgumentNullException: 
//  pen is null. 
public void DrawEllipse(Pen pen, int x, int y, int width, int height); 
+0

trao cho bạn câu trả lời – Tony

2

nếu bạn muốn vẽ vòng tròn vào nút sau đó mã này có thể sử dụng đầy đủ. khác nếu bạn muốn vẽ một vòng tròn trên điều khiển khác chỉ cần thay đổi tên của điều khiển và cũng là sự kiện. như nút sự kiện ở đây được gọi. nếu bạn muốn vẽ vòng tròn này trong hộp nhóm, hãy gọi sự kiện Groupbox. liên quan

public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     this.button1.Location = new Point(108, 12); 
     // this.Paint += new PaintEventHandler(Form1_Paint); 
     this.button1.Paint += new PaintEventHandler(button1_Paint); 
    } 
    void button1_Paint(object sender, PaintEventArgs e) 
    { 
     Graphics g = this.button1.CreateGraphics(); 
     Pen pen = new Pen(Color.Red); 
     g.DrawEllipse(pen, 10, 10, 20, 20); 

    } 





} 
21

Không có phương pháp DrawCircle; sử dụng DrawEllipse để thay thế. Tôi có một lớp tĩnh với phương pháp mở rộng đồ họa (trong số những người khác không được hiển thị ở đây) mà vẽ và điền vào các vòng tròn. Họ là hàm bao quanh DrawEllipseFillEllipse:

public static class GraphicsExtensions 
{ 
    public static void DrawCircle(this Graphics g, Pen pen, 
            float centerX, float centerY, float radius) 
    { 
     g.DrawEllipse(pen, centerX - radius, centerY - radius, 
         radius + radius, radius + radius); 
    } 

    public static void FillCircle(this Graphics g, Brush brush, 
            float centerX, float centerY, float radius) 
    { 
     g.FillEllipse(brush, centerX - radius, centerY - radius, 
         radius + radius, radius + radius); 
    } 
} 

Bạn có thể gọi nó như thế này:

g.DrawCircle(myPen, centerX, centerY, radius); 
+2

Tôi thích điều này vì đó là mã có thể sử dụng lại được. Đồ tốt! –

1

Với mã này, bạn có thể dễ dàng vẽ một vòng tròn ... C# là rất tốt và dễ dàng bạn tôi

public partial class Form1 : Form 
{ 


public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Graphics myGraphics = base.CreateGraphics(); 
     Pen myPen = new Pen(Color.Red); 
     SolidBrush mySolidBrush = new SolidBrush(Color.Red); 
     myGraphics.DrawEllipse(myPen, 50, 50, 150, 150); 
    } 
} 
Các vấn đề liên quan