zoukankan      html  css  js  c++  java
  • c#图片输出

    1:  Response.BinaryWrite() 其实就是和输出文字一样 只是图片是流的形式;

            delegate long myDel(int first, int second);
            FileStream fs;
            byte[] _byte;
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.ContentType = "image/jpeg";
                fs = new FileStream(Server.MapPath("~/file/") + HttpUtility.UrlDecode("你好.jpg"), FileMode.Open);
                _byte = new byte[fs.Length];
                IAsyncResult ir = fs.BeginRead(_byte, 0, (int)fs.Length, null, null);
                fs.Read(_byte, 0, fs.EndRead(ir));
                fs.Close();
                Response.BinaryWrite(_byte);
    
            }

    2:循环

            protected void Button1_Click(object sender, EventArgs e)
            {
                string filePath = Server.MapPath("~/file/") + HttpUtility.UrlDecode("你好.jpg");
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("你好.jpg", Encoding.UTF8));
                //FileInfo fi = new FileInfo(filePath);
                //Response.WriteFile(fi.FullName);
                //Response.End();
    
    
                int length = 0;
                byte[] arr = new byte[1024 * 1024 * 2];
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    length = fs.Read(arr, 0, arr.Length);
                }
                byte[] arrNew = new byte[length];
                System.Buffer.BlockCopy(arr, 0, arrNew, 0, length);
                Response.BinaryWrite(arrNew);
            }
            public static void ResponseFile(string filePath, HttpContext context, bool hasfileName)
            {
                Stream iStream = null;
                byte[] buffer = new Byte[10000];
                int length;
                long dataToRead;
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath), Encoding.UTF8));
                using (iStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    dataToRead = iStream.Length;
                    while (dataToRead > 0)
                    {
                        if (context.Response.IsClientConnected)
                        {
                            length = iStream.Read(buffer, 0, 10000);
                            context.Response.OutputStream.Write(buffer, 0, length);
                            context.Response.Flush();
                            buffer = new Byte[10000];
                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            dataToRead = -1;
                        }
                    }
                }
  • 相关阅读:
    NPOI导出excel表格应用
    通过用户密码获取共享文件
    sql之left join、right join、inner join的区别
    C#日志编写
    C#预编译指令
    匿名类型(C# 编程指南)
    反射
    SQL通用查询
    Lambda 表达式(C# 编程指南)
    自定义用户控件编写——(文件夹目录选择)
  • 原文地址:https://www.cnblogs.com/y112102/p/3139574.html
Copyright © 2011-2022 走看看