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

    แลปนี้จะแสดงให้เห็นถึงการใช้Brush ในลักษณะ ต่างๆ แลปนี้โปรดอ่านใบงานเทียบไปด้วยจะทำความเข้าใจได้ง่ายขึ้น

  • การระบายสีด้วย SolidBrush
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Point[] pt = { new Point(10,22),
                           new Point(188,246),
                           new Point(280,192),
                           new Point(250,48)
                         };
            e.Graphics.FillClosedCurve(Brushes.Blue, pt);
            e.Graphics.DrawClosedCurve(Pens.Red, pt);
        }

  • การระบายสีด้วย HatchBrush
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.SetClientSizeCore(500, 600);
            HatchBrush brush;
            int x = 20;
            int y = 20;
            foreach (HatchStyle brushStyle in Enum.GetValues(typeof(HatchStyle)))
            {
                brush = new HatchBrush(brushStyle, Color.Navy, Color.Yellow);
                e.Graphics.FillRectangle(brush, x, y, 40, 20);

                y += 30;
                if ((y + 30) > this.ClientSize.Height)
                {
                    y = 20;
                    x = 180;
                }
            }
        }

  • การระบายสีด้วย TextureBrush
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Image image = Image.FromFile("C:\\Users\\Priv8\\Pictures\\1.jpg");
            TextureBrush brush = new TextureBrush(image);
            Rectangle rect = new Rectangle(200, 200, 180, 150);
            e.Graphics.FillEllipse(brush, rect);

        }

  • การระบายสีด้วย TextureBrush ลงบน Panel
        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
                LinearGradientBrush pnlGdt =
                new LinearGradientBrush(panel1.ClientRectangle,
                Color.Yellow, Color.Navy, 0f, true);

            e.Graphics.FillRectangle(pnlGdt, panel1.ClientRectangle);
            pnlGdt.Dispose();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
             LinearGradientBrush pnlGdt =
             new LinearGradientBrush(panel2.ClientRectangle,
             Color.Yellow, Color.Navy, 90f, true);

            e.Graphics.FillRectangle(pnlGdt, panel2.ClientRectangle);
            pnlGdt.Dispose();
        }

  • การระบายสีด้วย Path Gradient Brush
      private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {
            LinearGradientBrush pnlGdt =
           new LinearGradientBrush(panel1.ClientRectangle,
               Color.Yellow, Color.Navy, 0f, true);

            e.Graphics.FillRectangle(pnlGdt, panel1.ClientRectangle);
            pnlGdt.Dispose();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            LinearGradientBrush pnlGdt =
                 new LinearGradientBrush(panel2.ClientRectangle,
                     Color.Yellow, Color.Navy, 90f, true);

            e.Graphics.FillRectangle(pnlGdt, panel2.ClientRectangle);
            pnlGdt.Dispose();
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {
         //เพิ่ม GraphicsPath
            GraphicsPath path = new GraphicsPath();
            //สร้างวงนอกขนาดเท่าpanel
            path.AddEllipse(panel3.ClientRectangle);
            //สร้างGradientBrush จาก GraphicsPath
            PathGradientBrush br = new PathGradientBrush(path);
            //กำหนดจุดศูนย์กลางให้Gradient
            br.CenterPoint = new PointF(panel3.ClientRectangle.Width / 2,
                panel3.ClientRectangle.Height / 2);
            //สีตรงจุดศูนย์กลาง
            br.CenterColor = Color.Navy;
            //สีรอบๆจุดศูนย์กลาง
            br.SurroundColors = new Color[] { Color.Yellow };
            e.Graphics.FillPath(br, path);
        }


  • ท้าทาย!!!ให้เลือนจุดศูนย์กลางและปรับเปลียนสี ให้ได้รูปดังนี้
  • จาก การระบายสีด้วย Path Gradient Brush ให้เปลี่ยนpanel3 ตามโค้ดด้านล่างนี้
        private void panel3_Paint(object sender, PaintEventArgs e)
        {
            //เพิ่ม GraphicsPath
            GraphicsPath path = new GraphicsPath();
            //สร้างวงนอกขนาดเท่าpanel
            path.AddEllipse(panel3.ClientRectangle);
            //สร้างGradientBrush จาก GraphicsPath
            PathGradientBrush br = new PathGradientBrush(path);
            //กำหนดจุดศูนย์กลางให้Gradient
            br.CenterPoint = new PointF(panel3.ClientRectangle.Width / 1,
                panel3.ClientRectangle.Height / 7);
            //สีตรงจุดศูนย์กลาง
            br.CenterColor = Color.White;
            //สีรอบๆจุดศูนย์กลาง
            br.SurroundColors = new Color[] { Color.Black };
            e.Graphics.FillPath(br, path);
        }

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

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

VISITOR