hanlder.ashx文件
- public class Handler4 : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- #region 将图片转成Base64
- FileStream fs = new FileStream("I26S-1280x1024-s.jpg", FileMode.Open, FileAccess.Read);
- byte[] buffur = new byte[fs.Length];
- fs.Read(buffur, 0, (int)fs.Length);
- string strPic = Convert.ToBase64String(buffur);
- #endregion
- #region 将Base64转成byte[]
- byte[] buffurPic = Convert.FromBase64String(strPic);
- #endregion
- context.Response.ContentType = "image/jpeg";
- context.Response.Clear();
- context.Response.BufferOutput = true;
- context.Response.OutputStream.Write(buffurPic, 0, buffurPic.Length);
- context.Response.Flush();
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
页面文件
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src="js/jquery.js"></script>
- </head>
- <body>
- <script language="JavaScript">
- $(document).ready(function () {
- $('#img1').attr('src', '/Handler/Handler4.ashx');
- });
- </script>
- <img id="img1" src="" />
- </body>
- </html>
MVC Action方法
- public ActionResult GetImg()
- {
- #region 将图片转成Base64
- FileStream fs = new FileStream("I26S-1280x1024-s.jpg", FileMode.Open, FileAccess.Read);
- byte[] buffur = new byte[fs.Length];
- fs.Read(buffur, 0, (int)fs.Length);
- string strPic = Convert.ToBase64String(buffur);
- #endregion
- #region 将Base64转成byte[]
- byte[] buffurPic = Convert.FromBase64String(strPic);
- #endregion
- return File(buffurPic, "image/jpeg");
- }
页面文件
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>PicShow</title>
- <script src="~/jquery/jquery.min.js"></script>
- <script language="JavaScript">
- $(document).ready(function () {
- $('#img1').attr('src', '@Url.Action("GetImg", "Selector")');
- });
- </script>
- </head>
- <body>
- <img id="img1" /><br /><br />
- <img id="img2" src="@Url.Action("GetImg", "Selector")" />
- </body>
- </html>