• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  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. /// @整理: [email protected]  
  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来做这个。  

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Visual C# 2005 - 如何制作多变化字体之向量字发布时间:2022-07-13
下一篇:
C#版Websocket实例发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap