一、缩略图
--》首先找到图片、或和用户的上传结合在一起
--》创建一个画布 与之前不同的是我们的画布给它一个固定的大小
-->创建一个画笔
--》
context.Response.ContentType = "image/jpeg"; //得到图片的路径 string filePath = context.Server.MapPath("image/1.jpg"); //读取图片 using (Image img = Image.FromFile(filePath)) { //创建一个画布,固定尺寸(也就是缩略图的尺寸) using (Bitmap map = new Bitmap(200, 300)) { //创建画笔 using (Graphics g = Graphics.FromImage(map)) { //把图片画到画布上,注意第二个参数是设置的画布的宽和高 g.DrawImage(img, new Rectangle(0, 0, map.Width, map.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); //输出 map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } }
二、文件下载
context.Response.ContentType = "text/html"; //获得客户端请求的下载文件名 string downFileName=context .Request.QueryString["name"]; if (!string .IsNullOrEmpty(downFileName)) { //判断客户端请求下载的文件在服务端是否存在 //先获得该文件的全路径 string filePath = context.Server.MapPath(downFileName); if (File.Exists(filePath)) { //对文件爱你路径进行转码,防止向客户端输出时乱码 string file=context.Server.UrlEncode(filePath); //想用户弹出一个保存对话框 context.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file + "\""); //如果存在,通过writeFile(_)方法向客户端输出文件 context.Response.WriteFile(filePath); } else { context.Response.Write("文件不存在,请确定文件的路径是否正确"); } }
时间: 2024-12-19 06:57:43