zoukankan      html  css  js  c++  java
  • 将图片的二进制字节 在HTML页面中显示

    两种方法:

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

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI.WebControls;
     6 
     7 namespace Test
     8 {
     9     /// <summary>
    10     /// Imge 的摘要说明
    11     /// </summary>
    12     public class Imge : IHttpHandler
    13     {
    14 
    15         public void ProcessRequest(HttpContext context)
    16         {
    17 
    18             #region 方法一
    19             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    20             System.IO.Stream str = new FileUpload().PostedFile.InputStream;
    21             System.Drawing.Bitmap map = new System.Drawing.Bitmap(str);
    22             map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    23             context.Response.ClearContent();
    24             context.Response.ContentType = "image/Jpeg";
    25             context.Response.BinaryWrite(ms.ToArray());  //将二进制字节输出到页面 
    26             #endregion
    27 
    28             #region 方法二
    29             System.IO.FileStream fs = new System.IO.FileStream("Filename", System.IO.FileMode.Open, System.IO.FileAccess.Read);
    30             byte[] datas = new byte[fs.Length];
    31             fs.Read(datas, 0, Convert.ToInt32(fs.Length));
    32             fs.Close();
    33             context.Response.OutputStream.Write(datas, 0, Convert.ToInt32(fs.Length));
    34             context.Response.End();
    35             #endregion
    36         }
    37 
    38         public bool IsReusable
    39         {
    40             get
    41             {
    42                 return false;
    43             }
    44         }
    45     }
    46 }
    View Code

    HTML页面代码:

    1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    2 <html xmlns="http://www.w3.org/1999/xhtml">
    3 <head>
    4     <title></title>
    5 </head>
    6 <body>
    7 <img src="/Imge.ashx" />  <!---图片的src指向Imge.ashx 就可以--->
    8 </body>
    9 </html>
  • 相关阅读:
    【HackerRank】Sherlock and MiniMax
    【HackerRank】Game Of Rotation
    【HackerRank】Bus Station
    【HackerRank】 有洞的地图
    【HackerRank】Sherlock and Array
    【HackerRank】QuickSort(稳定快排,空间复杂度O(n))
    【HackerRank】 The Full Counting Sort
    51获取6050原始数据并通过匿名上位机显示波形
    51单片机之IIC通信原理及软件仿真
    寻迹小车开发日记
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5223705.html
Copyright © 2011-2022 走看看