/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { string fileExtemsion = System.IO.Path.GetExtension(originalImagePath).ToLower(); ImageFormat imgF = ImageFormat.Png; switch (fileExtemsion) { case ".gif": imgF = ImageFormat.Gif; break; case ".jpg": imgF = ImageFormat.Jpeg; break; case ".bmp": imgF = ImageFormat.Bmp; break; case ".png": imgF = ImageFormat.Png; break; } MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode, imgF, interpolationMode, smoothingMode); }
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) { MakeThumbnailMode makeThumbnailMode = MakeThumbnailMode.None; switch (mode) { case "HW": //指定高宽缩放(可能变形) makeThumbnailMode = MakeThumbnailMode.HW; break; case "W": //指定宽,高按比例 makeThumbnailMode = MakeThumbnailMode.W; break; case "H": //指定高,宽按比例 makeThumbnailMode = MakeThumbnailMode.H; break; case "Cut": //指定高宽裁减(不变形) makeThumbnailMode = MakeThumbnailMode.Cut; break; default: break; } MakeThumbnail(originalImagePath, thumbnailPath, width, height, makeThumbnailMode); }
/// <summary> /// 生成图片水印缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">水印缩略图保存路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="waterMarkPicPath">水印图片</param> /// <param name="watermarkPosition">水印位置</param> /// <param name="transparentValue">水印透明度</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeImageWaterThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, string waterMarkPicPath, string watermarkPosition = "WM_CENTER", int transparentValue = 30, InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { MakeImageWaterThumbnail(originalImagePath, thumbnailPath, width, height, mode, ImageFormat.Png, waterMarkPicPath, watermarkPosition, transparentValue, interpolationMode, smoothingMode); }
///// <summary>创建规定大小的图像/// 源图像只能是JPG格式 ///// </summary> ///// <param name="oPath">源图像绝对路径</param> ///// <param name="tPath">生成图像绝对路径</param> ///// <param name="width">生成图像的宽度</param> ///// <param name="height">生成图像的高度</param> //public void CreateImage(string oPath, string tPath, int width, int height) //{ //Bitmap originalBmp = new Bitmap(oPath); //// 源图像在新图像中的位置 //int left, top; //if (originalBmp.Width <= width && originalBmp.Height <= height) //{ //// 原图像的宽度和高度都小于生成的图片大小 //left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2); //top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2); //// 最终生成的图像 //Bitmap bmpOut = new Bitmap(width, height); //using (Graphics graphics = Graphics.FromImage(bmpOut)) //{ //// 设置高质量插值法 //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //// 清空画布并以白色背景色填充 //graphics.Clear(Color.White); //// 把源图画到新的画布上 //graphics.DrawImage(originalBmp, left, top, originalBmp.Width, originalBmp.Height); //} //bmpOut.Save(tPath); //bmpOut.Dispose(); //originalBmp.Dispose(); //return; //} //// 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形, //// 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上 //int newWidth, newHeight; //if (width * originalBmp.Height < height * originalBmp.Width) //{ //newWidth = width; //newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width); //// 缩放成宽度跟预定义的宽度相同的,即left=0,计算top //left = 0; //top = (int)Math.Round((decimal)(height - newHeight) / 2); //} //else //{ //newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height); //newHeight = height; //// 缩放成高度跟预定义的高度相同的,即top=0,计算left //left = (int)Math.Round((decimal)(width - newWidth) / 2); //top = 0; //} //// 生成按比例缩放的图,如:160*80的图 //Bitmap bmpOut2 = new Bitmap(newWidth, newHeight); //using (Graphics graphics = Graphics.FromImage(bmpOut2)) //{ //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight); //graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight); //} //// 再把该图画到预先定义的宽高的画布上,如160*120 //Bitmap lastbmp = new Bitmap(width, height); //using (Graphics graphics = Graphics.FromImage(lastbmp)) //{ //// 设置高质量插值法 //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //// 清空画布并以白色背景色填充 //graphics.Clear(Color.White); //// 把源图画到新的画布上 //graphics.DrawImage(bmpOut2, left, top); //} //lastbmp.Save(tPath); //lastbmp.Dispose(); //originalBmp.Dispose(); //} ///// <summary> ///// Creates a thumbnail from an existing image. Sets the biggest dimension of the ///// thumbnail to either desiredWidth or Height and scales the other dimension down ///// to preserve the aspect ratio ///// </summary> ///// <param name="imageStream">stream to create thumbnail for</param> ///// <param name="desiredWidth">maximum desired width of thumbnail</param> ///// <param name="desiredHeight">maximum desired height of thumbnail</param> ///// <returns>Bitmap thumbnail</returns> //public Bitmap CreateThumbnail(Bitmap originalBmp, int desiredWidth, int desiredHeight) //{ //// If the image is smaller than a thumbnail just return it //if (originalBmp.Width <= desiredWidth && originalBmp.Height <= desiredHeight) //{ //return originalBmp; //} //int newWidth, newHeight; //// scale down the smaller dimension //if (desiredWidth * originalBmp.Height < desiredHeight * originalBmp.Width) //{ //newWidth = desiredWidth; //newHeight = (int)Math.Round((decimal)originalBmp.Height * desiredWidth / originalBmp.Width); //} //else //{ //newHeight = desiredHeight; //newWidth = (int)Math.Round((decimal)originalBmp.Width * desiredHeight / originalBmp.Height); //} //// This code creates cleaner (though bigger) thumbnails and properly //// and handles GIF files better by generating a white background for //// transparent images (as opposed to black) //// This is preferred to calling Bitmap.GetThumbnailImage() //Bitmap bmpOut = new Bitmap(newWidth, newHeight); //using (Graphics graphics = Graphics.FromImage(bmpOut)) //{ //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight); //graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight); //} //return bmpOut; //} #endregion #endregion #region 生成缩略图同时生成水印 /// <summary> /// 生成文字水印缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">水印缩略图保存路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="watermarkText">水印文字</param> /// <param name="watermarkPosition">水印位置</param> /// <param name="fontFamily">水印字体样式</param> /// <param name="fontSize">水印字体大小</param> /// <param name="fontColor">水印字体颜色</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeTextWaterThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, string watermarkText, string watermarkPosition = "WM_CENTER", string fontFamily = "arial", int fontSize = 14, string fontColor = "#FFFFFF", InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { MakeTextWaterThumbnail(originalImagePath, thumbnailPath, width, height, mode, ImageFormat.Png, watermarkText, watermarkPosition, fontFamily, fontSize, fontColor, interpolationMode, smoothingMode); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="imageFormat">图片保存格式</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>当原图尺寸小于指定缩略尺寸时, 不进行缩略, 直接输出原图, 并且不更改尺寸.</remarks> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, System.Drawing.Imaging.ImageFormat imageFormat, InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { using (Image originalImage = Image.FromFile(originalImagePath)) { Graphics graphics; Bitmap bitmap = GetThumbnail(originalImage, width, height, mode, out graphics, interpolationMode, smoothingMode); try { //以xxx格式保存缩略图 bitmap.Save(thumbnailPath, imageFormat); } finally { bitmap.Dispose(); graphics.Dispose(); } } }
/// <summary> /// 生成图片水印缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">水印缩略图保存路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="imageFormat">图片保存格式</param> /// <param name="waterMarkPicPath">水印图片</param> /// <param name="watermarkPosition">水印位置</param> /// <param name="transparentValue">水印透明度</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeImageWaterThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, ImageFormat imageFormat, string waterMarkPicPath, string watermarkPosition = "WM_CENTER", int transparentValue = 30, InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { using (Image originalImage = Image.FromFile(originalImagePath)) { Graphics picture; Bitmap bitmap = GetThumbnail(originalImage, width, height, mode, out picture, interpolationMode, smoothingMode); Image watermark = new Bitmap(waterMarkPicPath); ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float tranValue = (float)transparentValue / 100; float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, tranValue, 0.0f }, //透明度默认值为0.3 new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; int watermarkWidth = 0; int watermarkHeight = 0; double bl = 1d; //计算水印图片的比率 //取背景的1/4宽度来比较 if ((bitmap.Width > watermark.Width * 4) && (bitmap.Height > watermark.Height * 4)) { bl = 1; } else if ((bitmap.Width > watermark.Width * 4) && (bitmap.Height < watermark.Height * 4)) { bl = Convert.ToDouble(bitmap.Height / 4) / Convert.ToDouble(watermark.Height); } else if ((bitmap.Width < watermark.Width * 4) && (bitmap.Height > watermark.Height * 4)) { bl = Convert.ToDouble(bitmap.Width / 4) / Convert.ToDouble(watermark.Width); } else { if ((bitmap.Width * watermark.Height) > (bitmap.Height * watermark.Width)) { bl = Convert.ToDouble(bitmap.Height / 4) / Convert.ToDouble(watermark.Height); } else { bl = Convert.ToDouble(bitmap.Width / 4) / Convert.ToDouble(watermark.Width); } } watermarkWidth = Convert.ToInt32(watermark.Width * bl); watermarkHeight = Convert.ToInt32(watermark.Height * bl); switch (watermarkPosition) { case "WM_TOP_LEFT": xpos = 10; ypos = 10; break; case "WM_TOP_RIGHT": xpos = bitmap.Width - watermarkWidth - 10; ypos = 10; break; case "WM_BOTTOM_RIGHT": xpos = bitmap.Width - watermarkWidth - 10; ypos = bitmap.Height - watermarkHeight - 10; break; case "WM_BOTTOM_LEFT": xpos = 10; ypos = bitmap.Height - watermarkHeight - 10; break; case "WM_CENTER": xpos = bitmap.Width / 2 - (watermarkWidth / 2); ypos = bitmap.Height / 2 - (watermarkHeight / 2); break; } picture.DrawImage(watermark, new Rectangle(xpos, ypos, watermarkWidth, watermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); try { //以xxx格式保存缩略+水印图 bitmap.Save(thumbnailPath, imageFormat); } finally { watermark.Dispose(); imageAttributes.Dispose(); bitmap.Dispose(); picture.Dispose(); } } }
/// <summary> /// 生成文字水印缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">水印缩略图保存路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="imageFormat">图片保存格式</param> /// <param name="watermarkText">水印文字</param> /// <param name="watermarkPosition">水印位置</param> /// <param name="fontFamily">水印字体样式</param> /// <param name="fontSize">水印字体大小</param> /// <param name="fontColor">水印字体颜色</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeTextWaterThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, ImageFormat imageFormat, string watermarkText, string watermarkPosition = "WM_CENTER", string fontFamily = "arial", int fontSize = 14, string fontColor = "#FFFFFF", InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { //using (Image originalImage = Image.FromFile(originalImagePath)) //{ //Bitmap bitmap = GetThumbnail(originalImage, width, height, mode, out picture, // interpolationMode, smoothingMode); //Font crFont = new Font(fontFamily, fontSize); //SizeF crSize = picture.MeasureString(watermarkText, crFont); //float xpos = 0; //float ypos = 0; //switch (watermarkPosition) //{ // case "WM_TOP_LEFT": // xpos = (crSize.Width / 2); // ypos = 8; // break; // case "WM_TOP_RIGHT": // xpos = ((float)bitmap.Width * (float)1.00) - (crSize.Width / 2); // ypos = 8; // break; // case "WM_BOTTOM_RIGHT": // xpos = ((float)bitmap.Width * (float)1.00) - (crSize.Width / 2); // ypos = ((float)bitmap.Height * (float)1.00) - (crSize.Height); // break; // case "WM_BOTTOM_LEFT": // xpos = (crSize.Width / 2); // ypos = ((float)bitmap.Height * (float)1.00) - crSize.Height; // break; // case "WM_CENTER": // xpos = ((float)bitmap.Width * (float).50); // ypos = ((float)bitmap.Height * (float).50) - (crSize.Height / 2); // break; //} //StringFormat strFormat = new StringFormat(); //strFormat.Alignment = StringAlignment.Center; //SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); //picture.DrawString(watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, strFormat); //SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, ColorTranslator.FromHtml(fontColor))); //picture.DrawString(watermarkText, crFont, semiTransBrush, xpos, ypos, strFormat); //try //{ // //以xxx格式保存缩略+水印图 // bitmap.Save(thumbnailPath, imageFormat); //} //finally //{ // semiTransBrush2.Dispose(); // semiTransBrush.Dispose(); // bitmap.Dispose(); // picture.Dispose(); //} string tempPath = "/Upload/Temp/" + new Guid() + ".jpg"; addWatermarkText(originalImagePath, HttpContext.Current.Server.MapPath(tempPath), watermarkText, watermarkPosition, fontFamily, fontSize, fontColor, interpolationMode, smoothingMode); MakeThumbnail(HttpContext.Current.Server.MapPath(tempPath), thumbnailPath, width, height, mode); //} }
/// <summary> /// 获取缩略图对象 /// </summary> /// <param name="originalImage">源图对象</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="graphics">输出绘图对象, 可继续追加水印</param> /// <param name="interpolationMode">指定在缩放或旋转图像时使用的算法</param> /// <param name="smoothingMode">指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘</param> /// <remarks>当原图尺寸小于指定缩略尺寸时, 不进行缩略, 直接输出原图, 并且不更改尺寸.</remarks> public static Bitmap GetThumbnail(Image originalImage, int width, int height, MakeThumbnailMode mode, out Graphics graphics, //DONE: 由外部设置缩略图的清晰度, 拟减少文件大小 BEN ADD 20130123 22:41 //TODO: 参数太多, 用对象封装, 拟减少调用的痛苦程度 BEN ADD 20130124 16:40 InterpolationMode interpolationMode = InterpolationMode.High, SmoothingMode smoothingMode = SmoothingMode.HighQuality) { int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; //TODO: Auto目前按照容器宽度进行等比例判断, 这是错误的, 应该根据原图的宽高比进行判断 if (mode == MakeThumbnailMode.Auto) { if (towidth > toheight) { mode = MakeThumbnailMode.W; } else { mode = MakeThumbnailMode.H; } } //当原图尺寸小于指定缩略尺寸时, 不进行缩略, 直接输出原图, 并且不更改尺寸. if (ow < towidth && oh < toheight) { towidth = ow; toheight = oh; } else { switch (mode) { case MakeThumbnailMode.HW: //指定高宽缩放(可能变形) break; case MakeThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case MakeThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case MakeThumbnailMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } } //新建一个bmp图片 Bitmap bitmap = new System.Drawing.Bitmap(towidth, toheight); bitmap.MakeTransparent(Color.Transparent); //新建一个画板 graphics = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 graphics.InterpolationMode = interpolationMode; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = smoothingMode; //清空画布并以透明背景色填充 graphics.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); return(bitmap); }
public static bool SmallPic(string originalImagePath, string thumbnailPath, int width, int height, int targetRate, MakeThumbnailMode mode, ref byte[] retBytes, ImageFormat format) { Bitmap bitmap; Bitmap bitmap2; System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath); int num = width; int num2 = height; int num3 = 0; int num4 = 0; int num5 = image.Width; int num6 = image.Height; if (((num > num5) || (num2 > num6)) && ((mode == MakeThumbnailMode.Width) || (mode == MakeThumbnailMode.Height))) { num = num5; num2 = num6; mode = MakeThumbnailMode.Solid; } switch (mode) { case MakeThumbnailMode.Liquid: if ((((double)image.Width) / ((double)image.Height)) > (((double)num) / ((double)num2))) { num2 = (num * image.Height) / image.Width; } else { num = (num2 * image.Width) / image.Height; } break; case MakeThumbnailMode.Width: num2 = (image.Height * width) / image.Width; break; case MakeThumbnailMode.Height: num = (image.Width * height) / image.Height; break; case MakeThumbnailMode.Cut: if ((((double)image.Width) / ((double)image.Height)) >= (((double)num) / ((double)num2))) { num5 = (num6 * num) / num2; num4 = 0; num3 = (image.Width - num5) / 2; break; } num6 = (num5 * num2) / num; num3 = 0; num4 = (image.Height - num6) / 2; break; case MakeThumbnailMode.Part: if ((((double)image.Width) / ((double)image.Height)) <= (((double)num) / ((double)num2))) { num6 = num2; num5 = (num6 * image.Width) / image.Height; num4 = 0; num3 = (num - num5) / 2; break; } num5 = num; num6 = (num5 * image.Height) / image.Width; num3 = 0; num4 = (num2 - num6) / 2; break; case MakeThumbnailMode.Contain: if ((((double)image.Width) / ((double)image.Height)) <= (((double)num) / ((double)num2))) { num5 = (image.Height * num) / num2; num4 = 0; num3 = (image.Width - num5) / 2; break; } num6 = (image.Width * num2) / num; num3 = 0; num4 = (image.Height - num6) / 2; break; case MakeThumbnailMode.Rate: height = (image.Height * targetRate) / 100; width = (image.Width * targetRate) / 100; num = (image.Width * height) / image.Height; num2 = (image.Height * width) / image.Width; break; } try { bitmap = new Bitmap(originalImagePath); bitmap2 = new Bitmap(bitmap, num, num2); if (!string.IsNullOrEmpty(thumbnailPath)) { bitmap2.Save(thumbnailPath); } MemoryStream stream = new MemoryStream(); bitmap2.Save(stream, format); retBytes = stream.GetBuffer(); stream.Close(); bitmap.Dispose(); bitmap2.Dispose(); } catch (Exception exception) { if (image != null) { image.Dispose(); } bitmap = null; bitmap2 = null; throw exception; } finally { image.Dispose(); bitmap = null; bitmap2 = null; } return(true); }
public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, int targetRate, MakeThumbnailMode mode, ref byte[] retBytes) { System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath); int num = width; int num2 = height; int x = 0; int y = 0; int num5 = image.Width; int num6 = image.Height; if (((num > num5) || (num2 > num6)) && ((mode == MakeThumbnailMode.Width) || (mode == MakeThumbnailMode.Height))) { num = num5; num2 = num6; mode = MakeThumbnailMode.Solid; } switch (mode) { case MakeThumbnailMode.Liquid: if ((((double)image.Width) / ((double)image.Height)) > (((double)num) / ((double)num2))) { num2 = (num * image.Height) / image.Width; } else { num = (num2 * image.Width) / image.Height; } break; case MakeThumbnailMode.Width: num2 = (image.Height * width) / image.Width; break; case MakeThumbnailMode.Height: num = (image.Width * height) / image.Height; break; case MakeThumbnailMode.Cut: if ((((double)image.Width) / ((double)image.Height)) >= (((double)num) / ((double)num2))) { num5 = (num6 * num) / num2; y = 0; x = (image.Width - num5) / 2; break; } num6 = (num5 * num2) / num; x = 0; y = (image.Height - num6) / 2; break; case MakeThumbnailMode.Part: if ((((double)image.Width) / ((double)image.Height)) <= (((double)num) / ((double)num2))) { num6 = num2; num5 = (num6 * image.Width) / image.Height; y = 0; x = (num - num5) / 2; break; } num5 = num; num6 = (num5 * image.Height) / image.Width; x = 0; y = (num2 - num6) / 2; break; case MakeThumbnailMode.Contain: if ((((double)image.Width) / ((double)image.Height)) <= (((double)num) / ((double)num2))) { num6 = image.Height; num5 = (num6 * num) / num2; y = 0; x = (image.Width - num5) / 2; break; } num5 = image.Width; num6 = (num5 * num2) / num; x = 0; y = (image.Height - num6) / 2; break; case MakeThumbnailMode.Rate: height = (image.Height * targetRate) / 100; width = (image.Width * targetRate) / 100; num = (image.Width * height) / image.Height; num2 = (image.Height * width) / image.Width; break; } System.Drawing.Image image2 = new Bitmap(num, num2); Graphics graphics = Graphics.FromImage(image2); graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.Clear(Color.Transparent); graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel); try { if (!string.IsNullOrEmpty(thumbnailPath)) { image2.Save(thumbnailPath, ImageFormat.Jpeg); } MemoryStream stream = new MemoryStream(); image2.Save(stream, ImageFormat.Jpeg); retBytes = stream.GetBuffer(); stream.Close(); } catch (Exception exception) { throw exception; } finally { image.Dispose(); image2.Dispose(); graphics.Dispose(); } return(true); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode) { Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case MakeThumbnailMode.指定高宽缩放可能变形: //指定高宽缩放(可能变形) break; case MakeThumbnailMode.指定宽然后高按比例: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case MakeThumbnailMode.指定高然后宽按比例: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case MakeThumbnailMode.指定高宽裁减不变形: //指定高宽裁减(不变形) if (originalImage.Width / (double)originalImage.Height > towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; } //新建一个bmp图片 Image bitmap = new Bitmap(towidth, toheight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以指定格式保存缩略图 bitmap.Save(thumbnailPath, originalImage.RawFormat); } catch (Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="imageFormat">缩略图保存格式</param> /// <remarks>当原图尺寸小于指定缩略尺寸时, 不进行缩略, 直接输出原图, 并且不更改尺寸.</remarks> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode, System.Drawing.Imaging.ImageFormat imageFormat) { Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (mode == MakeThumbnailMode.Auto) { if (ow > oh) { mode = MakeThumbnailMode.W; } else { mode = MakeThumbnailMode.H; } } //当原图尺寸小于指定缩略尺寸时, 不进行缩略, 直接输出原图, 并且不更改尺寸. if (ow < towidth && oh < toheight) { try { //以xxx格式保存缩略图 originalImage.Save(thumbnailPath, imageFormat); } catch (Exception e) { throw e; } finally { originalImage.Dispose(); } return; } switch (mode) { case MakeThumbnailMode.HW: //指定高宽缩放(可能变形) break; case MakeThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case MakeThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case MakeThumbnailMode.Cut: //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 using (Bitmap bitmap = new System.Drawing.Bitmap(towidth, toheight)) { bitmap.MakeTransparent(Color.Transparent); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以xxx格式保存缩略图 bitmap.Save(thumbnailPath, imageFormat); } catch (Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <remarks>缩略图默认png格式</remarks> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, MakeThumbnailMode mode) { MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode, System.Drawing.Imaging.ImageFormat.Png); }
public UploadImageHandlerBase(MakeThumbnailMode mode = MakeThumbnailMode.None, bool isLocalSave = true, ApplicationKeyType applicationKeyType = ApplicationKeyType.None) : base(isLocalSave, applicationKeyType) { ThumbnailMode = mode == MakeThumbnailMode.None ? MakeThumbnailMode.W : mode; }