如何將水印圖像添加到其他圖像上?將水印圖像放在其他圖像上(C#,ASP.Net)
我可以將文字放在圖像上作爲水印,但是現在我有一張圖像,我想放在那裏而不是文本。我如何在C#中執行此操作?
再來一次具體,我有圖像X,我想用它作爲水印符號。我希望這個符號出現在我的網站上顯示的所有圖像上。因此,我將有圖像X水印圖像Y和Z.
下面的代碼我現在有創建水印:
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);
}
從你上面的代碼另一種解決方案,它與GIF格式圖片的問題。 – 2009-09-16 04:05:33
你,我不擔心GIF。我所有的圖片都是JPG格式,水印是PNG,所以我只是忽略了這些。 – Miles 2009-09-19 16:01:31