C# draw cylinder [C# 繪製圓柱]

C# draw cylinder [C# 繪製圓柱]

C# draw cylinder [C# 繪製圓柱]


ChatGPT 回答

To draw a cylinder in C#, you can use the Graphics class provided by the .NET Framework. Here's an example:

csharp
Copy code
private void DrawCylinder(Graphics g, Pen pen, Brush brush, float x, float y, float width, float height)
{
    float radius = width / 2;
    float diameter = radius * 2;
    float topY = y + (height - diameter) / 2;

    // Draw top ellipse
    g.FillEllipse(brush, x, topY, width, diameter);

    // Draw bottom ellipse
    g.FillEllipse(brush, x, topY + height - diameter, width, diameter);

    // Draw connecting rectangle
    g.FillRectangle(brush, x, topY + radius, width, height - diameter);

    // Draw top ellipse outline
    g.DrawEllipse(pen, x, topY, width, diameter);

    // Draw bottom ellipse outline
    g.DrawEllipse(pen, x, topY + height - diameter, width, diameter);

    // Draw connecting rectangle outline
    g.DrawRectangle(pen, x, topY + radius, width, height - diameter);
}
This method takes a Graphics object, a Pen object for the outlines, a Brush object for the fill, and the x, y coordinates, width, and height of the cylinder. It calculates the necessary dimensions for the top and bottom ellipses and the connecting rectangle, and then uses the Graphics object to draw them with the specified Pen and Brush objects.

To call this method, you can create a Graphics object from a PictureBox or other control, and then pass it to the method along with the desired Pen and Brush objects and the cylinder dimensions:

csharp
Copy code
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Pen pen = new Pen(Color.Black);
    Brush brush = new SolidBrush(Color.Blue);

    DrawCylinder(g, pen, brush, 50, 50, 100, 200);
}
This code creates a new Pen object with black color and a Brush object with blue color, and then calls the DrawCylinder method with the Graphics object from a PictureBox, x and y coordinates of 50 and 50, a width of 100, and a height of 200. This will draw a blue cylinder with black outlines at position (50, 50) with a width of 100 and a height of 200.


自己開發範例

Form1.cs

namespace DrawCylinder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void DrawCylinder(Graphics g, Pen pen, Brush brush, float x, float y, float width, float height,bool blnVertical=true)
        {
            float radius = (blnVertical == true)?(width / 2): (height / 2);//計算圓半徑
            float diameter = radius * 2;//計算圓直徑
            float TopPoint = (blnVertical == true) ? (y + (height - diameter) / 2): (x + (width - diameter) / 2);

            if(blnVertical)
            {
                //實體
                // Draw top ellipse
                g.FillEllipse(brush, x, TopPoint, width, diameter);

                // Draw bottom ellipse
                g.FillEllipse(brush, x, TopPoint + height - diameter, width, diameter);

                // Draw connecting rectangle
                g.FillRectangle(brush, x, TopPoint + radius, width, height - diameter);

                //邊框
                // Draw top ellipse outline
                g.DrawEllipse(pen, x, TopPoint, width, diameter);

                // Draw bottom ellipse outline
                g.DrawEllipse(pen, x, TopPoint + height - diameter, width, diameter);

                // Draw connecting rectangle outline
                g.DrawRectangle(pen, x, TopPoint + radius, width, height - diameter);
            }
            else
            {
                //實體
                // Draw top ellipse
                g.FillEllipse(brush, TopPoint, y, height, diameter);

                // Draw bottom ellipse
                g.FillEllipse(brush, TopPoint + width - diameter, y, height, diameter);

                // Draw connecting rectangle
                g.FillRectangle(brush, TopPoint + radius, y, width - diameter, height);

                //邊框
                // Draw top ellipse outline
                g.DrawEllipse(pen, TopPoint, y, height, diameter);

                // Draw bottom ellipse outline
                g.DrawEllipse(pen, TopPoint + width - diameter, y, height, diameter);

                // Draw connecting rectangle outline
                g.DrawRectangle(pen, TopPoint + radius, y, width - diameter, height);
            }

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Black);
            Brush brush = new SolidBrush(Color.Blue);

            DrawCylinder(g, pen, brush, 50, 50, 100, 500);
            DrawCylinder(g, pen, brush, 50, 50, 500, 100,false);
        }
    }
}

Form1.Designer.cs

namespace DrawCylinder
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(800, 450);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private PictureBox pictureBox1;
    }
}

2 thoughts on “C# draw cylinder [C# 繪製圓柱]

  1. C# 圖片物件 pictureBox 繪圖事件 Paint

    private void pictureBox1_Paint(object sender, PaintEventArgs e)

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *