Csharp/C#教程:将水印图像放在其他图像上(C#,ASP.Net)分享


将水印图像放在其他图像上(C#,ASP.Net)

如何在其他图像上添加水印图像?

我可以将图像上的文字作为水印,但现在我有一个我想放在那里而不是文字的图像。 我如何在C#中执行此操作?

再过一次具体,我有图像X,我想用它作为水印符号。 我希望在我的网站上显示时,此符号会显示在我的所有图像上。 因此,我将在图像Y和Z上对图像X进行水印处理。

这是我目前创建水印的代码:

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

在你调用gr.DrawString的同一个地方,你也可以做gr.DrawImage(position,size,overlayImage)。 如果从PNG文件(具有透明度)加载图像到叠加层以产生最佳质量,则会有所帮助。

这是另一种使用GDI +的解决方案

使用GDI + for .NET创建水印照片

上述就是C#学习教程:将水印图像放在其他图像上(C#,ASP.Net)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1000058.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