C#给图片加水印标记,可设置透明度

  1. // <summary>  

  2. /// Creating a Watermarked Photograph with GDI+ for .NET  

  3. /// </summary>  

  4. /// <param name="rSrcImgPath">原始图片的物理路径</param>  

  5. /// <param name="rMarkImgPath">水印图片的物理路径</param>  

  6. /// <param name="rMarkText">水印文字(不显示水印文字设为空串)</param>  

  7. /// <param name="rDstImgPath">输出合成后的图片的物理路径</param>  

  8. /// @整理: anyrock@mending.cn  

  9. public void BuildWatermark(string rSrcImgPath,string rMarkImgPath,string rMarkText,string rDstImgPath)  

  10. {  

  11.      //以下(代码)从一个指定文件创建了一个Image 对象,然后为它的 Width 和 Height定义变量。  

  12.      //这些长度待会被用来建立一个以24 bits 每像素的格式作为颜色数据的Bitmap对象。  

  13.      Image imgPhoto = Image.FromFile(rSrcImgPath);  

  14.      int phWidth = imgPhoto.Width;  

  15.      int phHeight = imgPhoto.Height;  

  16.      Bitmap bmPhoto=new Bitmap(phWidth,phHeight, PixelFormat.Format24bppRgb);  

  17.      bmPhoto.SetResolution(72,72);  

  18.      Graphics grPhoto = Graphics.FromImage(bmPhoto);  

  19.      //这个代码载入水印图片,水印图片已经被保存为一个BMP文件,以绿色(A=0,R=0,G=255,B=0)作为背景颜色。  

  20.      //再一次,会为它的Width 和Height定义一个变量。  

  21.      Image imgWatermark = new Bitmap(rMarkImgPath);  

  22.      int wmWidth = imgWatermark.Width;  

  23.      int wmHeight = imgWatermark.Height;  

  24.      //这个代码以100%它的原始大小绘制imgPhoto 到Graphics 对象的(x=0,y=0)位置。  

  25.      //以后所有的绘图都将发生在原来照片的顶部。  

  26.      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;  

  27.      grPhoto.DrawImage(  

  28.           imgPhoto,                                        

  29.           new Rectangle(0, 0, phWidth, phHeight),   

  30.           0,                                                    

  31.           0,                                                      

  32.           phWidth,                                          

  33.           phHeight,                                        

  34.           GraphicsUnit.Pixel);  

  35.      //为了最大化版权信息的大小,我们将测试7种不同的字体大小来决定我们能为我们的照片宽度使用的可能的最大大小。  

  36.      //为了有效地完成这个,我们将定义一个整型数组,接着遍历这些整型值测量不同大小的版权字符串。  

  37.      //一旦我们决定了可能的最大大小,我们就退出循环,绘制文本  

  38.      int[] sizes = new int[]{16,14,12,10,8,6,4};  

  39.      Font crFont = null;   

  40.      SizeF crSize = new  SizeF();   

  41.      for (int i=0 ;i<7; i++)  

  42.      {   

  43.           crFont = new Font("arial", sizes[i],  

  44.                 FontStyle.Bold);  

  45.           crSize = grPhoto.MeasureString(rMarkText,  

  46.                 crFont);  

  47.           if((ushort)crSize.Width < (ushort)phWidth)  

  48.                 break;  

  49.      }  

  50.      //因为所有的照片都有各种各样的高度,所以就决定了从图象底部开始的5%的位置开始。  

  51.      //使用rMarkText字符串的高度来决定绘制字符串合适的Y坐标轴。  

  52.      //通过计算图像的中心来决定X轴,然后定义一个StringFormat 对象,设置StringAlignment 为Center。  

  53.      int yPixlesFromBottom = (int)(phHeight *.05);  

  54.      float yPosFromBottom = ((phHeight -   

  55.           yPixlesFromBottom)-(crSize.Height/2));  

  56.      float xCenterOfImg = (phWidth/2);  

  57.      StringFormat StrFormat = new StringFormat();  

  58.      StrFormat.Alignment = StringAlignment.Center;  

  59.      //现在我们已经有了所有所需的位置坐标来使用60%黑色的一个Color(alpha值153)创建一个SolidBrush 。  

  60.      //在偏离右边1像素,底部1像素的合适位置绘制版权字符串。  

  61.      //这段偏离将用来创建阴影效果。使用Brush重复这样一个过程,在前一个绘制的文本顶部绘制同样的文本。  

  62.      SolidBrush semiTransBrush2 =   

  63.           new SolidBrush(Color.FromArgb(153, 0, 0,0));   

  64.      grPhoto.DrawString(rMarkText,                          

  65.           crFont,                                                   

  66.           semiTransBrush2,                                      

  67.           new PointF(xCenterOfImg+1,yPosFromBottom+1),   

  68.           StrFormat);  

  69.      SolidBrush semiTransBrush = new SolidBrush(  

  70.           Color.FromArgb(153, 255, 255, 255));  

  71.      grPhoto.DrawString(rMarkText,                      

  72.           crFont,                                               

  73.           semiTransBrush,                                     

  74.           new PointF(xCenterOfImg,yPosFromBottom),    

  75.           StrFormat);  

  76.      //根据前面修改后的照片创建一个Bitmap。把这个Bitmap载入到一个新的Graphic对象。  

  77.      Bitmap bmWatermark = new Bitmap(bmPhoto);   

  78.      bmWatermark.SetResolution(  

  79.           imgPhoto.HorizontalResolution,   

  80.           imgPhoto.VerticalResolution);  

  81.      Graphics grWatermark =  

  82.           Graphics.FromImage(bmWatermark);  

  83.      //通过定义一个ImageAttributes 对象并设置它的两个属性,我们就是实现了两个颜色的处理,以达到半透明的水印效果。  

  84.      //处理水印图象的第一步是把背景图案变为透明的(Alpha=0, R=0, G=0, B=0)。我们使用一个Colormap 和定义一个RemapTable来做这个。  

  85.      //就像前面展示的,我的水印被定义为100%绿色背景,我们将搜到这个颜色,然后取代为透明。  

  86.      ImageAttributes imageAttributes =  

  87.           new ImageAttributes();  

  88.      ColorMap colorMap = new ColorMap();  

  89.      colorMap.OldColor=Color.FromArgb(255, 0, 255, 0);  

  90.      colorMap.NewColor=Color.FromArgb(0, 0, 0, 0);  

  91.      ColorMap[] remapTable = {colorMap};  

  92.      //第二个颜色处理用来改变水印的不透明性。  

  93.      //通过应用包含提供了坐标的RGBA空间的5x5矩阵来做这个。  

  94.      //通过设定第三行、第三列为0.3f我们就达到了一个不透明的水平。结果是水印会轻微地显示在图象底下一些。  

  95.      imageAttributes.SetRemapTable(remapTable,  

  96.           ColorAdjustType.Bitmap);  

  97.      float[][] colorMatrixElements = {   

  98.                                                      new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},  

  99.                                                      new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},  

  100.                                                      new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},  

  101.                                                      new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},  

  102.                                                      new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}  

  103.                                                 };  

  104.      ColorMatrix wmColorMatrix = new  

  105.           ColorMatrix(colorMatrixElements);  

  106.      imageAttributes.SetColorMatrix(wmColorMatrix,   

  107.           ColorMatrixFlag.Default,   

  108.           ColorAdjustType.Bitmap);  

  109.      //随着两个颜色处理加入到imageAttributes 对象,我们现在就能在照片右手边上绘制水印了。  

  110.      //我们会偏离10像素到底部,10像素到左边。  

  111.      int markWidth;  

  112.      int markHeight;  

  113.      //mark比原来的图宽  

  114.      if(phWidth<=wmWidth)  

  115.      {  

  116.           markWidth = phWidth-10;  

  117.           markHeight = (markWidth*wmHeight)/wmWidth;  

  118.      }  

  119.      else if(phHeight<=wmHeight)  

  120.      {  

  121.           markHeight = phHeight-10;  

  122.           markWidth = (markHeight*wmWidth)/wmHeight;  

  123.      }  

  124.      else  

  125.      {  

  126.           markWidth = wmWidth;  

  127.           markHeight = wmHeight;  

  128.      }  

  129.      int xPosOfWm = ((phWidth - markWidth)-10);  

  130.      int yPosOfWm = 10;  

  131.      grWatermark.DrawImage(imgWatermark,   

  132.           new Rectangle(xPosOfWm,yPosOfWm,markWidth,  

  133.           markHeight),  

  134.           0,                          

  135.           0,                           

  136.           wmWidth,                

  137.           wmHeight,               

  138.           GraphicsUnit.Pixel,   

  139.           imageAttributes);  

  140.      //最后的步骤将是使用新的Bitmap取代原来的Image。 销毁两个Graphic对象,然后把Image 保存到文件系统。  

  141.      imgPhoto = bmWatermark;  

  142.      grPhoto.Dispose();  

  143.      grWatermark.Dispose();  

  144.      imgPhoto.Save(rDstImgPath,ImageFormat.Jpeg);  

  145.      imgPhoto.Dispose();  

  146.      imgWatermark.Dispose();                  

  147. }  
posted @ 2012-10-16 19:14  追风  阅读(604)  评论(0)    收藏  举报