/// <summary> /// 生成普通缩略图 /// </summary> /// <param name="sourceFile"> 源图的路径(含文件名及扩展名) </param> /// <param name="destFile"> 生成的缩略图所保存的路径(含文件名及扩展名)</param> /// <param name="width"> 欲生成的缩略图的宽度(像素值) </param> /// <param name="height"> 欲生成的缩略图的高度(像素值) </param> private static bool GenThumbnail(string sourceFile, string destFile, int width, int height) { System.Drawing.Image imageFrom = null; Bitmap bmp = null; Graphics g = null; try { imageFrom = System.Drawing.Image.FromFile(sourceFile); // 源图宽度及高度 int imageFromWidth = imageFrom.Width; int imageFromHeight = imageFrom.Height; // 根据源图及欲生成的缩略图尺寸,计算缩略图的实际尺寸及其在"画布"上的位置 if (imageFromWidth > imageFromHeight) { height = imageFromHeight * width / imageFromWidth; } else { width = imageFromWidth * height / imageFromHeight; } // 创建画布 bmp = new Bitmap(width, height); g = Graphics.FromImage(bmp); // 用白色清空 g.Clear(Color.White); // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 g.SmoothingMode = SmoothingMode.HighQuality; // 在指定位置并且按指定大小绘制指定的 Image 的指定部分。 g.DrawImage(imageFrom, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel); string destPath = Path.GetDirectoryName(destFile); if (!string.IsNullOrWhiteSpace(destPath)) { if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } } //经测试 .jpg 格式缩略图大小与质量等最优 bmp.Save(destFile, ImageFormat.Jpeg); return(true); } catch (Exception ex) { throw new Exception("从" + sourceFile + "生成缩略图到" + destFile + "出现异常:" + ex); } finally { //显示释放资源 imageFrom.DisposeIfNotNull(); g.DisposeIfNotNull(); bmp.DisposeIfNotNull(); } }
/// <summary> /// 给图片添加文字水印 /// </summary> /// <param name="srcFile">源图片存放路径</param> /// <param name="destFile">目标图片存放路径</param> /// <param name="fontSize">字号,可超过72号的</param> /// <param name="throwException">出现异常时是否抛出</param> /// <param name="text">要添加的文字</param> public static bool AddWatermarkText(string srcFile, string destFile, int fontSize, string text) { if (string.IsNullOrWhiteSpace(srcFile) || string.IsNullOrWhiteSpace(destFile)) { throw new Exception("源图或目标图片的存放路径都不能为空"); } System.Drawing.Image img = null; Graphics picture = null; Font crFont = null; StringFormat StrFormat = new StringFormat(); SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); try { img = System.Drawing.Image.FromFile(srcFile); picture = Graphics.FromImage(img); SizeF crSize = new SizeF(); crFont = new Font("arial", fontSize, FontStyle.Bold); crSize = picture.MeasureString(text, crFont); float xpos = 0; float ypos = 0; xpos = (img.Width - crSize.Width) / 2; ypos = (img.Height - crSize.Height) / 2; StrFormat.Alignment = StringAlignment.Near; picture.DrawString(text, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat); picture.DrawString(text, crFont, semiTransBrush, xpos, ypos, StrFormat); string destPath = Path.GetDirectoryName(destFile); if (!string.IsNullOrWhiteSpace(destPath)) { if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } } img.Save(destFile); return(true); } catch (Exception ex) { throw new Exception("给图片添加水印时发生异常:" + ex); } finally { crFont.DisposeIfNotNull(); StrFormat.DisposeIfNotNull(); semiTransBrush2.DisposeIfNotNull(); semiTransBrush.DisposeIfNotNull(); picture.DisposeIfNotNull(); img.DisposeIfNotNull(); } }
/// <summary> /// 生成PNG缩略图 /// </summary> /// <param name="sourceFile"></param> /// <param name="destFile"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> private static bool PngThumbnail(string sourceFile, string destFile, int width, int height) { System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);//提供一个回调方法,用于决定获取缩略图的方法在何时提前取消执行。 Bitmap bmp = null; System.Drawing.Image thumbnail = null; try { bmp = new Bitmap(sourceFile); if (bmp.Width > bmp.Height)//计算等比例缩放的宽高,原图中2者中较大的值来计算 { height = bmp.Height * width / bmp.Width; } else { width = bmp.Width * height / bmp.Height; } thumbnail = bmp.GetThumbnailImage(width, height, myCallBack, IntPtr.Zero);//检索图片中是否包含缩略图,如果不包含,就按比例缩放来生成一个缩略图。 string destPath = Path.GetDirectoryName(destFile); if (!string.IsNullOrWhiteSpace(destPath)) { if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } } thumbnail.Save(destFile);//将图像保存到指定path中。 return(true); } catch (Exception ex) { throw new Exception("生成PNG缩略图时发生异常:" + ex); } finally { bmp.DisposeIfNotNull(); thumbnail.DisposeIfNotNull(); } }