发表:联高软件www.legalsoft.com.cn 摘要:文章:利用GDI+ for.NET 给图片加水印标记 摘要:说明:增加了判断水印图大小超过原图的情况,需要控制显示位置请自行修改。支持多种格式图片。文字翻译来自小新0574。/,发表于北京联高软件有限公司技术文章栏目,代码以高亮显示。
关键字:水印, 图片, for, gdi, 标记, new, int, bitmap, imgphoto, phwidth, 对象, float, imageattributes, phheight, colormap, param, markwidth, 绘制, 定义
说明:增加了判断水印图大小超过原图的情况,需要控制显示位置请自行修改。支持多种格式图片。文字翻译来自小新0574。
-
-
-
-
-
-
-
-
-
- public void BuildWatermark(string rSrcImgPath,string rMarkImgPath,string rMarkText,string rDstImgPath)
- {
-
-
- Image imgPhoto = Image.FromFile(rSrcImgPath);
- int phWidth = imgPhoto.Width;
- int phHeight = imgPhoto.Height;
- Bitmap bmPhoto=new Bitmap(phWidth,phHeight, PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(72,72);
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
-
-
- Image imgWatermark = new Bitmap(rMarkImgPath);
- int wmWidth = imgWatermark.Width;
- int wmHeight = imgWatermark.Height;
-
-
- grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
- grPhoto.DrawImage(
- imgPhoto,
- new Rectangle(0, 0, phWidth, phHeight),
- 0,
- 0,
- phWidth,
- phHeight,
- GraphicsUnit.Pixel);
-
-
-
- int[] sizes = new int[]{16,14,12,10,8,6,4};
- Font crFont = null;
- SizeF crSize = new SizeF();
- for (int i=0 ;i<7; i++)
- {
- crFont = new Font("arial", sizes[i],
- FontStyle.Bold);
- crSize = grPhoto.MeasureString(rMarkText,
- crFont);
- if((ushort)crSize.Width < (ushort)phWidth)
- break;
- }
-
-
-
- int yPixlesFromBottom = (int)(phHeight *.05);
- float yPosFromBottom = ((phHeight -
- yPixlesFromBottom)-(crSize.Height/2));
- float xCenterOfImg = (phWidth/2);
- StringFormat StrFormat = new StringFormat();
- StrFormat.Alignment = StringAlignment.Center;
-
-
-
- SolidBrush semiTransBrush2 =
- new SolidBrush(Color.FromArgb(153, 0, 0,0));
- grPhoto.DrawString(rMarkText,
- crFont,
- semiTransBrush2,
- new PointF(xCenterOfImg+1,yPosFromBottom+1),
- StrFormat);
- SolidBrush semiTransBrush = new SolidBrush(
- Color.FromArgb(153, 255, 255, 255));
- grPhoto.DrawString(rMarkText,
- crFont,
- semiTransBrush,
- new PointF(xCenterOfImg,yPosFromBottom),
- StrFormat);
-
- Bitmap bmWatermark = new Bitmap(bmPhoto);
- bmWatermark.SetResolution(
- imgPhoto.HorizontalResolution,
- imgPhoto.VerticalResolution);
- Graphics grWatermark =
- Graphics.FromImage(bmWatermark);
-
-
-
- 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[][] 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, 0.3f, 0.0f},
- new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
- };
- ColorMatrix wmColorMatrix = new
- ColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(wmColorMatrix,
- ColorMatrixFlag.Default,
- ColorAdjustType.Bitmap);
-
-
- int markWidth;
- int markHeight;
-
- if(phWidth<=wmWidth)
- {
- markWidth = phWidth-10;
- markHeight = (markWidth*wmHeight)/wmWidth;
- }
- else if(phHeight<=wmHeight)
- {
- markHeight = phHeight-10;
- markWidth = (markHeight*wmWidth)/wmHeight;
- }
- else
- {
- markWidth = wmWidth;
- markHeight = wmHeight;
- }
- int xPosOfWm = ((phWidth - markWidth)-10);
- int yPosOfWm = 10;
- grWatermark.DrawImage(imgWatermark,
- new Rectangle(xPosOfWm,yPosOfWm,markWidth,
- markHeight),
- 0,
- 0,
- wmWidth,
- wmHeight,
- GraphicsUnit.Pixel,
- imageAttributes);
-
- imgPhoto = bmWatermark;
- grPhoto.Dispose();
- grWatermark.Dispose();
- imgPhoto.Save(rDstImgPath,ImageFormat.Jpeg);
- imgPhoto.Dispose();
- imgWatermark.Dispose();
- }