将图片的二进制字节 在HTML页面中显示

两种方法:

后端的一般处理程序:Imge.ashx

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls; namespace Test
{
/// <summary>
/// Imge 的摘要说明
/// </summary>
public class Imge : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{ #region 方法一
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Stream str = new FileUpload().PostedFile.InputStream;
System.Drawing.Bitmap map = new System.Drawing.Bitmap(str);
map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "image/Jpeg";
context.Response.BinaryWrite(ms.ToArray()); //将二进制字节输出到页面
#endregion #region 方法二
System.IO.FileStream fs = new System.IO.FileStream("Filename", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] datas = new byte[fs.Length];
fs.Read(datas, , Convert.ToInt32(fs.Length));
fs.Close();
context.Response.OutputStream.Write(datas, , Convert.ToInt32(fs.Length));
context.Response.End();
#endregion
} public bool IsReusable
{
get
{
return false;
}
}
}
}

HTML页面代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<img src="/Imge.ashx" /> <!---图片的src指向Imge.ashx 就可以--->
</body>
</html>
上一篇:js-ES6学习笔记-module(2)


下一篇:ubuntu16.04 禁用Guest用户