c#+GUI在aspx页面画图

画图的方式有两种:

1.将图片内容响应到页面,将image控件的src或imageurl指定为这个页面

代码:aspx页面的cs文件

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class KuTu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            DrawImage();
    }

    private void DrawImage()
    {

        //设置页面的content type为JPEG文件
        //并且清除所有的响应头部信息 
        Response.ContentType = "image/jpeg";
        Response.Clear();
        //对响应作出缓冲以便处理完成后发送页面 
        Response.BufferOutput = true;
        //创建一字体风格 
        Font rectangleFont = new Font(
        "Arial", 10, FontStyle.Bold);
        //创建整数变量 
        int height = 100;
        int width = 200;
        //创建一个随机数字生成器并且基于它创建
        //变量值 
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);
        //创建一张位图并且使用它创建一个
        //Graphics对象 
        Bitmap bmp = new Bitmap(
        width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Clear(Color.LightGray);
        //使用这个Graphics对象绘制3个矩形 
        g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width - 3, height - 3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);
        //使用这个Graphics对象输出一个字符串
        // on the rectangles. 
        g.DrawString(
        "ASP.NET Samples", rectangleFont,
        SystemBrushes.WindowText, new PointF(10, 40));
        //在其中两个矩形上添加颜色 
        g.FillRectangle(
        new SolidBrush(
        Color.FromArgb(a, 255, 128, 255)),
        x, 20, 100, 50);
        g.FillRectangle(
        new LinearGradientBrush(
        new Point(x, 10),
        new Point(x1 + 75, 50 + 30),
        Color.FromArgb(128, 0, 0, 128),
        Color.FromArgb(255, 255, 255, 240)),
        x1, 50, 75, 30);
        //把位图保存到响应流中并且把它转换成JPEG格式 
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        //释放掉Graphics对象和位图所使用的内存空间 
        g.Dispose();
        bmp.Dispose();
        //把输出结果发送到客户端 
        Response.Flush();
    }

}

2.将图片保存到指定的路径,将image控件的src或imageurl指定为该路径

代码:在类文件中画、webservice中画、aspx的cs文件中画皆可

private void MoveImage(int x)
    {
        //设置页面的content type为JPEG文件
        //并且清除所有的响应头部信息 
        Response.ContentType = "image/jpeg";
        Response.Clear();
        //对响应作出缓冲以便处理完成后发送页面 
        Response.BufferOutput = true;
        string str = DateTime.Now.ToString();
        Bitmap image = new Bitmap(500, 60);
        Graphics g = Graphics.FromImage(image);
        string thefullname = Server.MapPath("image") + "\\as.jpg";
        g.Clear(Color.White);
        //g.DrawString(str,new Font("Courier New", 10),new SolidBrush(Color.Red),20,5);
        Pen p = new Pen(Color.Red);
        g.DrawRectangle(p, x, 30, 30, 20);
        //Graphics 类还有很多绘图方法可以绘制 直线、曲线、圆等等 
        //image.Save(thefullname, System.Drawing.Imaging.ImageFormat.Jpeg);
        //return "image/as.jpg"; 
       image.Save(Response.OutputStream, ImageFormat.Jpeg);
        //释放掉Graphics对象和位图所使用的内存空间 
        g.Dispose();
        image.Dispose();
        //把输出结果发送到客户端 
        Response.Flush();
    }
原文地址:https://www.cnblogs.com/lvcha/p/1699180.html