欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-11 17:50:34
本文实例讲述了c#实现动态数据绘图graphic的方法。分享给大家供大家参考,具体如下: using system; using system.collecti...

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

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#相关内容感兴趣的读者可查看本站专题:《c#图片操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

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