2009-08-03 2 views
17

Wie füge ich ein Wasserzeichenbild auf andere Bilder hinzu?Platzieren Sie Wasserzeichen auf andere Bilder (C#, ASP.Net)

Ich bin in der Lage, Text auf ein Bild als Wasserzeichen zu platzieren, aber jetzt habe ich ein Bild, das ich anstelle des Textes dort anbringen möchte. Wie mache ich das in C#?

Nur noch einmal um genau zu sein, ich habe Bild X und ich möchte es als Wasserzeichen verwenden. Ich möchte, dass dieses Symbol auf meinem gesamten Bild erscheint, wenn es auf meiner Website angezeigt wird. So werde ich Bild ohne Wasserzeichen X auf das Bild Y und Z.

Hier ist der Code, den ich zur Zeit haben, dass das Wasserzeichen erzeugt:

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); 

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

Von Ihrem Code verwendet, hat es Probleme mit Foto GIF-Format. –

+0

ya, ich habe mir keine Sorgen um GIFs gemacht. Alle meine Bilder sind JPGs und das Wasserzeichen ist ein PNG, also ignorierte ich sie einfach. – Miles

Antwort

11

an der gleichen Stelle, wo Sie gr.DrawString aufrufen, können Sie auch tun gr.DrawImage (position, size, overlayImage). Es hilft, wenn Ihr Bild-Overlay aus einer PNG-Datei (mit Transparenz) geladen wird, um die beste Qualität zu erzielen.

+1

ahhhhh ich fühle mich jetzt wie ein Idiot ... hahaha, Danke – Miles

Verwandte Themen