NET文件流输出到Web页面显示(jpg、png、tif...)

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int cishu = Config.GetInt32SafeFromQueryString(this, "cishu", -1);
                string FilePath = Request["FilePath"];
                //定义图片流
                Byte[] byteImg = null;
                if (FilePath.Length > 0 && FilePath.Contains("tif"))
                {
                    var imginfo = System.Drawing.Image.FromFile(FilePath);
                    MemoryStream msinfo = new MemoryStream();
                    //将文件保留到指定 流中
                    imginfo.Save(msinfo, System.Drawing.Imaging.ImageFormat.Bmp);
                    byteImg = msinfo.ToArray();
                }
                else
                {
                    byteImg = FilePath == string.Empty ? new byte[0] : File.ReadAllBytes(FilePath);
                }
                获取最后一个 "\" 的索引
                //int startindex = FilePath.LastIndexOf(@"\") + 1;
                获取文件名称
                //string FileName = FilePath.Substring(startindex, FilePath.Length - startindex);

                Response.Clear();//这里能解决64位windows Server2008服务器和2003服务器下载要求输入密码的问题 
                //Response.ContentType = "image";
                //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                //Response.OutputStream.Write(byteImg, 0, byteImg.Length);
                //Response.Flush();
                //Response.End();

                //ConvertTiff2Jpeg(FilePath, FilePath.Replace("tif","jpg"));

                if (cishu != -1)
                {
                    Stream stream = new MemoryStream(byteImg);
                    System.Drawing.Bitmap img = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(stream, false); //转换成Bitmap 


                    if (cishu == 1)
                    {
                        //顺时针旋转90
                        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }
                    else if (cishu == 2)
                    {
                        //顺时针旋转180
                        img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    }
                    else if (cishu == 3)
                    {
                        //顺时针旋转270
                        img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    }
                    else
                    {
                        //默认顺时针旋转90
                        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }

                    MemoryStream ms = new MemoryStream();
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    Byte[] bytems = ms.ToArray();


                    Response.ContentType = "image/jpg";
                    Response.Buffer = false;
                    Response.BinaryWrite(bytems);//写入二进制流
                    Response.End();

                }
                else
                {

                    Response.ContentType = "image/jpg";
                    Response.Buffer = false;
                    Response.BinaryWrite(byteImg);//写入二进制流 
                    Response.End();
                }
            }

        }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值