C#給圖片加浮水印標記,可設定透明度

來源:互聯網
上載者:User
  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. }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.