模拟水印相机

上效果图

 

 
public void WaterMark()
        {
            string sourcePath = @"D:\0.测试图片\微信图片_20230829184000.jpg";
            string saveFilePath = @"D:\0.测试图片\WaterMarkImg\";
            using (StreamReader sr = new StreamReader(sourcePath))
            {
                Image image = Image.FromStream(sr.BaseStream);
                //创建一把刷子
                //防止索引像素格式引发异常的像素格式
                Bitmap bmp = new Bitmap(image.Width, image.Height,  PixelFormat.Format24bppRgb);
                Graphics grap = Graphics.FromImage(bmp);
                grap.Clear(Color.White);
                grap.SmoothingMode = SmoothingMode.HighQuality;
                grap.InterpolationMode = InterpolationMode.High;
                grap.DrawImage(image, 0, 0, image.Width, image.Height);
                //计算字体大小
                TextWaterMark(grap, image.Width, image.Height);
                //返回有水印的图片流
                MemoryStream stream = new MemoryStream();
                bmp.Save(stream, ImageFormat.Png);
                var newbytes = stream.GetBuffer();
                var saveName = this.GetSaveName("水印图片.png");
                var fullName = Path.Combine(saveFilePath, saveName);
                var fullPath = Path.GetDirectoryName(fullName);
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }
                using (FileStream fs = new FileStream(fullName, FileMode.Append,  FileAccess.Write))
                {
                    fs.Write(newbytes, 0, newbytes.Length);
                    fs.Close();
                }
            }
        }

 

public void AddTextWaterMark(Item item, Graphics grap, int width, int height)
{
string time = item.BuyDate.ToShortTimeString3();//时间 HH:mm
string day = item.BuyDate.ToShortDateString3() + " " + DateTime.Now.ToWeekString();//日期 yyyy-MM-dd 星期
string address = item.OfflineShopAddress;//地址
string securityCode = "防伪 " + this.GetRandomString(14);//防伪码

//计算字体大小
Font font = new Font("微软雅黑", 14, FontStyle.Bold);
int fontSize = 30;
int[] sizes = new int[] { 30, 28, 26, 24, 22, 20, 18, 16, 14 };
SizeF crSize0 = new SizeF();
for (int i = 0; i < 7; i++)
{
fontSize = sizes[i];
font = new Font("微软雅黑", fontSize, FontStyle.Bold);
crSize0 = grap.MeasureString("已验证考勤信息真实性", font);//水印宽度
if ((ushort)crSize0.Width < (ushort)width / 4)
break;
}
Font font2 = new Font("微软雅黑", fontSize + 6, FontStyle.Bold);//时间:Arial
Font font5 = new Font("微软雅黑", fontSize - 4, FontStyle.Bold);
Font font3 = new Font("微软雅黑", fontSize - 8, FontStyle.Bold);//防伪
Font font4 = new Font("黑体", fontSize + 6, FontStyle.Bold);//打卡

Brush brush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));//白色
Brush brush2 = new SolidBrush(Color.FromArgb(255, 38, 38, 38));//黑色
Brush brush3 = new SolidBrush(Color.FromArgb(255, 4, 46, 113));//蓝色
Brush brush4 = new SolidBrush(Color.FromArgb(255, 254, 191, 50));//黄色
Brush brush5 = new SolidBrush(Color.FromArgb(200, 255, 255, 255));//白色-透明

SizeF crSize = grap.MeasureString(day, font);
SizeF crSize5 = grap.MeasureString("已验证考勤信息真实性", font5);

float xpos = ((float)width * (float).02);
float ypos = ((float)height * (float).99) - crSize5.Height;
grap.RotateTransform(0);
grap.DrawString("已验证考勤信息真实性", font5, brush2, xpos + 1, ypos + 1);
grap.DrawString("已验证考勤信息真实性", font5, brush5, xpos, ypos);

ypos = ypos - 5;//行间距

ypos = ypos - crSize.Height;
//左下角——日期
grap.DrawString(day, font, brush2, xpos + 10 + 1, ypos + 1);
grap.DrawString(day, font, brush, xpos + 10, ypos);

