GDI+画图

来源:互联网 发布:清明雨上 知乎 编辑:程序博客网 时间:2024/05/01 22:53

using System.Drawing;
using System.Drawing.Drawing2D;

 

 Bitmap像一张画布,Graphics 如同画布的手,用Pen或Brush等绘图工具在Bitmap这张画布上画画。

步骤:

1、首先构造一个Bitmap(位图)对象,为创建的图形提供内存空间。

2、然后创建Graphics对象,并通过Graphics对象提供的方法在Bitmap对象上画图像。

3、绘制完成后,调用Bitmap对象的Save方法把该图象保存为磁盘文件或将其发送到Http输出流中,显示在浏览器中

 

 

private void button1_Click(object sender, EventArgs e)
{
            //绘图步骤:
            //第一步:创建画板
            //第二步:创建笔 或 刷
            //第三步:在画板上绘制边框形状 或 填充形状
            Graphics Gra = this.CreateGraphics(); //当前窗体为画板

            //Graphics Gra1 = button1.CreateGraphics();//按钮为画板
            int[] ArrInt = new int[] { 4, 3, 5, 8, 2 };
            int sum = 0;
            foreach (int i in ArrInt)
            {
                sum += i;
            }
            float num = 0f; //扇形的开始角度
            for (int i = 0; i < ArrInt.Length; i++)
            {
                Color[] c = new Color[] { Color.Red, Color.Green, Color.Blue, Color.Black, Color.Yellow };
                SolidBrush SB = new SolidBrush(c[i % 5]); //实心单色画笔(除数与被除数的余数)
                float spli = ArrInt[i] * 360f / sum; //扇形的角度
                Gra.FillPie(SB, 100, 100, 200, 200, num, spli);
                //绘制实心扇形(实心刷,x坐标,y坐标,宽,高,开始角度,结束角度)
                num += spli;
            }

 

 

 

            //其它用法:

            //准备刷子 实心
            //SolidBrush SB = new SolidBrush(Color.Red);
            //过度          
            LinearGradientBrush LGB = new LinearGradientBrush(new Point(0,200), new Point(200, 200), Color.Green, Color.Blue);
            //填充
            //TextureBrush TB = new TextureBrush(Image.FromFile(@"F:/a.jpg"));
            //Gra.FillEllipse(TB, 0, 0, 400, 200); //实心椭圆

            ////准备画笔
            Pen pen = new Pen(Color.Green, 3.5f);
            //pen.EndCap = LineCap.ArrowAnchor;
            //pen.DashStyle = DashStyle.Dot;

            //Gra.DrawLine(pen, new Point(10, 400), new Point(10, 10));//线
            //Gra.DrawLine(pen, new Point(10, 400), new Point(400, 400));//线

            //Gra.DrawLine(pen, new Point(0, 0), new Point(100, 100));//线
            //Gra.DrawEllipse(pen, 0, 0, 200, 200);//圆
            //Gra.DrawPie(pen, 0, 0, 100, 100, 0, 120); //扇形
            Gra.DrawRectangle(pen, 0, 0, 50, 50);      //矩形边框
            Gra.FillRectangle(LGB, 400, 400, 50, 50);  //实心矩形
}