flash 无刷新上传文件 图片缩略图 后台

来源:互联网 发布:知乎 飞利浦电动剃须刀 编辑:程序博客网 时间:2024/09/21 09:26
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using System.Drawing;using System.Drawing.Imaging;namespace BookShop.Web.ashx{ /// /// upload 的摘要说明 /// public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); string action = context.Request["action"]; if (action == "up") { HttpPostedFile file = context.Request.Files["Filedata"]; //获取文件名 string fileName = file.FileName; //获取文件的后缀名 string fileExt = Path.GetExtension(context.Request.MapPath(fileName)).ToLower(); //得到水印的图片路径 string waterFile = context.Request.MapPath("/Images/chongzhi.jpg"); //判断文件类型 if (fileExt == ".jpg" || fileExt == ".gif") { //创建文件夹 string dir = "/ImageUpload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; //创建随机名称 string name = Guid.NewGuid().ToString(); Directory.CreateDirectory(Path.GetDirectoryName(context.Request.MapPath(dir))); string fullDir = dir + name + fileExt; //给图片加水印的功能 //获取要添加水印的图片 using (Image img = Image.FromStream(file.InputStream)) { //创建一个水印的图片 using (Image waterImg = Image.FromFile(waterFile)) { //创建一个画板 using (Bitmap bitMap = new Bitmap(img.Width, img.Height)) { //创建画笔 Graphics g = Graphics.FromImage(bitMap); //画水印 g.DrawImage(bitMap, new Rectangle(img.Width - waterImg.Width, img.Height - waterImg.Height, waterImg.Width, waterImg.Height), 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel, SetImageAttr(50)); //保存文件 file.SaveAs(context.Request.MapPath(fullDir)); //把图片的途径传过期 同时把宽度和高度传过期 context.Response.Write("ok:" + fullDir+":"+img.Width+":"+img.Height); } } } //缩略图 int width=250; int height=250; using (Image img = Image.FromStream(file.InputStream)) { if (img.Width > img.Height) { height = width * img.Height / img.Width; } else { width = height * img.Width / img.Height; } //画缩略图 using (Bitmap bmp = new Bitmap(width, height)) { //创建画笔 using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(img, 0, 0, width, height); //保存文件 bmp.Save(context.Server.MapPath(fullDir)); } } } } else { //文件类型不正确 context.Response.Write("文件类型错误"); } } else if (action == "cut") { //接受数据 int x = Convert.ToInt32(context.Request.Form["x"]); int y = Convert.ToInt32(context.Request.Form["y"]); int width = Convert.ToInt32(context.Request.Form["width"]); int height = Convert.ToInt32(context.Request.Form["height"]); string imgUrl = context.Request.Form["imgUrl"]; //图片截取 先创建一个画板 using (Bitmap bitMap = new Bitmap(width, height)) { //创建一支画笔 using (Graphics g = Graphics.FromImage(bitMap)) { //画那张图 using (Image img = Image.FromFile(context.Request.MapPath(imgUrl))) { g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel); //得到的图 创建一个新的文件名称 string newFile = Guid.NewGuid().ToString().Substring(0, 8); //把图片保存到文件夹中 bitMap.Save(context.Server.MapPath("/ImageUpload/"+newFile)); //把得到是缩略图返回回去 context.Response.Write("/ImageUpload/" + newFile); } } } } } //图片保存的时候生成缩略图 public ImageAttributes SetImageAttr(float ff) { //定义矩阵:RGBAW float[][] f = { new float[]{ff/100f,0,0,0,0}, new float[]{0,1,0,0,0}, new float[]{0,0,1,0,0}, new float[]{0,0,0,ff/100f,0}, new float[]{0,0,0,0,1} }; ColorMatrix colorMatrix = new ColorMatrix(f);//使用自己定义的矩阵。 ImageAttributes imageAttr = new ImageAttributes();//指定了图像的颜色的信息 imageAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);//用我们自己定义的矩阵跳转图片的颜色信息。 return imageAttr; } public bool IsReusable { get { return false; } } }}
原创粉丝点击