2011-10-27 8 views
0

Je cherche un moyen d'ajouter un filigrane sur l'image avec minime perte de qualité. Quelqu'un pourrait-il m'aider? Le filigrane peut être un texte ou une image.Ajout de filigrane sur l'image

+0

vous voulez un filigrane visible ou invisible? – ObscureRobot

+0

Qu'est-ce que cela signifie dans un filigrane invisible? Transparent? J'ai plutôt besoin d'un filigrane transparent. – Alexandre

+2

Les filigranes que vous décrivez existent dans les données d'image. Je parle de filigranes qui vivent dans les métadonnées ou qui se cachent dans la structure de données de l'image. Un exemple trivial serait l'insertion d'une chaîne de copyright dans les données EXIF. Un exemple plus sophistiqué impliquerait plusieurs morceaux de données EXIF ​​apparemment non apparentés qui peuvent être combinés en une somme de contrôle. Ce que vous préférez dépend de votre objectif. – ObscureRobot

Répondre

1

Que diriez-vous de cette méthode pour ajouter l'image en filigrane à l'image

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

Cette méthode sauvegarde-t-elle l'image avec un filigrane de haute qualité? – Alexandre

+0

oui il devrait être .... – rockyashkumar

+0

Il ne compile pas à cause de cette ligne --- gr.DrawImage (position, size, overlayImage); position, taille et overlayImage sont indéfinis. – Alexandre