类名:Upload
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Web.Util; namespace Tools { /// <summary> /// UploadPic 的摘要说明。 /// </summary> public class UploadPic { #region 构造函数 public UploadPic() { // // TODO: 在此处添加构造函数逻辑 // } #endregion #region 上传图片,并生成缩略图 public string upload(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, int tWidth, int tHeight) { string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 Byte[] oFileByte = new byte[UploadFile.PostedFile.ContentLength]; System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 if(oWidth < tWidth && oHeight < tHeight) { result = "smaller"; return result; } //按比例计算出缩略图的宽度和高度 if(oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 string tFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file; //保存缩略图的物理路径 result = file; try { //以JPG格式保存图片 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } return result; } public string uploadPNGSIZE(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, int tWidth, int tHeight) { string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 Byte[] oFileByte = new byte[UploadFile.PostedFile.ContentLength]; System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 if(oWidth < tWidth && oHeight < tHeight) { result = "smaller"; return result; } //按比例计算出缩略图的宽度和高度 if(oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../images/") + file;//保存原图的物理路径 string tFullName = System.Web.HttpContext.Current.Server.MapPath("../../images/") + file; //保存缩略图的物理路径 result = file; try { //以JPG格式保存图片 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Png); } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } return result; } #endregion public string upload_watermark(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, int tWidth, int tHeight) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 Byte[] oFileByte = new byte[UploadFile.PostedFile.ContentLength]; System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); //加水印 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + "watermark.png"); Graphics g_water = Graphics.FromImage(oImage); g_water.DrawImage(copyImage, new Rectangle(oImage.Width - copyImage.Width, 50, copyImage.Width, copyImage.Height), 10, 20, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); g_water.Dispose(); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 if(oWidth < tWidth && oHeight < tHeight) { result = "smaller"; return result; } //按比例计算出缩略图的宽度和高度 if(oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 string tFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file; //保存缩略图的物理路径 result = file; try { //以不同格式保存图片 if(filetype.Trim().ToLower().CompareTo(".png") == 0) { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Png); } else { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } return result; } public string upload_watermark_origin(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { int tWidth = 0, tHeight = 0; string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 Byte[] oFileByte = new byte[UploadFile.PostedFile.ContentLength]; System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); //加水印 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + "watermark.png"); Graphics g_water = Graphics.FromImage(oImage); g_water.DrawImage(copyImage, new Rectangle(oImage.Width - copyImage.Width, 50, copyImage.Width, copyImage.Height), 10, 20, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); g_water.Dispose(); int oWidth = oImage.Width; //原图宽度 tWidth = oWidth; int oHeight = oImage.Height; //原图高度 tHeight = oHeight; if(oWidth < tWidth && oHeight < tHeight) { result = "smaller"; return result; } //按比例计算出缩略图的宽度和高度 if(oWidth >= oHeight) { tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); } else { tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); } //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 string tFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file; //保存缩略图的物理路径 result = file; try { //以不同格式保存图片 if(filetype.Trim().ToLower().CompareTo(".png") == 0) { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Png); } else { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } return result; } #region 上传图片 public string uploadDirect(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") >= 0) { if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 result = file; try { //以不同格式保存图片 if(filetype.Trim().ToLower().CompareTo(".png") == 0) { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); } else { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch(Exception ex) { //抛出异常 throw ex; } finally { //释放资源 oImage.Dispose(); } } else { string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存原图的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); } return result; } public string uploadArticle(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") >= 0) { if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 result = file; try { //以不同格式保存图片 if(filetype.Trim().ToLower().CompareTo(".png") == 0) { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); } else { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); } } else { string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("File/") + file;//保存原图的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); } return result; } public string uploadDirectWeb(System.Web.UI.WebControls.FileUpload UploadFile) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("pdf") < 0) { if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 result = file; try { //以不同格式保存图片 if(filetype.Trim().ToLower().CompareTo(".png") == 0) { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); } else { oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); } } else { string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".pdf"; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存原图的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); } return result; } public string uploadPNG(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; //检查上传文件的格式是否有效 if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("pdf") < 0) { if(UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { result = "上传图片格式无效!"; return result; } //生成原图 System.IO.Stream oStream = UploadFile.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../UploadImg/") + file;//保存原图的物理路径 result = file; try { //以JPG格式保存图片 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Png); } catch(Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); } } else { string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".pdf"; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存原图的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); } return result; } public string uploadFlv(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { string result = ""; //检查上传文件的格式是否有效 string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".flv"; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存文件的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); return result; } public string uploadFile(System.Web.UI.HtmlControls.HtmlInputFile UploadFile) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; string file = DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存文件的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); return result; } public string uploadProjectFile(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, string filename) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; string file = filename + "_" + DateTime.Now.ToShortDateString().Replace("-", "").Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../file/") + file;//保存文件的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); return result; } public string uploadAdvertisement(System.Web.UI.HtmlControls.HtmlInputFile UploadFile, string filename) { string filetype = System.IO.Path.GetExtension(UploadFile.PostedFile.FileName); string result = ""; if(filetype.Trim().CompareTo(".flv") != 0) { result = "wrongformat"; } else { string file = filename + filetype; string oFullName = System.Web.HttpContext.Current.Server.MapPath("../../flash/") + file;//保存文件的物理路径 result = file; UploadFile.PostedFile.SaveAs(oFullName); } return result; } #endregion } }