2009-12-02 22 views
14

Ho questo codice che disegna un rettangolo (Im cercando di rifare il MS Paint)cerchi di disegno con 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(); 
       } 

Ma cosa succede se voglio disegnare un cerchio, che cosa cambierà?

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

risposta

3

Si dovrebbe usare 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

anche a te per la risposta – Tony

2

se si vuole disegnare cerchio sul pulsante allora questo codice potrebbe essere l'uso pieno. altro se vuoi disegnare un cerchio su un altro controllo cambia solo il nome del controllo e anche l'evento. come qui viene chiamato il pulsante dell'evento. se vuoi disegnare questo cerchio nella casella di gruppo, chiama l'evento Groupbox. riguarda

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

Non esiste un metodo DrawCircle; utilizzare invece DrawEllipse. Ho una classe statica con metodi di estensione grafica (tra cui non mostrati qui) che disegnano e riempiono i cerchi. Sono wrapper intorno DrawEllipse e FillEllipse:

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

si può chiamare in questo modo:

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

Mi piace perché è codice riutilizzabile. Roba buona! –

1

Con questo codice si può facilmente disegnare un cerchio ... C# è grande e facile il mio amico

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