使用WebUploader使用,及使用后测试横拍或竖拍图片图片方向不对等解决方案
2014-09-12 15:14
381 查看
<%@ WebHandler Language="C#" Class="UploadHandleringNew" %> using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.IO; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using ysw.BLL; using ysw.Models; using System.Drawing; public class UploadHandleringNew : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public string camera = String.Empty;//机型 public string focalLength = String.Empty;//焦距 public string shutterSpeed = String.Empty;//速度 public string aperture = String.Empty;//光圈 public string iso = String.Empty;//感光度 public string treePath = System.Web.HttpContext.Current.Server.MapPath("~/"); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; context.Response.ContentEncoding = System.Text.Encoding.UTF8; System.Collections.Generic.IList<TempImages> list = new System.Collections.Generic.List<TempImages>(); if (context.Session["TempImageList"] != null) { list = context.Session["TempImageList"] as System.Collections.Generic.IList<TempImages>; } if (list.Count >= 6) { context.Response.Write("上传数量已经超出系统限制的6个文件"); return; } else { HttpPostedFile file = context.Request.Files[0]; string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\"; if (file != null) { if (!System.IO.Directory.Exists(uploadPath)) { System.IO.Directory.CreateDirectory(uploadPath); } Random random = new Random(); string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "-" + random.Next(999999999) + file.FileName.Substring(file.FileName.Length - 4); // 文件名称 string fileName_s = "x_" + fileName; // 缩略图文件名称 string fileName_syp = "water_" + fileName; // 水印图文件名称(图片) string webFilePath = ""; string webFilePath_s = ""; string webFilePath_s1 = ""; string webFilePath_syp = ""; webFilePath = treePath + @"originalImages\" + fileName; // 服务器端文件路径 webFilePath_s = treePath + @"thumbnails\" + fileName_s; // 服务器端缩略图路径 webFilePath_syp = treePath + @"waterImages\" + fileName_syp; // 服务器端带水印图路径(图片) webFilePath_s1 = treePath + @"thumbnails166\" + fileName_s; // 服务器端缩略图路径 string webFilePath_sypf = System.Web.HttpContext.Current.Server.MapPath(@"~/images/synew.png"); // 服务器端水印图路径(图片) file.SaveAs(webFilePath); AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);//生成图片水印图 int towidth = 0; int toheight = 0; //MakeImage1(webFilePath, webFilePath_syp, 500); MakeImage(webFilePath, webFilePath_s, 235); //将临时数据保存到临时表中 TempImages tempImage = new TempImages(); tempImage.ISBN = fileName; if (context.Session["UserInfo"] != null) { tempImage.UserId = (context.Session["UserInfo"] as ysw.Model.UserInfo).Id; } else { tempImage.UserId = 2; } tempImage.CreateTime = DateTime.Now; tempImage.Id = TempImageManager.AddTempImages(tempImage); if (tempImage.Id > 0) { list.Add(tempImage); context.Session["TempImageList"] = list; string str = "waterImages/" + fileName_syp + ",thumbnails/" + fileName_s + "," + camera + "," + focalLength + "," + shutterSpeed + "," + aperture + "," + iso; //上传成功后让上传队列的显示自动消失 //string str = fileName + ","; //int toheight1 = 90; // int towidth1 = 90; // if ((double)towidth >(double)toheight) // { // toheight1 = toheight * 90 / towidth; // } // else // { // towidth1 = towidth * 90 / toheight; // } // str += towidth1 + "," + toheight1; context.Response.Write(str); } else { context.Response.Write("0"); } } else { context.Response.Write("0"); } } } public static void rotating(string Path_syp) { System.Drawing.Image img = System.Drawing.Image.FromFile(Path_syp); System.Drawing.Imaging.PropertyItem[] exif = img.PropertyItems; byte orientation = 0; foreach (System.Drawing.Imaging.PropertyItem i in exif) { if (i.Id == 274) { orientation = i.Value[0]; i.Value[0] = 1; img.SetPropertyItem(i); } } switch (orientation) { case 2: img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: img.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case 5: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: img.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: break; } foreach (System.Drawing.Imaging.PropertyItem i in exif) { if (i.Id == 40962) { i.Value = BitConverter.GetBytes(img.Width); } else if (i.Id == 40963) { i.Value = BitConverter.GetBytes(img.Height); } } //System.Drawing.Image bitmap = new System.Drawing.Bitmap(500, 500); //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); // //以jpg格式保存缩略图 // bitmap.Save(Path_syp, System.Drawing.Imaging.ImageFormat.Jpeg); // //image.Dispose(); // bitmap.Dispose(); // g.Dispose(); } /// <summary> /// 生成缩略图方法(700px图片) /// </summary> /// <param name="webFilePath"></param> /// <param name="webFilePath_s"></param> /// <param name="size"></param> private static void MakeImage1(string webFilePath, string webFilePath_s, int size) { System.Drawing.Image image = System.Drawing.Image.FromFile(webFilePath); int towidth = size; int toheight = size; int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; toheight = image.Height * size / image.Width; System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); g.Dispose(); bitmap.Save(webFilePath_s, System.Drawing.Imaging.ImageFormat.Jpeg); image.Dispose(); } /// <summary> /// 生成缩略图 /// </summary> /// <param name="webFilePath">原图的路径</param> /// <param name="webFilePath_s">缩略图的路径</param> /// <param name="size">缩略图的大小</param> public static void MakeImage(string webFilePath, string webFilePath_s, int size) { System.Drawing.Image image = System.Drawing.Image.FromFile(webFilePath); int towidth = size; int toheight = size; int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; if ((double)image.Width / (double)image.Height > (double)towidth / (double)toheight) { oh = image.Height; ow = image.Height * towidth / toheight; y = 0; x = 0; } else { ow = image.Width; oh = image.Width * toheight / towidth; x = 0; y = 0; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(webFilePath_s, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { image.Dispose(); bitmap.Dispose(); g.Dispose(); } } /// <summary> /// 生成水印缩略图 /// </summary> /// <param name="Path"></param> /// <param name="Path_syp"></param> /// <param name="Path_sypf"></param> protected void AddWaterPic(string Path, string Path_syp, string Path_sypf) { System.Drawing.Image image = System.Drawing.Image.FromFile(Path); System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Imaging.PropertyItem[] exif = image.PropertyItems; byte orientation = 0; foreach (System.Drawing.Imaging.PropertyItem i in exif) { if (i.Id == 274) { orientation = i.Value[0]; i.Value[0] = 1; image.SetPropertyItem(i); } } switch (orientation) { case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: image.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: break; } foreach (System.Drawing.Imaging.PropertyItem i in exif) { if (i.Id == 40962) { i.Value = BitConverter.GetBytes(image.Width); } else if (i.Id == 40963) { i.Value = BitConverter.GetBytes(image.Height); } } int towidth = 500; int toheight = 500; int x = 0; int y = 0; int ow = image.Width; int oh = image.Height; if (image.Width < 500 ) { toheight = image.Height; towidth = image.Width; } else { towidth = 500; toheight = image.Height * 500 / image.Width; } //rotating(Path); System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); g.DrawImage(copyImage, new System.Drawing.Rectangle(bitmap.Width - 10 - copyImage.Width, bitmap.Height - 8 - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); g.Dispose(); bitmap.Save(Path_syp); image.Dispose(); } public bool IsReusable { get { return false; } } }
View Code
相关文章推荐
- 使用Webuploader和图片Base64特性完成IE8及其它浏览器的头像上传
- 【项目相关】MVC中使用WebUploader进行图片预览上传以及编辑
- 使用WebUploader跨域上传图片,用SpringMVC实现服务器端
- web_uploader上传图片,官方的直接使用是不行的-java版本
- yii2中使用webuploader实现图片上传的实战项目
- 使用webuploader批量上传图片
- WebUploader文件图片上传插件的使用
- SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
- Asp.Net Mvc 使用WebUploader 多图片上传
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
- 使用WebUploader解决安卓微信浏览器上传图片中遇到的bug
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
- MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件02-多文件上传
- 使用webuploader上传图片
- 使用WebUploader上传图片
- Asp.Net Mvc 使用WebUploader 多图片上传
- Webuploader图片上传控件的使用,项目中都可以引用
- MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传
- 使用WebUploader解决安卓微信浏览器上传图片中遇到的bug