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

C#实现网页画图功能

程序员文章站 2023-12-29 11:06:40
本文实例为大家分享了c#实现网页画图的具体代码,供大家参考,具体内容如下代码贴着保存下using system;using system.collections;using system.collec...

本文实例为大家分享了c#实现网页画图的具体代码,供大家参考,具体内容如下

代码贴着保存下

using system;
using system.collections;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.io;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
 
public partial class _default : system.web.ui.page
{
  int h = 1000;
  int w = 1000;
  protected void page_load(object sender, eventargs e)
  {
    bitmap img = new bitmap(h, w);//创建bitmap对象
    memorystream stream = draw();
 
    img.save(stream, imageformat.jpeg);     //保存绘制的图片
    response.clear();
    response.contenttype = "image/jpeg";
    response.binarywrite(stream.toarray());
  }
 
  public memorystream draw()
  {
    string[] words = {"壹","贰","叁","肆","伍","陆"};
    bitmap img = new bitmap(h, w);//创建bitmap对象
    graphics g = graphics.fromimage(img);//创建graphics对象
    g.drawrectangle(new pen(color.white, img.height), 2, 2, img.width - 2, img.height - 2); //矩形 底色
 
 
    arraylist coordinate = getxy(words.length,img.height,img.width);
    arraylist radius = new arraylist();
 
    var r = new random();
    color mycolor = color.fromargb(r.next(100, 150), r.next(255), r.next(255), r.next(255));
 
    font font = new font("arial", 20);// 字体
    lineargradientbrush font_brush = new lineargradientbrush(new rectangle(0, 0, img.width, img.height), color.black, color.black, 1.2f, true);
 
    int j = 0;
    //画圆 写字
    foreach (point p in coordinate)
    {
      int r = r.next(20, 40);
      radius.add(r);
      solidbrush bush = new solidbrush(mycolor);
      g.fillellipse(bush, p.x - r, p.y - r, 2*r, 2*r);//画填充椭圆的方法,x坐标、y坐标、宽、高:
 
      g.drawstring(words[j++], font, font_brush, p); // 标记
    }
 
    //连线
    var pencolor = color.fromargb(140, r.next(255), r.next(255), r.next(255));
    for (int i = 1; i < coordinate.count; i++) 
    {
      pen pen = new pen(pencolor, 2);
      g.drawline(pen, (point)coordinate[0], (point)coordinate[i]);
    }
    
    memorystream stream = new memorystream();  //保存绘制的图片
    img.save(stream, imageformat.jpeg);     //保存绘制的图片
    return stream;
  }
 
  private arraylist getxy(int len, int h, int w) 
  {
    arraylist al = new arraylist();
    double d = 50.0;
    var r = new random();
    int h1 = (int)(0.1 * h);
    int h2 = (int)(0.9 * h);
    int w1 = (int)(0.1 * w);
    int w2 = (int)(0.9 * w);
 
    while (al.count < len) 
    {
      point p = new point(r.next(h1,h2), r.next(w1,w2));
      bool add = true;
      foreach (point q in al) 
      {
        if (dist(p, q) < d)
        {
          add = false;
          break;
        }
      }
      if (add)
        al.add(p);
 
    }
 
    return al;
  }
 
  private double dist(point p1,point p2) 
  {
    return math.sqrt(math.abs(p1.x - p2.x) * math.abs(p1.x - p2.x) + math.abs(p1.y - p2.y) * math.abs(p1.y - p2.y));
  }
}

效果如下

C#实现网页画图功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: