GDI+绘制简单的图形

protected void Page_Load(object sender, EventArgs e)

        {

            /*

                首先需要引用的命名空间是:System.Drawing;

            */

            GraphicsImage();

        }



        private void GraphicsImage()

        {

            int width = 800, hight = 400;

            Bitmap image = new Bitmap(width, hight);

            Graphics g = Graphics.FromImage(image);  //创建一个高200,宽440的画布



            try

            {

                g.Clear(Color.LightBlue);   //清空背景色



                /*在画布上写字*/

                Font font1 = new Font("宋体", 10);  //设置字体类型和大小

                Brush brush = new SolidBrush(Color.Black);  //设置画刷颜色           

                g.DrawString("我的GDI+测试", font1, brush, 100, 20);



                Pen pen = new Pen(Color.Brown, 1);  //创建画笔对象



                /*在画布上画线*/

                g.DrawLine(pen, 4, 80, 600, 80);  //绘制直线



                /*在画布上画矩形*/

                g.DrawRectangle(pen, 100, 120, 45, 100);  //绘制矩形



                /*在画布上画多边形*/

                Point[] points = new Point[5];

                points[0].X = 300; points[0].Y = 60;

                points[1].X = 250; points[1].Y = 80;

                points[2].X = 300; points[2].Y = 100;

                points[3].X = 350; points[3].Y = 100;

                points[4].X = 600; points[4].Y = 80;

                g.DrawPolygon(pen, points); //绘制多边形



                g.DrawEllipse(pen, 50, 60, 80, 80);

                //绘制椭圆形

                g.DrawEllipse(pen, 150, 80, 100, 40);

                //绘制扇形

                g.DrawPie(pen, 270, 60, 100, 100, 180, 130);





                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

                Response.ClearContent();

                Response.ContentType = "image/Gif";

                Response.BinaryWrite(ms.ToArray());

            }

            catch (Exception ms)

            {

                Response.Write(ms.Message);

            }

        }

  

你可能感兴趣的:(DI)