其他地方的图像水印图像(C#,ASP.Net) [英] Place watermark image on other images (C#, ASP.Net)
问题描述
我如何添加水印图像到其它图像?
我能够把文字图像作为一个水痕上,但现在我有,我想放在那里而不是文字的图像。我该怎么做这在C#?
只是一个更多的时间是具体的,我有图像的X,我想用它作为水印标志。我想在我的网站上显示时,该符号在我所有的图像出现。因此,我将有图像的X水印图像Y和Z
这里的code我现在有一个创建水印:
公共静态无效AddWaterMark(MemoryStream的小姐,串watermarkText,MemoryStream的OutputStream中)
{
为System.Drawing.Image IMG = System.Drawing.Image.FromStream(毫秒);
图形克= Graphics.FromImage(IMG);
字体的字体=新的字体(宋体,(浮点)40);
色色= Color.FromArgb(50,241,235,105);
双切=(双)img.Height /(双)img.Width;
双角= Math.Atan(切)*(180 / Math.PI);
双halfHypotenuse =的Math.sqrt((img.Height * img.Height)+(img.Width * img.Width))/ 2;
双正弦,余弦,opp1,ADJ1,opp2,ADJ2; 的for(int i = 100; I> 0;我 - )
{
字体=新的字体(宋体,我,FontStyle.Bold);
的SizeF的SizeF = gr.MeasureString(watermarkText,字体,int.MaxValue); 罪= Math.Sin(角*(Math.PI / 180));
COS = Math.Cos(角*(Math.PI / 180));
opp1 =罪* sizef.Width;
ADJ1 = COS * sizef.Height;
opp2 =罪* sizef.Height;
ADJ2 = COS * sizef.Width; 如果(opp1 + ADJ1< img.Height&放大器;&安培; opp2 + ADJ2< img.Width)
打破;
//
} 的StringFormat的StringFormat =新的StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center; gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.RotateTransform((浮点)角);
gr.DrawString(watermarkText,字体,新SolidBrush(颜色),新的点((int)的halfHypotenuse,0),的StringFormat); img.Save(OutputStream的,ImageFormat.Jpeg);
}
在调用gr.DrawString同一个地方
,你也可以做gr.DrawImage(位置,大小,窗格在overlayImage)。它帮助,如果你的图像到覆盖从PNG文件(透明)加载到生产出质量最好的。
How do I add a watermark image onto other images?
I'm able to place text on an image as a water mark but now I have an image that I'd like to put on there instead of the text. How do I do this in C#?
Just one more time to be specific, I have image X and I want to use it as a watermark symbol. I want this symbol to appear on all my image when shown on my website. So I will have image X watermarked on image Y and Z.
Here's the code I currently have that creates the watermark:
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);
}
in the same place where you call gr.DrawString, you can also do 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.
这篇关于其他地方的图像水印图像(C#,ASP.Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!