注:效果如下图
效果图1 效果图2
原理1:将用户的个人信息写在图片上
项目的解决方案如下请看标记的地方:
代码展示如下:
先引用 图片和文件处理类
using System.Drawing;
using System.IO;
代码如下:
Random rd = new Random();
int a = rd.Next(1, 4);
///图片的路径 这里可以提供多张背景图随机选择
string path = Server.MapPath(".") + "/img/" + a + ".png";
//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0,640, 1080);
Font f = new Font("Microsoft Yahei", 32);
Brush b = new SolidBrush(Color.Black);
string addText = "呢称";
g.DrawString(addText, f, b, 230, 100);
g.Dispose();
//加图片水印 直接用之前的流不用再次读取
// System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(".") + "/img/tx.jpg");
Graphics g2 = Graphics.FromImage(image);
g2.DrawImage(copyImage, new Rectangle(15, 15, 200, 200), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g2.Dispose();
//添加二维码快
System.Drawing.Image copyImage2 = System.Drawing.Image.FromFile(Server.MapPath(".") + "/img/ewm.png");
Graphics g3 = Graphics.FromImage(image);
g3.DrawImage(copyImage2, new Rectangle(100, 560, 400, 400), 0, 0, copyImage2.Width, copyImage2.Height, GraphicsUnit.Pixel);
g3.Dispose();
//添加失效日期
Graphics g4 = Graphics.FromImage(image);
g4.DrawImage(image, 0, 0, 640, 1080);
Font f1 = new Font("Microsoft Yahei", 16);
Brush b1 = new SolidBrush(Color.Black);
string addText2 = "于" + DateTime.Now.AddDays(7).ToString("yyyy-MM-dd") + "失效";
g4.DrawString(addText2, f1, b1, 200, 1000);
g4.Dispose();
//保存加水印过后的图片
string newPath = Server.MapPath(".") + "/img/bg2.jpg";
image.Save(newPath);
image.Dispose();