/eBlog/eBlog.Common/Image/ImageHelper.cs
# · C# · 358 lines · 279 code · 41 blank · 38 comment · 25 complexity · a5fb1d6199475e652b0e88b2116782f0 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.Drawing.Imaging;
- using System.Text;
- using System.Drawing;
-
- namespace eBlog.Common
- {
- public class ImageHelper
- {
-
- /// <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, string 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 "HW"://指定高宽缩放(可能变形)
- break;
- case "W"://指定宽,高按比例
- toheight = originalImage.Height * width / originalImage.Width;
- break;
- case "H"://指定高,宽按比例
- towidth = originalImage.Width * height / originalImage.Height;
- break;
- case "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图片
- Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
-
- //新建一个画板
- 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
- {
- //以jpg格式保存缩略图
- bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- catch (System.Exception e)
- {
- throw e;
- }
- finally
- {
- originalImage.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- }
-
- /// <summary>
- /// 添加图片水印
- /// </summary>
- /// <param name="oldFilePath">原始图片路径</param>
- /// <param name="newFilePath">将要添加水印图片路径</param>
- /// <param name="waterPosition">水印位置</param>
- /// <param name="waterImagePath">水印图片路径</param>
- /// <param name="watermarkTransparency"></param>
- /// <param name="quality">质量</param>
- public static void CreateWaterImage(string oldFilePath, string newFilePath, int waterPosition, string waterImagePath, int watermarkTransparency, int quality)
- {
- System.Drawing.Image image = System.Drawing.Image.FromFile(oldFilePath);
-
- Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
-
- Graphics g = Graphics.FromImage(bmp);
- g.Clear(Color.White);
-
- g.DrawImage(image, 0, 0, image.Width, image.Height);
-
- //设置透明度
- System.Drawing.Image watermark = new Bitmap(waterImagePath);
- 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 transparency = 0.5F;
- if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
- {
- transparency = (watermarkTransparency / 10.0F);
- }
-
- 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, transparency, 0.0f}, //注意:倒数第二处为0.0f为完全透明,1.0f为完全不透明
- 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 _width = image.Width;
- int _height = image.Height;
- int xpos = 0;
- int ypos = 0;
- int WatermarkWidth = 0;
- int WatermarkHeight = 0;
- double bl = 1d;
- //计算水印图片的比率
- //取背景的1/4宽度来比较
- if ((_width > watermark.Width * 2) && (_height > watermark.Height * 2))
- {
- bl = 1;
- }
- else if ((_width > watermark.Width * 2) && (_height < watermark.Height * 2))
- {
- bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);
-
- }
- else if ((_width < watermark.Width * 2) && (_height > watermark.Height * 2))
- {
- bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
- }
- else
- {
- if ((_width * watermark.Height) > (_height * watermark.Width))
- {
- bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);
- }
- else
- {
- bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
- }
- }
- WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
- WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
- switch (waterPosition)
- {
- case 3:
- xpos = _width - WatermarkWidth - 10;
- ypos = 10;
- break;
- case 2:
- xpos = 10;
- ypos = _height - WatermarkHeight - 10;
- break;
- case 5:
- xpos = _width / 2 - WatermarkWidth / 2;
- ypos = _height / 2 - WatermarkHeight / 2;
- break;
- case 1:
- xpos = 10;
- ypos = 10;
- break;
- case 4:
- default:
- xpos = _width - WatermarkWidth - 10;
- ypos = _height - WatermarkHeight - 10;
- break;
- }
- g.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
- try
- {
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
- ImageCodecInfo ici = null;
- foreach (ImageCodecInfo codec in codecs)
- {
- if (codec.MimeType.IndexOf("jpeg") > -1)
- {
- ici = codec;
- }
- }
- EncoderParameters encoderParams = new EncoderParameters();
- long[] qualityParam = new long[1];
-
- if (quality < 0 || quality > 100)
- {
- quality = 80;
- }
-
- qualityParam[0] = quality;
-
- EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
- encoderParams.Param[0] = encoderParam;
-
- if (ici != null)
- {
- bmp.Save(newFilePath, ici, encoderParams);
- }
- else
- {
- bmp.Save(newFilePath);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- watermark.Dispose();
- imageAttributes.Dispose();
- image.Dispose();
- bmp.Dispose();
- }
- }
-
- /// <summary>
- /// 添加文字水印
- /// </summary>
- /// <param name="oldFilePath">原始图片路径</param>
- /// <param name="newFilePath">将要添加水印图片路径</param>
- /// <param name="waterPosition">水印位置</param>
- /// <param name="waterText">水印内容</param>
- /// <param name="quality"></param>
- /// <param name="fontname"></param>
- /// <param name="fontsize"></param>
- public static void CreateWaterText(string oldFilePath, string newFilePath, int waterPosition, string waterText, int quality, string fontname, int fontsize)
- {
- System.Drawing.Image image = System.Drawing.Image.FromFile(oldFilePath);
- Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
-
- Graphics g = Graphics.FromImage(bmp);
- g.Clear(Color.White);
-
- g.DrawImage(image, 0, 0, image.Width, image.Height);
-
- int _width = bmp.Width;
- int _height = bmp.Height;
-
- Font crFont = new Font(fontname, fontsize, FontStyle.Bold, GraphicsUnit.Pixel);
- SizeF crSize = g.MeasureString(waterText, crFont);
-
- float xpos = 0;
- float ypos = 0;
- switch (waterPosition)
- {
- case 3:
- xpos = ((float)_width * (float).99) - (crSize.Width / 2);
- ypos = (float)_height * (float).01;
- break;
- case 2:
- xpos = ((float)_width * (float).01) + (crSize.Width / 2);
- ypos = ((float)_height * (float).99) - crSize.Height;
- break;
- case 5:
- xpos = ((_width - crSize.Width) / 2) + crSize.Width / 2; //奇怪的表达式
- ypos = (_height - crSize.Height) / 2 + crSize.Height / 2;
- break;
- case 1:
-
- xpos = ((float)_width * (float).01) + (crSize.Width / 2);
- ypos = (float)_height * (float).01;
- break;
-
- case 4:
- default:
- xpos = ((float)_width * (float).99) - (crSize.Width / 2);
- ypos = ((float)_height * (float).99) - crSize.Height;
- break;
- }
-
- StringFormat StrFormat = new StringFormat();
- StrFormat.Alignment = StringAlignment.Center;
-
- //可设置透明度
- SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));
- g.DrawString(waterText, crFont, semiTransBrush, xpos, ypos, StrFormat);
-
- try
- {
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
- ImageCodecInfo ici = null;
- foreach (ImageCodecInfo codec in codecs)
- {
- if (codec.MimeType.IndexOf("jpeg") > -1)
- {
- ici = codec;
- }
- }
- EncoderParameters encoderParams = new EncoderParameters();
- long[] qualityParam = new long[1];
-
- if (quality < 0 || quality > 100)
- {
- quality = 80;
- }
-
- qualityParam[0] = quality;
-
-
- EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
- encoderParams.Param[0] = encoderParam;
-
- if (ici != null)
- {
- bmp.Save(newFilePath, ici, encoderParams);
- }
- else
- {
- bmp.Save(newFilePath);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- semiTransBrush.Dispose();
- image.Dispose();
- bmp.Dispose();
- }
- }
- }
- }