/// <summary> /// 上传图片. /// </summary> /// <param name="files">控件</param> /// <param name="pathurl">保存图片全路径</param> /// <returns></returns> public static void ImageUpLoad2(HttpPostedFile files, ref string pathurl) { if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); ext = ext.ToLower(); if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "png") { long FLength = files.ContentLength / 1024; if (FLength > MS_ConfigBLL.UploadSize) { EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!"); } else { files.SaveAs(pathurl); } } else { EKMessageBox.Show("文件上传失败,文件格式不允许上传!"); return; } } }
/// <summary> /// 上传文件 /// </summary> /// <param name="files">FileUpload控件</param> /// <returns></returns> public static void FileUpLoad(FileUpload files, ref string filepath) { //filepath = ""; if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); if (!EKFileUpload.CheckExt(ext)) { EKMessageBox.Show("文件上传失败,文件格式不允许上传!"); } else { if (filepath == "") { string FileName = "/" + MS_ConfigBLL.UploadPath + "/" + EKFileUpload.getMyPath() + EKFileUpload.createFileName() + "." + ext; string SaveFile = EKFileUpload.FilePath + FileName; EKFileUpload.doCreatrDir(SaveFile); files.SaveAs(SaveFile); filepath = FileName; } else { string SaveFile = filepath + "\\" + files.FileName; files.SaveAs(SaveFile); } files.Dispose(); } } }
/// <summary> /// 上传文件,带用户路径 /// </summary> /// <param name="files">FileUpload控件</param> /// <returns></returns> public static void FileUpLoadByAdminName(FileUpload files, ref string filepath) { filepath = ""; if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); if (!EKFileUpload.CheckExt(ext)) { EKMessageBox.Show("文件上传失败,因为文件格式不允许上传!"); } else { long FLength = files.PostedFile.ContentLength / 1024; if (FLength > MS_ConfigBLL.UploadSize) { EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!"); } else { string FileName = "/" + MS_ConfigBLL.UploadPath + "/Admin/" + MS_AdminBLL.AdminID + "/" + EKFileUpload.getMyPath() + EKFileUpload.createFileName() + "." + ext; string SaveFile = EKFileUpload.FilePath + FileName; EKFileUpload.doCreatrDir(SaveFile); files.SaveAs(SaveFile); filepath = FileName; } } } }
/// <summary> /// 上传文件.按日期生成 /// </summary> /// <param name="files">FileUpload控件</param> /// <param name="filepath">文件全路径</param> /// <returns></returns> public static void FileUpLoad2(HttpPostedFile files, ref string filepath) { //filepath = ""; if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); if (!EKFileUpload.CheckExt(ext)) { EKMessageBox.Show("文件上传失败,文件格式不允许上传!"); } else { files.SaveAs(filepath); } } }
/// <summary> /// 上传图片 /// </summary> /// <param name="files">FileUpload控件</param> /// <returns></returns> public static void ImageUpLoad(FileUpload files, ref string path) { if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); ext = ext.ToLower(); if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "png") { long FLength = files.PostedFile.ContentLength / 1024; if (FLength > MS_ConfigBLL.UploadSize) { EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!"); } else { if (path == "") { string createfilename = EKFileUpload.createFileName(); string FileName = "/" + MS_ConfigBLL.UploadPath + "/" + path + "/" + EKFileUpload.getMyPath() + createfilename + "." + ext; FileName.Replace("//", "/"); string SaveFile = EKFileUpload.FilePath + FileName; EKFileUpload.doCreatrDir(SaveFile); files.SaveAs(SaveFile); path = FileName; } else { string SaveFile = path + "\\" + files.FileName; files.SaveAs(SaveFile); } } } else { EKMessageBox.Show("文件上传失败,文件格式不允许上传!"); return; } } }
/// <summary> /// 上传略缩图并添加水印 /// </summary> /// <param name="files">FileUpload控件</param> /// <returns></returns> public static void ImageUpLoad(FileUpload files, Int32 Width, Int32 Height, Watermark watermark, ref string picpath) { //System.Drawing.Image xImage; System.Drawing.Bitmap xBitmap; int PhotoHeight, PhotoWidth; //Rectangle NewPhoto; System.Drawing.Imaging.ImageFormat xObject; picpath = ""; if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); ext = ext.ToLower(); if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "png") { long FLength = files.PostedFile.ContentLength / 1024; if (FLength > MS_ConfigBLL.UploadSize) { EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!"); } else { string createfilename = EKFileUpload.createFileName(); string FileName = "/" + MS_ConfigBLL.UploadPath + "/" + EKFileUpload.getMyPath() + createfilename + "." + ext; string SaveFile = EKFileUpload.FilePath + FileName; //EKFileUpload.doCreatrDir(SaveFile); //files.SaveAs(SaveFile); picpath = FileName.Replace("//", "/"); switch (ext) { case ".gif": xObject = System.Drawing.Imaging.ImageFormat.Gif; break; case ".bmp": xObject = System.Drawing.Imaging.ImageFormat.Bmp; break; case ".png": xObject = System.Drawing.Imaging.ImageFormat.Png; break; default: xObject = System.Drawing.Imaging.ImageFormat.Jpeg; break; } xBitmap = new Bitmap(files.FileContent);//------------------ PhotoHeight = xBitmap.Height; PhotoWidth = xBitmap.Width; Int32 TempHeight = 0; Int32 TempWidth = 0; if (PhotoWidth <= Width && PhotoHeight <= Height) { TempWidth = PhotoWidth; TempHeight = PhotoHeight; } else { if (PhotoWidth > Width)//图片宽度大于设定宽度 { TempWidth = Width; TempHeight = Convert.ToInt32(Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) * Convert.ToDecimal(Width)); if (TempHeight > Height) { TempWidth = Convert.ToInt32(Convert.ToDecimal(TempWidth) / Convert.ToDecimal(TempHeight) * Convert.ToDecimal(Height)); TempHeight = Height; } } else { if (PhotoHeight > Height) { TempHeight = Height; TempWidth = Convert.ToInt32(Convert.ToDecimal(PhotoWidth) / Convert.ToDecimal(PhotoHeight) * Convert.ToDecimal(Height)); } } //System.Web.HttpContext.Current.Response.Write(TempHeight.ToString()+","); //System.Web.HttpContext.Current.Response.Write(TempWidth.ToString()); //System.Web.HttpContext.Current.Response.End(); if (TempHeight <= 0) { TempHeight = 1; } if (TempWidth <= 0) { TempWidth = 1; } } System.Drawing.Image bitmap = new System.Drawing.Bitmap(TempWidth, TempHeight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //清空一下画布 g.Clear(Color.Empty); //在指定位置画图 g.DrawImage(xBitmap, new System.Drawing.Rectangle(0, 0, TempWidth, TempHeight), new System.Drawing.Rectangle(1, 1, xBitmap.Width - 1, xBitmap.Height - 1), System.Drawing.GraphicsUnit.Pixel); //添加水印 if (watermark != null) { if (watermark.MarkType == "text") { //声明字体对象 Font cFont = null; //用来测试水印文本长度得尺子 SizeF size = new SizeF(); int[] sizes = new int[] { watermark.Size, 48, 32, 16, 8, 6, 4 }; //探测出一个适合图片大小得字体大小,以适应水印文字大小得自适应 for (int i = 0; i < 7; i++) { if (sizes[i] == 0) { continue; } //创建一个字体对象 cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Regular); //是否加粗、倾斜 if (watermark.TextBlod && watermark.TextItalic) { cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold | FontStyle.Italic); } else if (watermark.TextBlod) { cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Bold); } else if (watermark.TextItalic) { cFont = new Font(watermark.FontFamily, sizes[i], FontStyle.Italic); } //测量文本大小 size = g.MeasureString(watermark.Text, cFont); //匹配第一个符合要求得字体大小 if ((ushort)size.Width < (ushort)TempWidth) { break; } } Brush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(watermark.Transparency * 255), watermark.TextColor)); //添加水印 文字 g.DrawString(watermark.Text, cFont, brush, watermark.Position(watermark.Place, new SizeF(TempWidth, TempHeight), size)); } else if (watermark.MarkType == "image") { //获得水印图像 System.Drawing.Image markImg = System.Drawing.Image.FromFile(EKFileUpload.FilePath + watermark.ImgPath); //创建颜色矩阵 float[][] ptsArray = { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, watermark.Transparency, 0 }, //注意:此处为0.0f为完全透明,1.0f为完全不透明 new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix colorMatrix = new ColorMatrix(ptsArray); //新建一个Image属性 ImageAttributes imageAttributes = new ImageAttributes(); //将颜色矩阵添加到属性 imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default); //原图过小 if (markImg.Width > TempWidth || markImg.Height > TempHeight) { System.Drawing.Image.GetThumbnailImageAbort callb = null; //对水印图片生成缩略图,缩小到原图得1/4 System.Drawing.Image new_img = markImg.GetThumbnailImage(TempWidth / 4, markImg.Height * TempWidth / 4 / markImg.Width, callb, new System.IntPtr()); //添加水印图片 g.DrawImage(new_img, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), new_img.Size), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imageAttributes); //释放缩略图 new_img.Dispose(); } else { //添加水印图片 g.DrawImage(markImg, watermark.Position(watermark.Place, new Size(TempWidth, TempHeight), markImg.Size), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes); } } } // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 95; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("jpeg")) { jpegICI = arrayICI[x]; //设置JPEG编码 break; } } if (jpegICI != null) { //System.Web.HttpContext.Current.Response.Write("a"); EKFileUpload.doCreatrDir(SaveFile); bitmap.Save(SaveFile, jpegICI, encoderParams); } else { //System.Web.HttpContext.Current.Response.Write("b"); EKFileUpload.doCreatrDir(SaveFile); bitmap.Save(SaveFile, xObject); } g.Dispose(); bitmap.Dispose(); xBitmap.Dispose(); } } //picpath = FileName; } else { EKMessageBox.Show("文件上传失败,因为文件格式不允许上传!"); return; } }
/// <summary> /// 上传图片并生成略缩图 /// </summary> /// <param name="files">FileUpload控件</param> /// <returns></returns> public static void ImageUpLoad(FileUpload files, Int32 ThumbnailWidth, Int32 ThumbnailHeight, ref string picpath, ref string smallpicpath) { //System.Drawing.Image xImage; System.Drawing.Bitmap xBitmap; int PhotoHeight, PhotoWidth; //Rectangle NewPhoto; System.Drawing.Imaging.ImageFormat xObject; string smallFileName = ""; string smallSaveFile = ""; picpath = ""; smallpicpath = ""; if (files.FileName != "") { string ext = EKFileUpload.getExtend(files.FileName); ext = ext.ToLower(); if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "png") { long FLength = files.PostedFile.ContentLength / 1024; if (FLength > MS_ConfigBLL.UploadSize) { EKMessageBox.Show("文件上传失败!因为上传的文件超过了" + MS_ConfigBLL.UploadSize + "KB!"); } else { string createfilename = EKFileUpload.createFileName(); string FileName = "/" + MS_ConfigBLL.UploadPath + "/" + EKFileUpload.getMyPath() + createfilename + "." + ext; string SaveFile = EKFileUpload.FilePath + FileName; EKFileUpload.doCreatrDir(SaveFile); files.SaveAs(SaveFile); picpath = FileName; smallFileName = "/" + MS_ConfigBLL.UploadPath + "/" + EKFileUpload.getMyPath() + "small" + createfilename + "." + ext; smallSaveFile = EKFileUpload.FilePath + smallFileName; switch (ext) { case ".gif": xObject = System.Drawing.Imaging.ImageFormat.Gif; break; case ".bmp": xObject = System.Drawing.Imaging.ImageFormat.Bmp; break; case ".png": xObject = System.Drawing.Imaging.ImageFormat.Png; break; default: xObject = System.Drawing.Imaging.ImageFormat.Jpeg; break; } xBitmap = new Bitmap(SaveFile);//------------------ PhotoHeight = xBitmap.Height; PhotoWidth = xBitmap.Width; Int32 TempHeight = 0; Int32 TempWidth = 0; if (PhotoWidth <= ThumbnailWidth && PhotoHeight <= ThumbnailHeight) { EKFileUpload.doCreatrDir(smallSaveFile); xBitmap.Save(smallSaveFile); } else { if (PhotoWidth > ThumbnailWidth)//图片宽度大于设定宽度 { TempWidth = ThumbnailWidth; TempHeight = Convert.ToInt32(Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) * Convert.ToDecimal(ThumbnailWidth)); if (TempHeight > ThumbnailHeight) { TempWidth = Convert.ToInt32(Convert.ToDecimal(TempWidth) / Convert.ToDecimal(TempHeight) * Convert.ToDecimal(ThumbnailHeight)); TempHeight = ThumbnailHeight; } } else { if (PhotoHeight > ThumbnailHeight) { TempHeight = ThumbnailHeight; TempWidth = Convert.ToInt32(Convert.ToDecimal(PhotoWidth) / Convert.ToDecimal(PhotoHeight) * Convert.ToDecimal(ThumbnailHeight)); } } //System.Web.HttpContext.Current.Response.Write(TempHeight.ToString()+","); //System.Web.HttpContext.Current.Response.Write(TempWidth.ToString()); //System.Web.HttpContext.Current.Response.End(); if (TempHeight <= 0) { TempHeight = 1; } if (TempWidth <= 0) { TempWidth = 1; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(TempWidth, TempHeight); //新建一个画板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //清空一下画布 g.Clear(Color.Empty); //在指定位置画图 g.DrawImage(xBitmap, new System.Drawing.Rectangle(0, 0, TempWidth, TempHeight), new System.Drawing.Rectangle(1, 1, xBitmap.Width - 1, xBitmap.Height - 1), System.Drawing.GraphicsUnit.Pixel); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 95; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("jpeg")) { jpegICI = arrayICI[x]; //设置JPEG编码 break; } } if (jpegICI != null) { //System.Web.HttpContext.Current.Response.Write("a"); EKFileUpload.doCreatrDir(smallSaveFile); bitmap.Save(smallSaveFile, jpegICI, encoderParams); } else { //System.Web.HttpContext.Current.Response.Write("b"); EKFileUpload.doCreatrDir(smallSaveFile); bitmap.Save(smallSaveFile, xObject); } bitmap.Dispose(); } xBitmap.Dispose(); } smallpicpath = smallFileName; } else { EKMessageBox.Show("文件上传失败,因为文件格式不允许上传!"); return; } } }