2011-10-27 96 views
0

我正在尋找一種在圖像上添加水印的方法,最小質量損失最小。任何人都可以幫我嗎?水印可以是文字或圖像。在圖像上添加水印

+0

你想可見或不可見水印添加圖像作爲水印圖像? – ObscureRobot

+0

隱形水印中的含義是什麼?透明?相反,我需要一個透明的水印。 – Alexandre

+2

您描述的水印存在於圖像數據中。我正在談論生活在元數據中的水印或隱藏在圖像的數據結構中。一個簡單的例子就是在EXIF數據中插入一個版權字符串。一個更復雜的例子會涉及幾個看起來不相關的EXIF數據片段,它們可以組合成一個校驗和。你喜歡哪個取決於你的目標。 – ObscureRobot

回答

1

這個怎麼樣方法

public static void AddWaterMark(MemoryStream ms, string watermarkText, MemoryStream outputStream) 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 
      Graphics gr = Graphics.FromImage(img); 
      Font font = new Font("Tahoma", (float)40); 
      Color color = Color.FromArgb(50, 241, 235, 105); 
      double tangent = (double)img.Height/(double)img.Width; 
      double angle = Math.Atan(tangent) * (180/Math.PI); 
      double halfHypotenuse = Math.Sqrt((img.Height * img.Height) + (img.Width * img.Width))/2; 
      double sin, cos, opp1, adj1, opp2, adj2; 

      for (int i = 100; i > 0; i--) 
      { 
       font = new Font("Tahoma", i, FontStyle.Bold); 
       SizeF sizef = gr.MeasureString(watermarkText, font, int.MaxValue); 

       sin = Math.Sin(angle * (Math.PI/180)); 
       cos = Math.Cos(angle * (Math.PI/180)); 
       opp1 = sin * sizef.Width; 
       adj1 = cos * sizef.Height; 
       opp2 = sin * sizef.Height; 
       adj2 = cos * sizef.Width; 

       if (opp1 + adj1 < img.Height && opp2 + adj2 < img.Width) 
        break; 
       // 
      } 

      StringFormat stringFormat = new StringFormat(); 
      stringFormat.Alignment = StringAlignment.Center; 
      stringFormat.LineAlignment = StringAlignment.Center; 

      gr.SmoothingMode = SmoothingMode.AntiAlias; 
      gr.RotateTransform((float)angle); 
      gr.DrawString(watermarkText, font, new SolidBrush(color), new Point((int)halfHypotenuse, 0), stringFormat); 
      gr.DrawImage(position, size, overlayImage) // It helps if your image-to-overlay is loaded from a PNG file (with transparency) to produce the best quality. 

      img.Save(outputStream, ImageFormat.Jpeg); 
     } 
+0

此方法是否以高質量保存具有水印的圖像? – Alexandre

+0

是的,它應該是.... – rockyashkumar

+0

它不會編譯,因爲這一行--- gr.DrawImage(position,size,overlayImage);位置,大小和overlayImage是未定義的。 – Alexandre