c#實現畫圖功能

1             //描繪曲線

             //創建曲線中的點

                 Point point1 = new Point(68, 420);

                 Point point2 = new Point(71, 410);

                 Point point3 = new Point(74, 423);

                 Point point4 = new Point(77, 412);

                 Point point5 = new Point(80, 400);

                 Point point6 = new Point(90, 380);

                 Point point7 = new Point(100, 350);

                 Point point8 = new Point(120, 320);

                 Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7, point8 };

                 GraphicsPath myPath = new GraphicsPath();

                 //AddCurve(點陣,起點,終點,彎曲程度)

                 myPath.AddCurve(curvePoints, 0, 7, 0.8f);

                 Pen myPen = new Pen(Color.Red, 1);

                 IMG.DrawPath(myPen, myPath);

---------------------------------------------------------------------------------------------

2 c#.net畫曲線圖(座標)

using System.Drawing.Imaging;

using System.Drawing.Drawing2D;

   //數據/月份

   public ArrayList arrData=new ArrayList();

   public ArrayList arrMonth=new ArrayList();

   //定義幕布高寬

   private int iCanvasWidth=400;

   private int iCanvasHeight=300;

   //x軸的間距

   private int iX=30;

   //y軸間距

   private int iY=15;

   public void InitArray()

   {

    for(int i=0;i<6;i++)

     arrMonth.Add(i+1);

             arrData.Add(200);

    arrData.Add(180);

    arrData.Add(285);

    arrData.Add(290);

    arrData.Add(275);

    arrData.Add(350);

   }

//    //pen 畫線

//    Pen myPen = new Pen(Color.Black);

//    Graphics g = this.CreateGraphics();

//    g.DrawEllipse(myPen, 20, 30, 10, 50);

//

//    Graphics g1 = this.CreateGraphics();

//    SolidBrush myBrush = new SolidBrush(Color.Red);

//    g1.FillEllipse(myBrush,20,30,40,40);

//    g1.FillRectangle(myBrush,70,70,10,30);

    this.InitArray();

    Bitmap bitmap= new Bitmap(iCanvasWidth, iCanvasHeight,PixelFormat.Format24bppRgb);

    //Graphics g=Graphics.FromImage(bitmap);

    Graphics g=this.CreateGraphics();

    g.Clear(Color.White);

   

    Font font = new Font("MS UI Gothic",12);

    SolidBrush brush = new SolidBrush(Color.Black);

    Pen pen = new Pen(Color.Black);

    pen.EndCap = LineCap.ArrowAnchor;

    pen.DashStyle = DashStyle.Solid;

    //座標軸

             g.DrawLine(pen,20,220,420,220);

    g.DrawLine(pen,20,220,20,20);

    //x軸標格

    g.DrawLine(Pens.Black,20+iX*1,220,20+iX*1,220-2);

    g.DrawLine(Pens.Black,20+iX*2,220,20+iX*2,220-2);

    g.DrawLine(Pens.Black,20+iX*3,220,20+iX*3,220-2);

    g.DrawLine(Pens.Black,20+iX*4,220,20+iX*4,220-2);

    g.DrawLine(Pens.Black,20+iX*5,220,20+iX*5,220-2);

    g.DrawLine(Pens.Black,20+iX*6,220,20+iX*6,220-2);

    //y軸標格

    g.DrawLine(Pens.Black, 20, 220 - iY*1, 20 + 2, 220 - iY*1);

    g.DrawLine(Pens.Black, 20, 220 - iY*2, 20 + 2, 220 - iY*2);

    g.DrawLine(Pens.Black, 20, 220 - iY*3, 20 + 2, 220 - iY*3);

    g.DrawLine(Pens.Black, 20, 220 - iY*4, 20 + 2, 220 - iY*4);

    g.DrawLine(Pens.Black, 20, 220 - iY*5, 20 + 2, 220 - iY*5);

    g.DrawLine(Pens.Black, 20, 220 - iY*6, 20 + 2, 220 - iY*6);

    //

    double dblX1=20;

    double dblY1=220;

    for(int i=0;i<arrData.Count;i++)

    {

     double dblData=Convert.ToDouble(arrData[i]);

     double dblY2=220-dblData*15/200;

    

     double dblX2=20+iX*Convert.ToInt32(arrMonth[i]);

     if(i!=0)

     {

      if(i<arrData.Count-1)

      {

      g.DrawLine(Pens.Black,float.Parse(dblX1.ToString()),float.Parse(dblY1.ToString()),float.Parse(dblX2.ToString()),float.Parse(dblY2.ToString()));

      }

      else

      {

      g.DrawLine(pen,float.Parse(dblX1.ToString()),float.Parse(dblY1.ToString()),float.Parse(dblX2.ToString()),float.Parse(dblY2.ToString()));

      }

     }

    

     dblX1=dblX2;

     dblY1=dblY2;

    }

   }

}

}

-------------------------------------------------------------------------------------------------------

C#畫折線圖一例

在.net中,微軟給我們提供了畫圖類(system.drawing.imaging),在該類中畫圖的基本功能都有。比如:直線、折線、矩形、多邊形、橢圓形、扇形、曲線等等,因此一般的圖形都可以直接通過代碼畫出來。接下來介紹一些畫圖函數:

