GDI+入门(九、ASP.NET中GDI+)

九、ASP.NETGDI+

protected void Page_Load(object sender, EventArgs e)

        {

            string Filename;

            System.Drawing.Image g;

            Filename = Server.MapPath("./1.jpg");

            g = System.Drawing.Image.FromFile(Filename);

            Response.ContentType = "image/jpeg";

            g.Save(Response.OutputStream, g.RawFormat);

            g.Dispose();

        }

实在是太简单了,不讲了吧

Response.ContentType = "image/jpeg";

Response.OutputStream

这两个地方很重中

 

WebServices中使用GDI+

[WebMethod]

        public Byte[] DrawChart(int north, int east, int south, int west)

        {

            System.IO.MemoryStream outStream;

            outStream = new System.IO.MemoryStream();

            float thisAngle = 0;

            float totalAngle = 0;

 

            int width = 200;

            int height = width;

            int diameter = width - (width / 10);

            int top = (height / 2) - (diameter / 2);

            int left = (width / 2) - (diameter / 2);

 

            int[] myData = { north, east, south, west };

 

            SolidBrush[] colorBrush ={

                                         new SolidBrush(Color.Red),

                                         new SolidBrush(Color.Yellow),

                                         new SolidBrush(Color.Blue),

                                         new SolidBrush(Color.Cyan)

                                    };

            int myDataTotal = 0;

            foreach (int tmpValue in myData)

            {

                myDataTotal += tmpValue;

            }

            Bitmap b = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(b);

 

            g.Clear(Color.Black);

 

            for (int i = 0; i < myData.Length; i++)

            {

                thisAngle = (Convert.ToSingle(myData[i]) / myDataTotal) * 360;

                g.DrawPie(Pens.Black, left, top, diameter, diameter, totalAngle, thisAngle);

                g.FillPie(colorBrush[i], left, top, diameter, diameter, totalAngle, thisAngle);

                totalAngle += thisAngle;

            }

 

            b.Save(outStream, ImageFormat.Gif);

            b.Dispose();

            g.Dispose();

            return outStream.ToArray();

        }

返回一个web页面可以接受的类型就好了,这个也是可用的。条件限制暂不截图。

下面是一个饼图例子

 

private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

              float thisAngle=0;

              float totalAngle=0;

              int width=200;

              int height=width;

              int diameter=width-(width/10);

              int top=(height/2)-(diameter/2);

              int left=(width/2)-(diameter/2);

              int[] myData={60,60,60,60,60,60};

              SolidBrush[] colorBrush={

                                          new SolidBrush(Color.Red),

                   new SolidBrush(Color.Orange),

                   new SolidBrush(Color.Yellow),

                   new SolidBrush(Color.Green),

                   new SolidBrush(Color.Blue),

                   new SolidBrush(Color.Purple),

                   new SolidBrush(Color.Black),

                   new SolidBrush(Color.Silver)

                                          };

              int myDataTotal=0;

              foreach(int tmpValue in myData)

              {

                   myDataTotal+=tmpValue;

              }

              Bitmap b=new Bitmap(width,height);

              Graphics g=Graphics.FromImage(b);

              g.SmoothingMode=SmoothingMode.AntiAlias;

              g.Clear(Color.White);

 

              for(int i=0; i<myData.Length; i++)

              {

                   thisAngle=(Convert.ToSingle(myData[i])/myDataTotal)*360;

                   g.DrawPie(Pens.White,left,top,diameter,diameter,totalAngle,thisAngle);

                   g.FillPie(colorBrush[i],left,top,diameter,diameter,totalAngle,thisAngle);

                   totalAngle+=thisAngle;     

              }

             

              Response.ContentType="image/Gif";

              b.Save(Response.OutputStream,ImageFormat.Gif);

              b.Dispose();

              g.Dispose();

 

         }

和那个webservice的没有区别哦,只不过这个输出了

 

使用网上图片的例子

using System.Net;

private void Button1_Click(object sender, System.EventArgs e)

         {

              try

              {//

                  WebClient webC=new WebClient();

                   string strFilename;

                   int newWidth=32;

                   int newHeight=32;

                   strFilename="http://localhost:4570/1.jpg";

                   Bitmap b1=new Bitmap(webC.OpenRead(strFilename));

                   Bitmap b2=new Bitmap(b1,newWidth,newHeight);

                   if(b1.RawFormat.Equals(ImageFormat.Gif))

                   {

                       Response.ContentType="image/gif";

                   }

                   else

                   {

                       Response.ContentType="image/jpeg";

                   }

                   b2.Save(Response.OutputStream,b1.RawFormat);

                   b2.Dispose();

                   b1.Dispose();

              }

              catch(Exception ex)

              {

                   Response.Write(ex.Message);

              }

         }

WebClient.OpenRead:为从具有指定 URI 的资源下载的数据打开一个可读的流

 

在后太生成图片的例子

private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

              int width=200;

              int height=200;

 

              Bitmap b=new Bitmap(width,height);

              Graphics g=Graphics.FromImage(b);

 

              g.Clear(Color.White);

              g.SmoothingMode=SmoothingMode.AntiAlias;

              g.TextRenderingHint=TextRenderingHint.AntiAlias;

              g.DrawString("BOBUI.DH",new Font("Arial",18,FontStyle.Bold|FontStyle.Underline),Brushes.DarkBlue,new Point(10,50));

              Response.ContentType="image/gif";

              b.Save(Response.OutputStream,ImageFormat.Gif);

              b.Dispose();

              g.Dispose();

         }

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

邮箱:bobui@163.com

QQ125941562

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值