[C#]LAB-18การเขียนโปรแกรมกราฟฟิก ด้วย GDI+(2)

   ตอนนี้ผมรุ้สึกได้ว่าเหนื่อยและเมื่อยมาก ๆผมจึงขอยึดหลักการเดิมของLAB17 คือนำโค้ดFrom1.csมาแปะ แล้วก็เขียนคอมเม้นของแต่ละการทดลองย่อยลงในโปรเจคเดียวกันไปเลย ไม่ต้องแยกเป็นหลาย ๆโปรเจคให้เยอะแยะ ดังนั้นการทดลองนี้ยังคงอ้างอิงใบงานการทดลองนะครับไม่เข้าใจไปดูใบงานใน LAB STORE(อยุ่ล่างสุด)

  • เขียนโค้ดลงFrom.1ตามนี้ครับ ผมจะใส่คอมเม้นแยกการทดลองไว้นะครับ ส่วนตอนรันโปรแกรมจริงๆผมจะทำคอมเม้นไว้ในส่วนของการทดลองอื่นเพื่อเลือกรันการทดลองเดียวจะได้เห็นผลชัดครับ แต่ตอนนี้ไม่ได้คอมเม้นไว้ครับเปิดทุกการทดลองหมดเมื่อรันแล้วมันจะเยอะและทับกันครับตามภาพด้านล่างนี่้เลยครับ
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
          //2. การวาดเส้นตรง
            Pen blackPen = new Pen(Color.Black, 3);
            Point point1 = new Point(100, 100);
            Point point2 = new Point(200, 100);
            e.Graphics.DrawLine(blackPen, point1, point2);
            blackPen.Dispose();

          //3. การวาดเส้นตรงด้วย pen style และ brush
            Pen pen = new Pen(Color.Red);
            e.Graphics.DrawLine(pen, 10, 50, 220, 50);

            pen = new Pen(Color.Green, 2);
            pen.DashStyle = DashStyle.DashDot;
            e.Graphics.DrawLine(pen, 10, 80, 220, 80);

            pen = new Pen(Brushes.DeepSkyBlue, 4);
            e.Graphics.DrawLine(pen, 10, 120, 220, 120);
            pen.Dispose();

          //4. การกำหนดจุดปลายของเส้นตรงด้วย style แบบต่างๆ
            Pen[] objPen = new Pen[11];
            for (int i = 0; i != 11; i++)
            {
                objPen[i] = new Pen(Color.Blue, 9);
            }
            objPen[0].EndCap = LineCap.AnchorMask;
            objPen[2].EndCap = LineCap.ArrowAnchor;
            objPen[3].EndCap = LineCap.DiamondAnchor;
            objPen[4].EndCap = LineCap.Flat;
            objPen[5].EndCap = LineCap.NoAnchor;
            objPen[6].EndCap = LineCap.Round;
            objPen[7].EndCap = LineCap.RoundAnchor;
            objPen[8].EndCap = LineCap.Square;
            objPen[9].EndCap = LineCap.SquareAnchor;
            objPen[10].EndCap = LineCap.Triangle;

            for (int i = 0; i != 11; i++) 
            {
                e.Graphics.DrawLine(objPen[i], 10, 10 + 20 * i, 200, 10 + 20 * i);
                objPen[i].Dispose();
            }

          //5. การวาดเส้นโค้ง
            Pen pen5 = new Pen(Color.Green);
            Point[] pt = {
                          new Point(20,200), new Point(50,20),
                          new Point(100,100), new Point(150,20), new Point(200,200)
                          };
            e.Graphics.DrawCurve(pen5, pt);
            pen5.Dispose();

          //6. การวาดเส้นโค้งด้วย Graphics path
            GraphicsPath gp = new GraphicsPath();
            gp.AddCurve(new Point[]
            {
                new Point(100,50),
                new Point(105,40),
                new Point(120,40),
                new Point(130,65),
                new Point(100,100)
            }, 0.5f);

            gp.AddCurve(new Point[]
            {
                new Point(100,100),
                new Point(70,65),
                new Point(80,40),
                new Point(95,40),
                new Point(100,50)
            }, 0.5f);
            e.Graphics.DrawPath(Pens.Red, gp);

          //7.1 การวาดสีเ หลีย มครัง ละรูปเดียว
            Pen mypen = new Pen(Color.Blue);
            e.Graphics.DrawRectangle(mypen, 10, 120, 100, 100);
            Rectangle rect = new Rectangle(10, 10, 100, 100);
            e.Graphics.DrawRectangle(mypen, rect);

          //7.2 การวาดสีเ หลีย มพร้อมกันครัง ละหลายๆ รูป
            Pen curPen = new Pen(Color.Blue, 3);
            Rectangle[] rect7 = {    new Rectangle (20, 20, 120, 20),
                                    new Rectangle (20, 50, 120, 30),
                                    new Rectangle (20, 90, 120, 140),
                                    new Rectangle (20, 140, 120, 60),
                               };
            e.Graphics.DrawRectangles(curPen, rect7);

          //8. การวาดวงกลมและวงรี
            Rectangle rect0 = new Rectangle(10, 10, 120, 100);
            e.Graphics.DrawEllipse(Pens.Cyan, rect0);

            Rectangle rect1 = new Rectangle(10, 120, 100, 100);
            e.Graphics.FillEllipse(Brushes.DeepPink, rect1);

            Rectangle rect2 = new Rectangle(150, 10, 120, 100);
            e.Graphics.DrawEllipse(Pens.DarkSlateBlue, rect2);

            Rectangle rect3 = new Rectangle(120, 120, 100, 100);
            e.Graphics.FillEllipse(Brushes.Firebrick, rect3);

          //9. การวาดส่วนโค้ง (Arc)
            Pen penEllipse = new Pen(Color.Brown);
            penEllipse.DashStyle = DashStyle.Dash;
            e.Graphics.DrawEllipse(penEllipse, 20, 20, 200, 150);

            Pen penArc = new Pen(Color.Magenta, 2);
            e.Graphics.DrawArc(penArc, 20, 20, 200, 150, 45, 180);

          //10. การวาดรูป Pie
            Pen penEllipse1 = new Pen(Color.Brown);
            penEllipse1.DashStyle = DashStyle.Dash;
            e.Graphics.DrawEllipse(penEllipse1, 20, 20, 200, 150);

            Pen penPie = new Pen(Color.Magenta, 2);
            e.Graphics.DrawPie(penPie, 20, 20, 200, 150, 45, 90);
            Pen penPie1 = new Pen(Color.BlueViolet, 2);
            e.Graphics.DrawPie(penPie1, 20, 20, 200, 150, 135, 45);

          //11. การสร้าง graphics path จากรูปต่างๆ
            GraphicsPath gpath = new GraphicsPath();
            gpath.AddEllipse(46, 4, 28, 28);
            gpath.AddLine(36, 32, 84, 32);
            gpath.AddLine(100, 80, 88, 84);
            gpath.AddLine(76, 50, 74, 84);
            gpath.AddLine(90, 150, 74, 84);
            gpath.AddLine(60, 100, 46, 150);
            gpath.AddLine(32, 150, 46, 84);
            gpath.AddLine(44, 50, 32, 84);
            gpath.AddLine(20, 80, 36, 32);
            e.Graphics.FillPath(Brushes.Blue, gpath);
        } 
    }
}
  • 2.ผลการวาดเส้นตรง
  • 3.ผลการวาดเส้นตรงด้วย pen style และ brush
  • 4.ผลการกำหนดจุดปลายของเส้นตรงด้วย style แบบต่างๆ
  • 5.ผลการวาดเส้นโค้ง
  • 6.ผลการวาดเส้นโค้งด้วย Graphics path
  • 7.1.ผลการวาดสีเ หลีย มครัง ละรูปเดียว
  • 7.2.ผลการวาดสีเ หลีย มพร้อมกันครัง ละหลายๆ รูป
  • 8.ผลการวาดวงกลมและวงรี
  • 9.ผลการวาดส่วนโค้ง (Arc)
  • 10.ผลการวาดรูป Pie
  • 11.ผลการสร้าง graphics path จากรูปต่างๆ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

VISITOR