System.IO.FileInfo f= new System.IO.FileInfo(@"\a.jpg");
System.IO.StreamReader r =  f.OpenText();
string a= r.ReadToEnd();
r.close();
通过以上操作得到了a.jpg图的string形式的 变量a 
问,怎样把a还原成图片显示在页面上.

解决方案 »

  1.   

    Bitmap类,构造函数传入stream。
      

  2.   

    TO: shrinerain(圣影雨)
    构造了Bitmap后呢,要怎样显示出来呀?恕小弟愚昧,能否提供一下代码.
      

  3.   

    给你个简单事例:
    后台代码:
    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;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    namespace CommonFunction.test
    {
    /// <summary>
    /// ValitionNO 的摘要说明。
    /// </summary>
    public class ValidateCode : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Panel Panel1;

    private void Page_Load(object sender, System.EventArgs e)
    {

    if(!this.IsPostBack)
    {
    this.CreateCheckCodeImage(GenerateCheckCode());
    }
    }
    private string GenerateCheckCode()
    {
    int number;
    char code;
    string checkCode = String.Empty; System.Random rd = new Random(); int i=1000 + rd.Next(8999); checkCode =i.ToString(); Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode;
    } private void CreateCheckCodeImage(string checkCode)
    {
    if(checkCode == null || checkCode.Trim() == String.Empty)
    return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
    Graphics g = Graphics.FromImage(image); try
    {
    //生成随机生成器
    Random random = new Random(); //清空图片背景色
    g.Clear(Color.Gainsboro);
    //Font font = new Font("Times New Roman",13);
    Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Regular));
    System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
    g.DrawString(checkCode, font, brush, 2, 2); //画图片的边框线
    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    Response.ClearContent();
    Response.ContentType = "image/Gif";
    Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
    g.Dispose();
    image.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
    }
    }
    HTML代码:
    <%@ Page language="c#" Codebehind="CheckCode1.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.test.ValidateCode" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" align="center" border="1">
    <TR>
    <TD></TD>
    <TD align="center">
    <asp:Panel id="Panel1" runat="server">Panel</asp:Panel></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    </TABLE>
    </form>
    </body>
    </HTML>
      

  4.   

    对图像解码
    FileStream fs =new FileStream(@"..\test.gif",System.IO.FileMode.Create);
    BinaryWriter bw=new BinaryWriter(fs);
    int readByte=0;
    int bytesToRead=100;
    byte[] base64Buffer=new byte[bytesToRead];
    do
      {
    readByte= r.ReadBase64(base64Buffer,0,bytesToRead);
    bw.Write(base64Buffer,0,readByte);
     }while(readByte>=bytesToRead);
    bw.Flush();
    bw.Close();
    刚写的,这样应该可以了吧。
      

  5.   

    然后<IMG src="CheckCode1.aspx">就可以显示图片拉
      

  6.   

    看看这个:
    //ImgID:上一个页面所传递的图像ID,
    //注:上一个页面中的一个gridview列出了数据库中的所有图像,(数据文件为xml文件),
    //图像以二进制形式存储于xml文件中.
    private void Page_Load(object sender, System.EventArgs e)
    {
    int ImgID = Convert.ToInt32(Request.QueryString["ID"]); //ID为图片ID 
    //建立数据库链接
    string fileName = Server.MapPath(".\\WriteXml.xml");   //要打开的文件

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(fileName);
    XmlNodeList node =  xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
    if(node!=null)
    {
    string strType = node.Item(1).InnerText;
    string strData =node.Item(4).InnerText;
    int nSize = int.Parse(node.Item(2).InnerText); Response.ContentType = strType;//设定输出文件类型
    //输出图象文件二进制数制
    Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize); 
    Response.End();
    //也可以保存为图像
    // FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
    // fs.Write((Convert.FromBase64String(strData), 0,nSize);
    // fs.Close();
    } }
      

  7.   

    给你个简单事例:
    后台代码:
    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;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    namespace CommonFunction.test
    {
    /// <summary>
    /// ValitionNO 的摘要说明。
    /// </summary>
    public class ValidateCode : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Panel Panel1;

    private void Page_Load(object sender, System.EventArgs e)
    {

    if(!this.IsPostBack)
    {
    this.CreateCheckCodeImage(GenerateCheckCode());
    }
    }
    private string GenerateCheckCode()
    {
    int number;
    char code;
    string checkCode = String.Empty; System.Random rd = new Random(); int i=1000 + rd.Next(8999); checkCode =i.ToString(); Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode;
    } private void CreateCheckCodeImage(string checkCode)
    {
    if(checkCode == null || checkCode.Trim() == String.Empty)
    return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
    Graphics g = Graphics.FromImage(image); try
    {
    //生成随机生成器
    Random random = new Random(); //清空图片背景色
    g.Clear(Color.Gainsboro);
    //Font font = new Font("Times New Roman",13);
    Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Regular));
    System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
    g.DrawString(checkCode, font, brush, 2, 2); //画图片的边框线
    g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    Response.ClearContent();
    Response.ContentType = "image/Gif";
    Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
    g.Dispose();
    image.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
    }
    }
    HTML代码:
    <%@ Page language="c#" Codebehind="CheckCode1.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.test.ValidateCode" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" align="center" border="1">
    <TR>
    <TD></TD>
    <TD align="center">
    <asp:Panel id="Panel1" runat="server">Panel</asp:Panel></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    </TABLE>
    </form>
    </body>
    </HTML>
      

  8.   

    能不能就在一个界面上处理完.可以在一个界面处理完。是先把流存储成图片到本地,然后再用<img>显示就行了。
      

  9.   

    谢谢大家的回贴,其实我的意思是在一个页面中处理,把图片显示出来,但并不是整个页面只输出一张图.,图只是页面的一部分.
    1.在多个页面中处理(大概就这各形式(<IMG src="处理图的页面.aspx">)这涉及到页面传参的问题.当前页面(例如说显示图页面.aspx)已经从数据库里得到一个DataTable,怎样把这个DataTable传到处理图的页面.aspx中?
    2.把流存储成图片到本地,然后再用<img>显示.这涉及到对文件创建和删除的权限,于及这带来文件命名,文件定时清除等麻烦(例如:当1000个用户并发访问该页面时,要生成1000张图,1000张图文件名要有一个规则生成和删除吧)
    ===============
    再次感谢大家的支持,,希望能有更好的解决方案.