C#照片预览,好处是图片不在项目中也可以查看

// 在一个页面中添加image控件,后台指向一个新页面,在新页面获取图片的二进制流,再展现在页面上
<body>
    <div  class= " pNavigation ">
        <div style= " overflow: hidden; ">
            <img alt= ""  class= " img_Navigation " src= " /Style/Images/Default/pixel.gif " />当前位置:消防设备管理
            >> 消防设备图纸查看
        </div>
    </div>
    <form id= " form1 " runat= " server ">
    <table id= " table " cellpadding= " 0 " cellspacing= " 0 " border= " 0 " style= " width: 100%;
        margin: 0px; " >
        <tr>
            <td align= " center ">
                <img id= " imgphoto " src= " ~/Style/Images/Photo/nopic.gif " runat= " server " style= " margin: 2px;
                    margin-left: 12px; height: 280px; width: 222px; "  />
            </td>
        </tr>
    </table>
    </form>
</body>
protected  void Page_Load( object sender, EventArgs e)
        {
             try
            {
                Model.LOGISTICS_BUILDINGEQUIPMENTPIC model = cBll.GetList( "  where t.LYH=' " + Request.QueryString[ " lyh "] +  " ' and t.LC=' " + Request.QueryString[ " lc "] +  " ' """).FirstOrDefault();
                 if (model !=  null)
                {
                     this.imgphoto.Src =  " ShowPhoto.aspx?lyh= " + Request.QueryString[ " lyh "] +  " &lc= " + Request.QueryString[ " lc "] +  " &r= " +  new Random().Next().ToString(CultureInfo.InvariantCulture);
                    
                }
            }
             catch (Exception exception)
            {
                Log.fWriterLog( " 住校管理之床位安排查看页(学生信息查看)页面初始化异常: " + exception.Message, exception);
            }
        }

// 新页面输出图片二进制流
///   <summary>
        
///  获取图片
        
///   </summary>
        
///   <param name="lyh"></param>
        
///   <param name="lc"></param>
         protected  void GetShowPhoto( string lyh, string lc)
        {
            Model.LOGISTICS_BUILDINGEQUIPMENTPIC model = cBll.GetList( "  where t.LYH=' " + lyh +  " ' and t.LC=' " + lc +  " ' """).FirstOrDefault();
             string basePath = Hzjg.Common.Config.ConfigManage.fGetAppConfig( " SaveFilePath ");
            basePath = basePath.Substring( 0, basePath.Length -  1);
             byte[] byteImg =  null; // 图片流
            Stream stream =  null;
             if (model !=  null)
            {
                 // 把文件转化为二进制流
                 string path = basePath + model.PICTUREPATH.Replace( " / "" \\ ");
                byteImg = ConvertToBinary(path);
                stream =  new MemoryStream(byteImg);
            }
             else
            {
                FileStream f =  new FileStream(Server.MapPath( " ~/Style/Images/Photo/nopic.jpg "), FileMode.Open, FileAccess.Read);
                byteImg =  new  byte[f.Length];
                f.Read(byteImg,  0, byteImg.Length);
                f.Close();
                stream =  new MemoryStream(byteImg);
            }
             var img = (Bitmap)Image.FromStream(stream,  false);  // 转换成Bitmap 
            Response.Buffer =  false;
            Response.ContentType =  " image/jpg ";
            Response.AddHeader( " Content-Disposition "" attachment;filename=photo.jpg ");  // 照片名称叫photo.jpg 
            Response.BinaryWrite(byteImg);  // 写入二进制流 
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

         ///   <summary>
        
///  把文件转化为二进制流
        
///   </summary>
        
///   <param name="Path"> 文件路径 </param>
        
///   <returns></returns>
         public  static  byte[] ConvertToBinary( string Path)
        {
            FileStream stream =  new FileInfo(Path).OpenRead();
             byte[] buffer =  new  byte[stream.Length];
            stream.Read(buffer,  0, Convert.ToInt32(stream.Length));
             return buffer;
        }

转载于:https://www.cnblogs.com/zecVip/p/4506667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值