GDI+图形图像编程实例(一)

GDI提供了丰富的图形图像处理功能,主要由二维矢量图形、图像处理等组成。并且GDI对各种字体、字号和样式显示文本提供大量的支持。使用GDI需要引入命名空间using System.Drawing;下面用两个实例来说明GDI的使用。

实例一:简单图形绘制

使用GDI绘制图形大致上可以分成以下步骤:

1.必须创建Graphics对象,然后进行各种画图操作。简单的说,在绘画前要有画布,Graphics就是用来创建画布的。

2.创建pen对象用来绘制直线和曲线对象。

3.创建brush对象。可以用画刷填充各种图形形状,如矩形、椭圆形、扇形、对边型等。

4.将绘制的图形在页面显示。

下面是绘制矩形的代码:

 

protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(260, 120);
        Graphics graphics = Graphics.FromImage(bitmap);
        graphics.Clear(Color.White);
        Pen pen = new Pen(Color.Black, 3);
        Rectangle rect = new Rectangle(20,20,80,50);
        graphics.DrawRectangle(pen, rect);

        SolidBrush mysolidbrush = new SolidBrush(Color.Yellow);
        graphics.FillRectangle(mysolidbrush, 20, 20, 80, 50);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());

    }


运行结果:  

下面是绘制扇形代码:

 

 protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(260, 120);
        Graphics graphics = Graphics.FromImage(bitmap);
        graphics.Clear(Color.White);
        Pen pen = new Pen(Color.Black, 3);
        Rectangle rect = new Rectangle(20, 20, 80, 50);
        graphics.DrawPie(pen, rect,60,-120);

        SolidBrush mysolidbrush = new SolidBrush(Color.Yellow);
        graphics.FillPie(mysolidbrush, 20, 20, 80, 50, 60, -120);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());

    }


运行结果:

实例二:绘制柱状图形

绘制柱状图形之前,首先要从数据库中检索出相应的数据,然后根据比列绘制图形。大体上可以分为以下几个步骤:

1.画图之前,首先建立一个画布。使用Bitmap 和Graphics对象,以便能够完成图形绘制。

2.绘制背景墙、网格线以及坐标轴。

3.为绘制的坐标轴绘制数据标记。X轴显示学院名称,Y轴显示学生人数。

4.将从数据库中检索出的数据按一定的比列绘制到图像中。

5.将绘制好的柱状图形显示在页面上。

       

以上图为例,具体代码如下:

                

 protected void Page_Load(object sender, EventArgs e)
    {
        int height = 400, width = 600;
        Bitmap image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);
        try
        {
            g.Clear(Color.White);

            Font font = new Font("Arial", 9, FontStyle.Regular);
            Font font1 = new Font("宋体", 20, FontStyle.Bold);
            Font font2 = new Font("宋体", 8, FontStyle.Italic);
            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true);
            g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
            g.DrawString("西安思源学院各分院人数统计", font1, myLinearGradientBrush, new PointF(130, 40));
            g.DrawString("电信学院:王红刚制作", font2, myLinearGradientBrush, new PointF(470, 380));

            g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);//绘制边框

            Pen mypen = new Pen(myLinearGradientBrush, 1);//绘制纵向线条
            int x = 100;
            for (int i = 0; i < 11; i++)
            {
                g.DrawLine(mypen, x, 80, x, 340);
                x = x + 40;
            }


            Pen mypen1 = new Pen(Color.Blue, 2);//绘制横向线条
            int y = 106;
            for (int i = 0; i < 6; i++)
            {
                g.DrawLine(mypen1, 100, y, 540, y);
                y = y + 40;
            }
            g.DrawLine(mypen1, 100, y, 540, y);
     
            String[] n = { "电信", "经贸", "管理", "汽车", "人文", "机电", "外语", "城建", "高职", "文管" };//x轴
            x = 103;
            for (int i = 0; i < 10; i++)
            {
                g.DrawString(n[i].ToString(), font, Brushes.Black, x, 348); //设置文字内容及输出位置
                x = x + 40;
            }

            String[] m = { "3000", "2500", " 2000", " 1500", " 1000", " 500", " 0" }; //y轴
            y = 100;
            for (int i = 0; i < 7; i++)
            {
                g.DrawString(m[i].ToString(), font, Brushes.Black, 60, y); //设置文字内容及输出位置
                y = y + 40;
            }

            int[] Count = new int[10];//查询数据将查询结果放到Count中
            SqlConnection conn = new SqlConnection("Server=localhost;Database=ASPNET_tech;Uid=sa;Pwd=sa");
            conn.Open();
            SqlDataAdapter da;
            DataSet ds = new DataSet();
            for (int i = 0; i < 10; i++)
            {
                string str = "select Coll_Num from student_info where ID=" + (i + 1);
                da = new SqlDataAdapter(str, conn);
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    Count[i] = 0;
                }
                else
                {
                    Count[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["Coll_Num"].ToString());
                }
            }
            conn.Close();
            x = 109;
            for (int i = 0; i < 10; i++) //显示柱状效果
            {
                SolidBrush mybrush = new SolidBrush(Color.Blue);
                g.FillRectangle(mybrush, x, 345 - Count[i] * 40 / 500, 20, Count[i] * 40 / 500);
                x = x + 40;
            }
            
            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());


        }
        finally
        {
            
            g.Dispose();
            image.Dispose();
        }
    }


 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值