C#实现动态数据绘图graphic的方法示例

时间:2022-04-21 04:33:26

本文实例讲述了c#实现动态数据绘图graphic的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.drawing.drawing2d;
namespace drawing
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    private int widthadded = 0;
    private int heightadded = 0;
    private int added_value = 15;
    private int incremented = 3;
    private double width_max = 0.0f;
    private double height_max = 0f;
    private string hstrlink;
    private string vstrlink;
    private const int hor_kedu=17;
    private const int ver_kedu = 13;
    private double helprdm = 0;
    private void form1_load(object sender, eventargs e)
    {
      width_max=convert.todouble(panel.clientsize.width);
      height_max = convert.todouble(panel.clientsize.height);
      btnstatusinfoforb();
    }
    private void button1_click(object sender, eventargs e)
    {
      graphics mygraphics_framwork;
      pen mypen = new pen(color.blue, 0.25f);
      mygraphics_framwork = panel.creategraphics();
      for (int increment = 0; increment < panel.height; increment += incremented)
      {
        mygraphics_framwork.drawline(mypen, 0, heightadded, panel.clientsize.width, heightadded);
        heightadded = heightadded + added_value;
      }
      for (int increment = 0; increment < panel.height; increment += incremented)
      {
        mygraphics_framwork.drawline(mypen, widthadded, 0, widthadded, panel.clientsize.height);
        widthadded = widthadded + added_value;
      }
      mygraphics_framwork.dispose();
      for (int hstart = 0; hstart < width_max; hstart += hor_kedu)
      {
        hstrlink = hstrlink + hstart.tostring() + "-";
      }
      label2.text = hstrlink;
      for (int start = (int)height_max; start > 0; start -= ver_kedu)
      {
        vstrlink = vstrlink + start.tostring() + "-" + "\n";
      }
      label3.text = vstrlink + "0-";
      btnstatusinfoacti();
      button1.enabled = false;
    }
    private void btnstatusinfoforb()
    {
      button2.enabled = false;
      button3.enabled = false;
      button4.enabled = false;
      trackbar1.enabled = false;
      rec_numbox.enabled = false;
    }
    private void btnstatusinfoacti()
    {
      button2.enabled = true;
      button3.enabled = true;
      button4.enabled = true;
      trackbar1.enabled = true;
      rec_numbox.enabled = true;
    }
    private void myfun()
    {
      graphics mygraphics = panel.creategraphics();
      font myfont = new font("times new roman", 72, fontstyle.italic);
      point startpoint = new point(0, 0);
      point endpoint = new point(30, 30);
      lineargradientbrush mybrush = new lineargradientbrush(startpoint, endpoint, color.black, color.yellow);
      mybrush.wrapmode = wrapmode.tileflipxy;
      mygraphics.drawstring("string", myfont, mybrush, 0, 0);
    }
    private void button2_click(object sender, eventargs e)
    {
      if (parabox1.text == null)
      {
        messagebox.show("请输入矩形的高度");
      }
      else
      {
        try
        {
          graphics mygraphics_rectangle = panel.creategraphics();
          pen mypen_rec = new pen(color.red, 3);
          rectangle rect = new rectangle();
          rect.x = 20; rect.y = panel.clientsize.height - convert.toint32(parabox1.text);
          rect.width = 20; rect.height = panel.clientsize.height;
          mygraphics_rectangle = panel.creategraphics();
          mygraphics_rectangle.drawrectangle(mypen_rec, rect);
          solidbrush myrectbrush = new solidbrush(color.purple);
          mygraphics_rectangle.fillrectangle(myrectbrush, rect);
          mygraphics_rectangle.dispose();
        }
        catch (exception excp)
        {
          messagebox.show(excp.message);
        }
      }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_click(object sender, eventargs e)
    {
      graphics mygraphics_rectangle = panel.creategraphics();
      if (rec_numbox == null)
      {
        messagebox.show("请输入你要绘制的矩形数量");
      }
      else
      {
        double count = convert.toint32(rec_numbox.text);
        double tablespace = (1 * width_max) / (4 * count);
        double tablewidth = (3 * width_max) / (4 * count);
        double recoffset = tablewidth + tablespace;//
        pen mypen_rec = new pen(color.red, 3);
        for (long rectnum = 0; rectnum < count; rectnum++)
        {
          random radom = new random();
          system.threading.thread.sleep(20);
          double randomhegiht =radom.next(0, convert.toint32(height_max));
          rectangle rect = new rectangle();
          rect.x =convert.toint32(rectnum*recoffset);
          rect.y = convert.toint32(panel.clientsize.height - randomhegiht);
          rect.width = convert.toint32(tablewidth);
          rect.height = panel.clientsize.height;
          mygraphics_rectangle = panel.creategraphics();
          mygraphics_rectangle.drawrectangle(mypen_rec, rect);
          solidbrush myrectbrush = new solidbrush(color.green);
          mygraphics_rectangle.fillrectangle(myrectbrush, rect);
        }
      mygraphics_rectangle.dispose();
      }
      rec_numbox.text = added_value.tostring();
      added_value = added_value+50;
    }
    private void button4_click(object sender, eventargs e)
    {
      stringformat sf=new stringformat();
      sf.alignment = stringalignment.near;
      graphics mygraphics = panel.creategraphics();
      rectanglef ref = new rectanglef(12, 30, 8, 38);
      font myfont = new font("times new roman", 62, fontstyle.italic);
      point startpoint = new point(0, 0);
      point endpoint = new point(30, 30);
      lineargradientbrush mybrush = new lineargradientbrush(startpoint, endpoint, color.green, color.blue);
      solidbrush myrectbrush = new solidbrush(color.purple);
      mybrush.wrapmode = wrapmode.tileflipxy;
      mygraphics.drawstring("b2spirit", myfont, mybrush, 0, 0);
      random r=new random();
      int temp=r.next(10,300);
      mygraphics.drawstring(temp.tostring(), myfont, myrectbrush, ref, sf);
    }
    private void trackbar1_scroll(object sender, eventargs e)
    {
      trackbar mytb=new trackbar();
      mytb=(trackbar)sender;
      rec_numbox.text = mytb.value.tostring();
    }
  }
}

截图

C#实现动态数据绘图graphic的方法示例

C#实现动态数据绘图graphic的方法示例

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://blog.csdn.net/microzone/article/details/16903943