码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-05-15 19:25:45      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

//在一个页面中添加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;
        }

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

标签:

原文地址:http://www.cnblogs.com/zecVip/p/4506667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!