/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler graphicsHandler) { return(DrawBySize(original, mode, width, height, ImageFormat.Jpeg, 100L, 60L, graphicsHandler)); }
/// <summary> /// 生成水印并保存到文件 /// </summary> /// <param name="imgPath">图片路径</param> /// <param name="waterPath">水印路径</param> /// <param name="pos">水印位置</param> /// <param name="handler">图片处理</param> /// <returns></returns> public static byte[] MakeWatermarkImage(string imgPath, string waterPath, WatermarkPosition pos, ImageGraphicsHandler handler) { Image img = Image.FromFile(imgPath), water = Image.FromFile(waterPath); byte[] bytes = MakeWatermarkImage(img, water, pos, handler); img.Dispose(); return(bytes); }
/// <summary> /// 生成水印 /// </summary> /// <param name="img">图片</param> /// <param name="water">水印</param> /// <param name="waterPos">水印位置</param> /// <param name="handler">处理图片</param> /// <returns>图片内存流</returns> public static byte[] MakeWatermarkImage( Image img, Image water, Point waterPos, ImageGraphicsHandler handler) { //尺寸检测 if (water.Width > img.Width || water.Height > img.Height) { throw new ArgumentException("水印图片尺寸超过原图,无法生成水印!"); } //绘制水印 Graphics g = Graphics.FromImage(img); g.DrawImage(water, waterPos.X, waterPos.Y); if (handler != null) { handler(img); if (img == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存流 MemoryStream ms = new MemoryStream(); img.Save(ms, img.RawFormat); //释放资源 g.Dispose(); byte[] bytes = ms.ToArray(); ms.Dispose(); water.Dispose(); return(bytes); }
/// <summary> /// 生成水印 /// </summary> /// <param name="img">图片</param> /// <param name="water">水印</param> /// <param name="pos">预置的水印位置</param> /// <param name="format">图像格式</param> /// <param name="handler">处理图片</param> /// <returns>图片内存流</returns> public static byte[] MakeWatermarkImage( Image img, Image water, WatermarkPosition pos, ImageGraphicsHandler handler) { int x; int y; int xOffset = 5; //宽度偏移量 int yOffset = 5; //高度偏移量 switch (pos) { case WatermarkPosition.Bottom: //正下方 x = (img.Width - water.Width) / 2; y = img.Height - water.Height - yOffset; break; case WatermarkPosition.LeftBottom: //左下方 x = xOffset; y = img.Height - water.Height - yOffset; break; case WatermarkPosition.Right: //右方 x = img.Width - water.Width - xOffset; y = (img.Height - water.Height) / 2; break; case WatermarkPosition.Middle: //正中 x = (img.Width - water.Width) / 2; y = (img.Height - water.Height) / 2; break; case WatermarkPosition.Left: //左边 x = xOffset; y = (img.Height - water.Height) / 2; break; case WatermarkPosition.RightTop: //右上方 x = img.Width - water.Width - xOffset; y = yOffset; break; case WatermarkPosition.Top: //正上方 x = (img.Width - water.Width) / 2; y = yOffset; break; case WatermarkPosition.LeftTop: //左上方 x = xOffset; y = yOffset; break; default: case WatermarkPosition.Default: case WatermarkPosition.RightBottom: //右下角 x = img.Width - water.Width - xOffset; y = img.Height - water.Height - yOffset; break; } return(MakeWatermarkImage(img, water, new Point(x, y), handler)); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="thumbHandler">对缩略图处理</param> public static MemoryStream MakeThumbnail(Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler thumbHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; int oh = original.Height; switch (mode) { case ImageSizeMode.SuitWidth: toHeight = original.Height * width / original.Width; break; case ImageSizeMode.SuitHeight: toWidth = original.Width * height / original.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (original.Width > original.Height) { toHeight = original.Height * width / original.Width; } else //根据高度适配 { toWidth = original.Width * height / original.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double)original.Width / (double)original.Height > (double)toWidth / (double)toHeight) { toWidth = width; toHeight = original.Height * width / original.Width; } else { toHeight = height; toWidth = original.Width * height / original.Height; } break; //裁剪 case ImageSizeMode.Cut: if ((double)original.Width / (double)original.Height > (double)toWidth / (double)toHeight) { oh = original.Height; ow = original.Height * toWidth / toHeight; y = 0; x = (original.Width - ow) / 2; } else { ow = original.Width; oh = original.Width * height / toWidth; x = 0; y = (original.Height - oh) / 2; } break; default: case ImageSizeMode.CustomSize: break; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle((width - toWidth) / 2, (height - toHeight) / 2, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (thumbHandler != null) { thumbHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); //图片质量参数 EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, 100L); ep.Param[1] = new EncoderParameter(Encoder.Compression, 60L); ImageCodecInfo imgCode = null; ImageCodecInfo[] imgCodes = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo code in imgCodes) { if (code.MimeType == "image/jpeg") { imgCode = code; break; } } if (imgCode != null) { bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return(ms); }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="format">图像格式</param> /// <param name="compression"></param> /// <param name="graphicsHandler">对缩略图处理</param> /// <param name="imageQuality"></param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageFormat format, long imageQuality, long compression, ImageGraphicsHandler graphicsHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; //原始宽度 int oh = original.Height; //原始高度 Size toSize = new Size(width, height); //要转换的图片尺寸 Size imageSize = GetSize(original.Size, toSize, mode); //转换的实际尺寸 //裁剪 if (mode == ImageSizeMode.Cut) { if ((double)original.Width / (double)original.Height > (double)toSize.Width / (double)toSize.Height) { oh = original.Height; ow = original.Height * toWidth / toHeight; y = 0; x = (original.Width - ow) / 2; } else { ow = original.Width; oh = original.Width * height / toWidth; x = 0; y = (original.Height - oh) / 2; } } else { x = (toSize.Width - imageSize.Width) / 2; y = (toSize.Height - imageSize.Height) / 2; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle(x, y, imageSize.Width, imageSize.Height), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (graphicsHandler != null) { graphicsHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); ImageCodecInfo imgCode = null; if (format != null) { string formatStr = format.ToString(); imgCode = imageCoders.ContainsKey(formatStr) ? imageCoders[formatStr] : null; } if (imgCode != null) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, imageQuality > 100 || imageQuality <= 0 ? 100L : imageQuality); ep.Param[1] = new EncoderParameter(Encoder.Compression, compression < 1L ? 100L : compression); bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); byte[] bytes = ms.ToArray(); ms.Dispose(); bitmap.Dispose(); return(bytes); }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="format">图像格式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageFormat format, long imageQuality, long compression, ImageGraphicsHandler graphicsHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; //原始宽度 int oh = original.Height; //原始高度 Size toSize = new Size(width, height); //要转换的图片尺寸 Size imageSize = GetSize(original.Size, toSize, mode); //转换的实际尺寸 //裁剪 if (mode == ImageSizeMode.Cut) { if ((double) original.Width/(double) original.Height > (double) toSize.Width/(double) toSize.Height) { oh = original.Height; ow = original.Height*toWidth/toHeight; y = 0; x = (original.Width - ow)/2; } else { ow = original.Width; oh = original.Width*height/toWidth; x = 0; y = (original.Height - oh)/2; } } else { x = (toSize.Width - imageSize.Width)/2; y = (toSize.Height - imageSize.Height)/2; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle(x, y, imageSize.Width, imageSize.Height), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (graphicsHandler != null) { graphicsHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); ImageCodecInfo imgCode = null; if (format != null) { string formatStr = format.ToString(); imgCode = imageCoders.ContainsKey(formatStr) ? imageCoders[formatStr] : null; } if (imgCode != null) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, imageQuality > 100 || imageQuality <= 0 ? 100L : imageQuality); ep.Param[1] = new EncoderParameter(Encoder.Compression, compression < 1L ? 100L : compression); bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return ms.ToArray(); }
/// <summary> /// 生成水印并保存到文件 /// </summary> /// <param name="imgPath">图片路径</param> /// <param name="waterPath">水印路径</param> /// <param name="pos">水印位置</param> /// <param name="handler">图片处理</param> /// <returns></returns> public static byte[] MakeWatermarkImage(string imgPath, string waterPath, WatermarkPosition pos, ImageGraphicsHandler handler) { Image img = Image.FromFile(imgPath), water = Image.FromFile(waterPath); byte[] bytes = MakeWatermarkImage(img, water, pos, handler); img.Dispose(); return bytes; }
/// <summary> /// 生成水印 /// </summary> /// <param name="img">图片</param> /// <param name="water">水印</param> /// <param name="pos">预置的水印位置</param> /// <param name="format">图像格式</param> /// <param name="handler">处理图片</param> /// <returns>图片内存流</returns> public static byte[] MakeWatermarkImage( Image img, Image water, WatermarkPosition pos, ImageGraphicsHandler handler) { int x; int y; int xOffset = 5; //宽度偏移量 int yOffset = 5; //高度偏移量 switch (pos) { case WatermarkPosition.Bottom: //正下方 x = (img.Width - water.Width)/2; y = img.Height - water.Height - yOffset; break; case WatermarkPosition.LeftBottom: //左下方 x = xOffset; y = img.Height - water.Height - yOffset; break; case WatermarkPosition.Right: //右方 x = img.Width - water.Width - xOffset; y = (img.Height - water.Height)/2; break; case WatermarkPosition.Middle: //正中 x = (img.Width - water.Width)/2; y = (img.Height - water.Height)/2; break; case WatermarkPosition.Left: //左边 x = xOffset; y = (img.Height - water.Height)/2; break; case WatermarkPosition.RightTop: //右上方 x = img.Width - water.Width - xOffset; y = yOffset; break; case WatermarkPosition.Top: //正上方 x = (img.Width - water.Width)/2; y = yOffset; break; case WatermarkPosition.LeftTop: //左上方 x = xOffset; y = yOffset; break; default: case WatermarkPosition.Default: case WatermarkPosition.RightBottom: //右下角 x = img.Width - water.Width - xOffset; y = img.Height - water.Height - yOffset; break; } return MakeWatermarkImage(img, water, new Point(x, y), handler); }
/// <summary> /// 生成水印 /// </summary> /// <param name="img">图片</param> /// <param name="water">水印</param> /// <param name="waterPos">水印位置</param> /// <param name="handler">处理图片</param> /// <returns>图片内存流</returns> public static byte[] MakeWatermarkImage( Image img, Image water, Point waterPos, ImageGraphicsHandler handler) { //尺寸检测 if (water.Width > img.Width || water.Height > img.Height) { throw new ArgumentException("水印图片尺寸超过原图,无法生成水印!"); } //绘制水印 Graphics g = Graphics.FromImage(img); g.DrawImage(water, waterPos.X, waterPos.Y); if (handler != null) { handler(img); if (img == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存流 MemoryStream ms = new MemoryStream(); img.Save(ms, img.RawFormat); //释放资源 g.Dispose(); water.Dispose(); byte[] bytes = ms.ToArray(); ms.Dispose(); return bytes; }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="thumbHandler">对缩略图处理</param> public static MemoryStream MakeThumbnail(Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler thumbHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; int oh = original.Height; switch (mode) { case ImageSizeMode.SuitWidth: toHeight = original.Height*width/original.Width; break; case ImageSizeMode.SuitHeight: toWidth = original.Width*height/original.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (original.Width > original.Height) { toHeight = original.Height*width/original.Width; } else //根据高度适配 { toWidth = original.Width*height/original.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double) original.Width/(double) original.Height > (double) toWidth/(double) toHeight) { toWidth = width; toHeight = original.Height*width/original.Width; } else { toHeight = height; toWidth = original.Width*height/original.Height; } break; //裁剪 case ImageSizeMode.Cut: if ((double) original.Width/(double) original.Height > (double) toWidth/(double) toHeight) { oh = original.Height; ow = original.Height*toWidth/toHeight; y = 0; x = (original.Width - ow)/2; } else { ow = original.Width; oh = original.Width*height/toWidth; x = 0; y = (original.Height - oh)/2; } break; default: case ImageSizeMode.CustomSize: break; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle((width - toWidth)/2, (height - toHeight)/2, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (thumbHandler != null) { thumbHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); //图片质量参数 EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 60L); ImageCodecInfo imgCode = null; ImageCodecInfo[] imgCodes = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo code in imgCodes) { if (code.MimeType == "image/jpeg") { imgCode = code; break; } } if (imgCode != null) { bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return ms; }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler graphicsHandler) { return DrawBySize(original, mode, width, height, ImageFormat.Jpeg, 100L, 60L, graphicsHandler); }