GDI+入门(6、综合练习)

六、综合练习

private void button1_Click(object sender, EventArgs e)

        {

            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(10, 10, 10, 50);

            gp.AddLine(10, 50, 50, 50);

            gp.AddLine(50, 50, 50, 10);

            gp.StartFigure();

            gp.AddLine(60, 10, 60, 50);

            gp.AddLine(60, 50, 100, 50);

            gp.AddLine(100, 50, 100, 10);

            gp.CloseFigure();

 

            Rectangle r = new Rectangle(110, 10, 40, 40);

            gp.AddEllipse(r);

 

            Region reg = new Region(gp);

            Graphics g = this.CreateGraphics();

            g.FillRegion(Brushes.Green, reg);

 

            reg.Dispose();

            gp.Dispose();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            GraphicsPath gp = new GraphicsPath();

            gp.AddLine(10, 10, 10, 50);

            gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);

            gp.AddLine(30, 70, 60, 70);

            gp.AddBezier(60, 70, 85, 70, 90, 55, 90, 50);

            gp.AddLine(90, 50, 90, 30);

            gp.AddLine(90, 30, 120, 10);

            gp.AddLine(120, 10, 150, 10);

            gp.AddLine(150, 10, 170, 30);

            gp.AddLine(170, 30, 170, 70);

 

            gp.StartFigure();

 

            gp.AddLine(60, 110, 40, 160);

            gp.AddLine(40, 160, 60, 180);

            gp.AddLine(60, 180, 140, 150);

            gp.AddLine(140, 150, 120, 110);

 

            Region reg = new Region(gp);

            Graphics g = this.CreateGraphics();

            g.FillRegion(Brushes.Green, reg);

            reg.Dispose();

            gp.Dispose();

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            Region r1 = new Region(new Rectangle(10, 10, 80, 20));

            RegionData r1Data = r1.GetRegionData();

            Region r2 = new Region(r1Data);

            Graphics g = this.CreateGraphics();

            g.FillRegion(Brushes.Green, r2);

            //g.FillRegion(Brushes.Red, r1);

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            if (comboBox1.SelectedItem == null)

                return;

            Rectangle r1 = new Rectangle(50, 10, 50, 130);

            Rectangle r2 = new Rectangle(10, 50, 130, 50);

            Graphics g = this.CreateGraphics();

            Region reg = new Region(r1);

            g.FillRectangle(Brushes.White, this.ClientRectangle);

            switch (comboBox1.SelectedItem.ToString())

            {

                case "Intersect":

                    reg.Intersect(r2);

                    g.FillRegion(Brushes.Orange, reg);

                    g.DrawRectangle(Pens.Black, r1);

                    g.DrawRectangle(Pens.Black, r2);

                    g.Dispose();

                    reg.Dispose();

                    break;

                case "Union":

      

                    reg.Union(r2);

                    g.FillRegion(Brushes.Orange, reg);

                    g.DrawRectangle(Pens.Black, r1);

                    g.DrawRectangle(Pens.Black, r2);

                    g.Dispose();

                    reg.Dispose();

                    break;

                case "Xor":

                    reg.Xor(r2);

                    g.FillRegion(Brushes.Orange, reg);

                    g.DrawRectangle(Pens.Black, r1);

                    g.DrawRectangle(Pens.Black, r2);

                    g.Dispose();

                    reg.Dispose();

                    break;

                case "Complement":

                    reg.Complement(r2);

                    g.FillRegion(Brushes.Orange, reg);

                    g.DrawRectangle(Pens.Black, r1);

                    g.DrawRectangle(Pens.Black, r2);

                    g.Dispose();

                    reg.Dispose();

                    break;

                case "Exclude":

                    reg.Exclude(r2);               

                    g.FillRegion(Brushes.Orange, reg);

                    g.DrawRectangle(Pens.Black, r1);

                    g.DrawRectangle(Pens.Black, r2);

                    g.Dispose();

                    reg.Dispose();

                    break;

            }

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            Region r = new Region();

            r.MakeInfinite();

            r.Exclude(new Rectangle(30, 30, 50, 20));

            r.Exclude(new Rectangle(30, 60, 50, 20));

            Graphics g = this.CreateGraphics();

            g.FillRegion(Brushes.Orange, r);

            r.Dispose();

            g.Dispose();

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

 

            g.FillRectangle(Brushes.White, this.ClientRectangle);

 

            Rectangle rect = new Rectangle(10, 10, 80, 50);

 

            g.DrawRectangle(Pens.Black, rect);

 

            g.SetClip(rect);

 

            g.FillEllipse(Brushes.Blue, 20, 20, 100, 100);

        }

 

        private void button7_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

 

            g.FillRectangle(Brushes.White, this.ClientRectangle);

 

            Region reg = new Region();

            reg.MakeEmpty();

 

            GraphicsPath gp = new GraphicsPath();

            gp.AddEllipse(10, 10, 50, 50);

            reg.Union(gp);

 

            gp.Reset();

 

            gp.AddLine(40, 40, 70, 10);

            gp.AddLine(70, 10, 100, 40);

            gp.CloseFigure();

            reg.Union(gp);

 

            reg.Union(new Rectangle(40, 50, 60, 60));

            g.SetClip(reg, CombineMode.Replace);

            g.FillRectangle(Brushes.Green, this.ClientRectangle);

 

            gp.Dispose();

            reg.Dispose();

        }

 

        private void button8_Click(object sender, EventArgs e)

        {

            Graphics g = this.CreateGraphics();

 

            g.FillRectangle(Brushes.White, this.ClientRectangle);

 

            GraphicsPath gp = new GraphicsPath();

            gp.AddString("BOBUI.DH"new FontFamily("Times New Roman"),

                (int)(FontStyle.Bold | FontStyle.Italic),

                144, new Point(5, 5), StringFormat.GenericTypographic);

            g.SetClip(gp);

            g.DrawImage(new Bitmap("1.jpg"),this.ClientRectangle);

            gp.Dispose();

        }

说明:

button1_ClickStartFigureCloseFigure的认识

观看效果图可以说明:

1. StartFigure之前的图像会自动闭合,接下来将绘制另一个图像

2. CloseFigure也会自动闭合图像,接下来将绘制另一个图像

3.  Rectangle r = new Rectangle(110, 10, 40, 40);

            gp.AddEllipse(r);

4. Region指示由矩形和由路径构成的图形形状的内部

button2_Click:和button1_Click一样

button3_ClickRegionData的使用

RegionData:封装构成 Region 对象的数据

button4_ClickRegion做并集运算

Intersect将此 Region 更新为其自身与指定的 Region 的交集

Union 将此 Region 更新为其自身与指定 GraphicsPath 的并集

Xor  将此 Region 对象更新为其自身与指定 GraphicsPath 对象的并集减去这两者的交集

Complement  将此 Region 更新为指定的 RectangleF 结构中与此 Region 不相交的部分

Exclude  将此 Region 更新为其内部与指定的 Rectangle 结构不相交的部分

button5_ClickRegion方法MakeInfinite

MakeInfinite 无限内部

button6_Click:剪切

SetClip将此 Graphics 的剪辑区域设置为指定 Graphics  Clip 属性

button7_Click:裁剪

button8_Click:文件剪切

把画板裁剪到只剩下文字部分,然后再把把图片绘制到文字画板上。

本人也在学习GDI+,写得比较简单,让高手见笑了。欢迎高手给我指点

邮箱:cosco.cheung@hotmail.com

QQ:4631163

你可能感兴趣的:(入门)