• C#动态创建图像的方法


    在过去,在Web应用程序中动态创建图像简直就是一场恶梦。但如今在ASP.NET中却变得非常容易了。动态创建图像通常用在动态生成附加码这一方面,图像形式的附加码是加强Web信息系统安全的一项十分重要的措施。本文就给出在C#中动态创建图像的方法。

        在C#中动态创建的图像,通常是以*.aspx网页的形式返回的。所以,通常在WebForm2.aspx中动态创建图像,然后,在WebForm1.aspx中的Image1控件调用WebForm2.aspx,这样,动态创建的图像就显示在WebForm1.aspx网页的Image1控件中了。具体请参见下面的实现代码。

        1、在WebForm2.aspx中动态生成图像代码,在Page_Load成员函数中生成。

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace DynamicallyCreateBMP
    {
     /// <summary>
     /// WebForm2 的摘要说明。
     /// </summary>
     public class WebForm2 : System.Web.UI.Page
     {
     
      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
       Bitmap objBitmap=new Bitmap(120,30);
       Graphics objGraphics=Graphics.FromImage(objBitmap);
       objGraphics.FillRectangle(new SolidBrush(Color.LightBlue),0,0,120,30);
       objGraphics.FillEllipse(new SolidBrush(Color.Blue),3,9,10,10);
       objGraphics.FillEllipse(new SolidBrush(Color.Yellow),4,10,8,8);
       objGraphics.DrawString("www.zzg.3126.net",new Font("Tahoma",8),new SolidBrush(Color.Green),16,8);
       this.Page.Response.Clear();
       this.Page.Response.ContentType="image/jpeg";
       objBitmap.Save(this.Page.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
       objGraphics.Dispose();
       objBitmap.Dispose();
      }

      #region Web 窗体设计器生成的代码
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {    
       this.Load += new System.EventHandler(this.Page_Load);

      }
      #endregion
     }
    }


        2、在WebForm1.aspx的Image1控件中装入图像WebForm2.aspx。

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace DynamicallyCreateBMP
    {
     /// <summary>
     /// WebForm1 的摘要说明。
     /// </summary>
     public class WebForm1 : System.Web.UI.Page
     {
      protected System.Web.UI.WebControls.Image Image1;
     
      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
       this.Image1.ImageUrl="webform2.aspx";  
      }

      #region Web 窗体设计器生成的代码
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {    
       this.Load += new System.EventHandler(this.Page_Load);

      }
      #endregion
     }
    }
  • 相关阅读:
    codeforces 368(div 2)前三题
    codeforces 368(div 2)前三题
    hihocoder编程练习赛6+多重背包的各种姿势
    hihocoder编程练习赛6+多重背包的各种姿势
    hihocoder1077,线段树单点修改的一点小技巧
    hihocoder1077,线段树单点修改的一点小技巧
    [NOIP2013]货车运输,最大生成树+LCA
    [NOIP2013]货车运输,最大生成树+LCA
    hihocoder 1080 线段树:区间加法&赋值
    hihocoder 1080 线段树:区间加法&赋值
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231682.html
Copyright © 2020-2023  润新知