代码之家  ›  专栏  ›  技术社区  ›  Booji Boy

如何在ASP.NET中将文本和图像写入response.outputstream

  •  0
  • Booji Boy  · 技术社区  · 15 年前

    我正在绘制图像并使用以下代码将其输出到网页:

    Bitmap image = new Bitmap(350, 350);
    Graphics g = Graphics.FromImage(image);
    // code using g to draw image here
    image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
    

    这个很好用。现在我想在输出中添加一些文本(javascript)。但是,当我这样做*时,要么文本不显示,要么图像显示为垃圾。

    正确的方法是什么?

    (*-我更改了ContentType并使用了reponse.write。还尝试将文本的字节数组写入输出流。)

    3 回复  |  直到 13 年前
        1
  •  3
  •   Lou Franco    15 年前

    您需要使用HTML响应,HTML包含您的javascript和 <img> 导致另一个请求的标记,您将使用此图像进行响应。

    可能会改变 <IMG & GT; 请求将此图像发送给 <iframe> 将SRC设置为请求。

    或者,它可以是 <script> 返回javascript并将其添加到DOM的标记。

        2
  •  3
  •   hova    15 年前

    如果您仍然希望在一个请求中执行此操作,那么 http://danielmclaren.net/2008/03/embedding-base64-image-data-into-a-webpage 可能是最好的方法。它涉及到base64编码的数据 src 的属性 img 标签。即

    <img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />
    
        3
  •  3
  •   Henry Gao    15 年前

    您可以为您的图像代码编写一个名为imagegrab.aspx的单独文件,并使用javascript在另一个文件中调用该文件。它应该工作得很好。例如

    <img border=0 height=150 src='ImageGrab.aspx?seqid=3'>
    

    imagegrab.aspx.cs看起来像

    public partial class ImageGrab : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                byte[] byteData = new byte[0];
    
                // fetch the value of parameter into the byte array
    
                string seq_id = Request.QueryString["seqid"];
    
                if (string.IsNullOrEmpty(seq_id))
                {
                    seq_id = "214";
                }
    
    
                byteData = DBManager.GetBinaryData(seq_id);
    
                Response.Clear();
    
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite(byteData);
                Response.End();
            }
            catch (Exception exc)
            {
                Response.Write(exc.Message);
            }
        }
    
    }