服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现网页画图功能

C#实现网页画图功能

2022-08-30 11:52epleone C#

这篇文章主要为大家详细介绍了C#实现网页画图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

代码贴着保存下

?
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
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#实现网页画图功能

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

原文链接:https://blog.csdn.net/epleone/article/details/46128017

延伸 · 阅读

精彩推荐
  • C#详解使用C#编写SqlHelper类

    详解使用C#编写SqlHelper类

    本篇文章主要介绍了使用C#编写SqlHelper类,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    A-Mortal9512022-01-22
  • C#C# 程序集和反射详解

    C# 程序集和反射详解

    本文主要介绍了C# 程序集和反射的相关知识。具有一定的参考价值,下面跟着小编一起来看下吧...

    邹琼俊6972021-12-18
  • C#C#处理和对接HTTP接口请求的方法

    C#处理和对接HTTP接口请求的方法

    下面通过四步给大家介绍了c#处理和对接http接口请求的方法,分步骤介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看下吧...

    无烦9202021-12-03
  • C#C#递归算法之分而治之策略

    C#递归算法之分而治之策略

    分而治之的策略主要是将大量复杂的问题分成多个子问题,解决各个子问题,从而解决原问题,下面就让我们看看具体如何实现。...

    张玉彬11672021-11-25
  • C#C#接口归纳总结实例详解

    C#接口归纳总结实例详解

    本篇文章通过实例代码对接口做了详解,需要的朋友可以参考下...

    wbb12102021-12-31
  • C#C#数组中List, Dictionary的相互转换问题

    C#数组中List, Dictionary的相互转换问题

    这篇文章主要介绍了C#数组中List, Dictionary的相互转换问题,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    C#教程网7752021-12-16
  • C#C#实现BBcode转为Markdown的方法

    C#实现BBcode转为Markdown的方法

    这篇文章主要给大家介绍了关于C#实现BBcode转Markdown的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    lindexi9512022-02-20
  • C#C# FileStream实现多线程断点续传

    C# FileStream实现多线程断点续传

    这篇文章主要为大家详细介绍了C# FileStream实现多线程断点续传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    airforce0948052022-02-21