C#实现网页画图功能

本文实例为大家分享了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));
  }
}

效果如下

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

(0)

相关推荐

  • C#画图之饼图折线图的实现方法

    本文实例讲述了C#画图之饼图折线图的实现方法,是C#程序设计中非常实用的技巧.分享给大家供大家参考.具体方法分析如下: 显示图像的控件定义如下: public PlaceHolder PlaceHolder1; 各个图像的类别名称如下: PictureType    图形种类    5    chChartTypeBarClustered    簇状条形图    0    NULL PictureType    图形种类    7    chChartTypeBarClustered3D   

  • C#实现打开画图的同时载入图片、最大化显示画图窗体的方法

    本文实例讲述了C#实现打开画图的同时载入图片.最大化显示画图窗体的方法.分享给大家供大家参考.具体实现方法如下: /// <summary> /// 打开画图的同时载入图片 /// 最大化显示画图窗体 /// </summary> public static void OpenAppShowImage() { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); i

  • C#控件picturebox实现画图功能

    本文实例为大家分享了C# picturebox实现画图功能的具体代码,供大家参考,具体内容如下 在Form上添加 一个pictureBox,一个button控件 如图所示: 这样我们的绘画面板就弄好了,把pictureBox的dock属性设置为fill,按键为清屏的作用. private Point p1, p2;//定义两个点(启点,终点) private static bool drawing=false;//设置一个启动标志 private void pictureBox1_MouseDo

  • 使用C#实现在屏幕上画图效果的代码实例

    以下这段C#代码实现的功能是在屏幕上画图的效果!具体代码如下: 复制代码 代码如下: //DllImport所在的名字空间 using System.Runtime.InteropServices;[DllImport("User32.dll")]public extern static System.IntPtr GetDC(System.IntPtr hWnd);  private void button19_Click(object sender, EventArgs e){  

  • C#利用GDI+画图的基础实例教程

    前言 最近做一个微信公众号服务,有一些简单的图片处理功能.主要就是用户在页面操作,前端做一些立刻显示的效果,然后提交保存时后端真正修改原图. 从程序设计的角度看,GDI包括两部分:一部分是GDI对象,另一部分是GDI函数.GDI对象定义了GDI函数使用的工具和环境变量,而GDI函数使用GDI对象绘制各种图形,在C#中,进行图形程序编写时用到的是GDI+(Graphics Device Interface Plus图形设备接口)版本,GDI+是GDI的进一步扩展,它使我们编程更加方便. C#中的G

  • C#实现网页画图功能

    本文实例为大家分享了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; u

  • php基于ob_start(ob_gzhandler)实现网页压缩功能的方法

    本文实例讲述了php基于ob_start('ob_gzhandler')实现网页压缩功能的方法.分享给大家供大家参考,具体如下: PHP生成网页后传送给浏览器显示 ,页面的打开速度除了与用户的网速有关,往往也跟页面的大小有很关系,我们可以从网 页大小着手,以提高网页的响应速度. 下面的代码是一个压缩网页的例子,我们利用ob_gzip函数,使用ob_start将输出内容压缩后放到"缓冲区"后再输出 . PHP代码 //启用压缩 if(function_exists('ob_gzip'))

  • C#实现的滚动网页截图功能示例

    本文实例讲述了C#实现的滚动网页截图功能.分享给大家供大家参考,具体如下: 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; namespace WindowsFormsApplic

  • nodeJS实现简单网页爬虫功能的实例(分享)

    本文将使用nodeJS实现一个简单的网页爬虫功能 网页源码 使用http.get()方法获取网页源码,以hao123网站的头条页面为例 http://tuijian.hao123.com/hotrank var http = require('http'); http.get('http://tuijian.hao123.com/hotrank',function(res){ var data = ''; res.on('data',function(chunk){ data += chunk;

  • JavaScript实现自动生成网页元素功能(按钮、文本等)

    创建元素的方法: 1.利用createTextNode()创建一个文本对象 2.利用createElement()创建一个标签对象 3.直接利用容器标签中的一个属性:innerHTML-----本质上改该标签容器中的"html代码",不是我们认为的对象树的操作 详解代码: <body> <input type="button" value="创建并添加节点1" onclick="addNode1()"/>

  • 基于jQuery实现网页打印功能

    直接上代码 <!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>index</title> </head> <body> <div style="text-align:cent

  • 禁用backspace网页回退功能的实现代码

    实例如下: <script language="JavaScript"> document.onkeydown = check; function check(e) { var code; if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; if (((event.keyCode == 8) && //BackSpac

  • Android使用WebView实现全屏切换播放网页视频功能

    首先写布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="fill_parent" and

  • php实现的支付宝网页支付功能示例【基于TP5框架】

    本文实例讲述了php实现的支付宝网页支付功能.分享给大家供大家参考,具体如下: 1.提交支付请求 require_once './payment/zfb/pagepay/service/AlipayTradeService.php'; require_once './payment/zfb/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php'; $payInfo=Db::name('pay_type')->where(['pay_i

  • laravel5.5添加echarts实现画图功能的方法

    一.下载echarts 我用的是3.X版本,下载地址 二.在页面中引入echarts <script type="text/javascript" src="/js/echarts.min.js"></script> 我把下载下来的echarts.min.js放在了public/js/目录下 三.通过post的请求获取数据并在页面展示 1.添加路由 Route::get('/test2', 'CunliangController@test2'

随机推荐