2009-12-02 23 views
14

que tienen este código que dibuja un rectángulo (Im tratando de rehacer el MS Paint)dibujando círculos 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(); 
       } 

Pero lo que si quiero dibujar un círculo, lo que va a cambiar?

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

Respuesta

3

Debe utilizar 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

thax para usted también para la respuesta – Tony

2

si desea dibujar el círculo en el botón a continuación, este código podría ser un uso completo. else si desea dibujar un círculo en otro control simplemente cambie el nombre del control y también el evento. como aquí se llama el botón de evento. si desea dibujar este círculo en el cuadro de grupo, llame al evento Groupbox. respecto

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

No hay DrawCircle método; use DrawEllipse en su lugar. Tengo una clase estática con métodos de extensión de gráficos (entre otros no se muestran aquí) que dibujan y completan círculos. Son envolturas alrededor DrawEllipse y 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); 
    } 
} 

se le puede llamar así:

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

Me gusta porque es un código reutilizable. ¡Buen material! –

1

Con este código se puede dibujar fácilmente un círculo ... C# es fácil y gran amigo de mi

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