zoukankan      html  css  js  c++  java
  • 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=" 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;  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;
            }
  • 相关阅读:
    Dart语言简介
    Flutter简介
    Flutter之环境配置详解for mac版本
    mac 安卓生成证书(uniapp项目安卓证书申请)
    IOS开发者账号申请流程以及开发证书与配置文件的使用
    解读typescript中 super关键字的用法
    解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed
    JS pc端和移动端共同实现复制到剪贴板功能实现
    Web前端接入人机识别验证码---腾讯防水墙
    Unity3D Demo项目开发记录
  • 原文地址:https://www.cnblogs.com/zecVip/p/4506667.html
Copyright © 2011-2022 走看看