//左下角——地址
SizeF crSize2 = grap.MeasureString(address, font);//水印宽度
var addressList = address.ToCharArray();
int index = addressList.Length;
while (crSize2.Width > (width / 4) * 3)
{
index--;
crSize2 = grap.MeasureString(address.Substring(0, index), font);
}
ypos = ypos - crSize2.Height;
Rectangle rect = new Rectangle(Convert.ToInt32(xpos), Convert.ToInt32(ypos), 2, Convert.ToInt32(crSize2.Height + crSize5.Height));
if (index < addressList.Length)
{
grap.DrawString(address.Substring(index, addressList.Length - index), font, brush2, xpos + 10 + 1, ypos + 1);
grap.DrawString(address.Substring(index, addressList.Length - index), font, brush, xpos + 10, ypos);

ypos = ypos - crSize2.Height;
grap.DrawString(address.Substring(0, index), font, brush2, xpos + 10 + 1, ypos + 1);
grap.DrawString(address.Substring(0, index), font, brush, xpos + 10, ypos);
rect = new Rectangle(Convert.ToInt32(xpos), Convert.ToInt32(ypos), 2, Convert.ToInt32(crSize2.Height) * 2 + Convert.ToInt32(crSize5.Height));
}
else
{
grap.DrawString(address, font, brush2, xpos + 10 + 1, ypos + 1);
grap.DrawString(address, font, brush, xpos + 10, ypos);
}

//设置矩形边框颜色和线条样式-黄色竖条
Pen pen = new Pen(Color.FromArgb(255, 254, 191, 50), 2);
grap.DrawRectangle(pen, rect);

//左下角——时间
SizeF crSize3 = grap.MeasureString(time, font2);//水印宽度
SizeF crSize33 = grap.MeasureString("打卡", font2);//水印宽度

ypos = ypos - 5;//行间距

//白色矩形
int juxingWidth = Convert.ToInt32(crSize3.Width + crSize33.Width);
int juxingHeight = Convert.ToInt32(crSize3.Height);
float y0 = ypos - crSize3.Height - 10;

int radius = 10; // 定义圆角的半径
GraphicsPath gp = new GraphicsPath();
gp.AddArc(xpos, y0, radius, radius, 180, 90);
gp.AddArc(xpos + juxingWidth, y0, radius, radius, 270, 90);
gp.AddArc(xpos + juxingWidth, y0 + juxingHeight, radius, radius, 0, 90);
gp.AddArc(xpos, y0 + juxingHeight, radius, radius, 90, 90);
gp.CloseAllFigures();
grap.FillPath(brush, gp);

//黄色矩形
float x00 = xpos + 4;
int juxingWidth2 = Convert.ToInt32(crSize33.Width);
int juxingHeight2 = Convert.ToInt32(crSize3.Height) - 4;
float y00 = ypos - crSize3.Height - 10 + 4;

int radius2 = 5; // 定义圆角的半径
GraphicsPath gp2 = new GraphicsPath();
gp2.AddArc(x00, y00, radius2, radius2, 180, 90);
gp2.AddArc(x00 + juxingWidth2, y00, radius2, radius2, 270, 90);
gp2.AddArc(x00 + juxingWidth2, y00 + juxingHeight2, radius2, radius2, 0, 90);
gp2.AddArc(x00, y00 + juxingHeight2, radius2, radius2, 90, 90);
gp2.CloseAllFigures();
grap.FillPath(brush4, gp2);

float x3 = xpos + 7;
ypos = ypos - crSize3.Height + 2;
grap.DrawString("打卡", font4, brush2, x3, ypos);
x3 = x3 + crSize33.Width;
grap.DrawString(time, font2, brush3, x3, ypos - 5);

ypos = ypos - 5;//行间距

//右下角——防伪
SizeF crSize4 = grap.MeasureString(securityCode, font3);//水印宽度
float x4 = ((float)width * (float).99) - crSize4.Width;
float y4 = ((float)height * (float).99) - crSize4.Height;
grap.DrawString(securityCode, font3, brush2, x4 - 1, y4 + 1);
grap.DrawString(securityCode, font3, brush, x4, y4);

y4 = y4 - 5;//行间距

//右下角——相机
SizeF crSize6 = grap.MeasureString("相机 真实时间", font5);//水印宽度
x4 = ((float)width * (float).99) - crSize6.Width;
y4 = y4 - crSize6.Height;
grap.DrawString("相机 真实时间", font5, brush2, x4 - 1, y4 + 1);
grap.DrawString("相机 真实时间", font5, brush, x4, y4);

y4 = y4 - 5;//行间距

//右下角——今日水印
SizeF crSize7 = grap.MeasureString("今日水印", font);//水印宽度
x4 = ((float)width * (float).99) - crSize7.Width;
y4 = y4 - crSize7.Height;
grap.DrawString("今日水印", font, brush2, x4 - 1, y4 + 1);
grap.DrawString("今日水印", font, brush, x4, y4);

grap.Dispose();

}
public string GetRandomString(int length)
{
byte[] b = new byte[4];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
Random r = new Random(BitConverter.ToInt32(b, 0));
string s = null;
string str = "0123456789";
str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < length; i++)
{
s += str.Substring(r.Next(0, str.Length - 1), 1);
}
return s;
}

 


posted @ 2024-03-01 14:01  绽放的菖蒲  阅读(159)  评论(0编辑  收藏  举报