Bitmap bmap=new Bitmap(500,500) /定義圖像大小;

bmap.Save(stream,imagecodecinfo) /將圖像保存到指定的輸出流;

Graphics gph /定義或創建gdi繪圖對像;

PointF cpt /定義二維平面中x,y座標;

DrawString(string,font,brush,ponitf) /用指定的brush和font對像在指定的矩形或點繪製指定的字符串;

DrawLine(pen,ponit,ponit) /用指定的筆(pen)對像繪製指定兩點之間直線;

DrawPolygon(pen,ponit[]) /用指定的筆(pen)對像繪製指定多邊形,比如三角形,四邊形等等;

FillPolygon(brush,ponit[]) /用指定的刷子(brush)對像填充指定的多邊形;

DrawEllipse(pen,x,y,width,height) /用指定的筆繪製一個邊框定義的橢圓;

FillEllipse(brush,x,y,width,height) /用指定的刷子填充一個邊框定義的橢圓;

DrawRectangle(pen,x,y,width,height) /用指定的筆繪製一個指定座標點、寬度、高度的矩形;

DrawPie(pen,x,y,width,height,startangle,sweepangle) /用指定的筆繪製一個指定座標點、寬度、高度以及兩條射線組成的扇形;

下面的例子可以畫出一個折線圖,代碼如下:

private void Form1_Load(object sender, EventArgs e)

         {

             string[] month=new string[12]{"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};

             float[] d=new float[12]{20.5f,60,10.8f,15.6f,30,70.9f,50.3f,30.7f,70,50.4f,30.8f,20};

             //畫圖初始化

             Bitmap bmap=new Bitmap(500,500);

             Graphics gph=Graphics.FromImage(bmap);

             gph.Clear(Color.White);

             PointF cpt=new PointF(40,420);//中心點

             PointF[] xpt=new PointF[3]{new PointF(cpt.Y+15,cpt.Y),new PointF(cpt.Y,cpt.Y-8),new PointF(cpt.Y,cpt.Y+8)};//x軸三角形

             PointF[] ypt=new PointF[3]{new PointF(cpt.X,cpt.X-15),new PointF(cpt.X-8,cpt.X),new PointF(cpt.X+8,cpt.X)};//y軸三角形

             gph.DrawString("某工廠某產品月生產量圖表", new Font("宋體", 14), Brushes.Black, new PointF(cpt.X+60, cpt.X));//圖表標題

             //畫x軸

             gph.DrawLine(Pens.Black, cpt.X,cpt.Y ,cpt.Y,cpt.Y);

             gph.DrawPolygon(Pens.Black,xpt);

             gph.FillPolygon(new SolidBrush(Color.Black),xpt);

             gph.DrawString("月份", new Font("宋體", 12), Brushes.Black, new PointF(cpt.Y+10, cpt.Y+10));

             //畫y軸

             gph.DrawLine(Pens.Black, cpt.X,cpt.Y,cpt.X,cpt.X);

             gph.DrawPolygon(Pens.Black,ypt);

             gph.FillPolygon(new SolidBrush(Color.Black),ypt);

             gph.DrawString("單位(萬)", new Font("宋體", 12), Brushes.Black, new PointF(0, 7));

             for(int i=1;i<=12;i++)

             {

                 //畫y軸刻度

                 if (i<11)

                 {

                     gph.DrawString((i*10).ToString(), new Font("宋體", 11), Brushes.Black, new PointF(cpt.X-30, cpt.Y-i*30-6));

                     gph.DrawLine(Pens.Black, cpt.X-3,cpt.Y-i*30 ,cpt.X,cpt.Y-i*30);

                 }

                 //畫x軸項目

                 gph.DrawString(month[i-1].Substring(0,1), new Font("宋體", 11), Brushes.Black, new PointF(cpt.X+i*30-5, cpt.Y+5));

                 gph.DrawString(month[i-1].Substring(1,1), new Font("宋體", 11), Brushes.Black, new PointF(cpt.X+i*30-5, cpt.Y+20));

                 if(month[i-1].Length>2) gph.DrawString(month[i-1].Substring(2,1), new Font("宋體", 11), Brushes.Black, new PointF(cpt.X+i*30-5, cpt.Y+35));

                 //畫點

                 gph.DrawEllipse(Pens.Black,cpt.X+i*30-1.5f,cpt.Y-d[i-1]*3-1.5f,3,3);

                 gph.FillEllipse(new SolidBrush(Color.Black),cpt.X+i*30-1.5f,cpt.Y-d[i-1]*3-1.5f,3,3);

                 //畫數值

                 gph.DrawString(d[i-1].ToString(), new Font("宋體", 11), Brushes.Black, new PointF(cpt.X+i*30,cpt.Y-d[i-1]*3));

                 //畫折線

                 if(i>1) gph.DrawLine(Pens.Red,cpt.X+(i-1)*30,cpt.Y-d[i-2]*3,cpt.X+i*30,cpt.Y-d[i-1]*3);

             }

             //保存輸出圖片

             //bmap.Save(Response.OutputStream, ImageFormat.Gif);

             PictureBox1.Image = bmap;

         }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章