首页 > 其他分享 >模拟水印相机

模拟水印相机

时间:2024-03-01 14:14:43浏览次数:17  
标签:xpos grap float ypos 水印 Height 相机 new 模拟

上效果图

 

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

 


标签:xpos,grap,float,ypos,水印,Height,相机,new,模拟
From: https://www.cnblogs.com/zfdcp-028/p/18046911

相关文章

  • Unity 贴图叠加,添加水印
    原创内容,转载请标明出处Shader"Custom/AddWatermarkShader"{Properties{_MainTex("MainTexture",2D)="white"{}_WatermarkTex("WatermarkTexture",2D)="white"{}_WatermarkPosit......
  • Tiktok api接口 获取视频列表、用户详情,视频无水印数据采集
    iDataRiver平台https://www.idatariver.com/zh-cn/提供开箱即用的Tiktok数据采集API,供用户按需调用。接口使用详情请参考Tiktok接口文档接口列表1.获取用户详情参数类型是否必填默认值示例值描述apikeystring是idr_***从控制台里复制apikeyuser_idnu......
  • P10202 [湖北省选模拟 2024] 沉玉谷 Solution
    好像比题解劣一个\(n\),但是也跑的很快。首先说明,问题等价于计算有多少种本质不同的方案使得整个序列被删完,证明省略。考虑用区间的方式表述这些操作,具体的,忽略删除后的移位操作,将每次删除的左右段点视为一个区间,则一定会有:区间的并是\([1,n]\)。区间之间要么不交,要么包含。......
  • 根据CXP XML文件配置相机
     1    说明GenICam定义了相机需要实施一个xml文件用于描述相机的功能、寄存器等详细信息,有了这些信息,host才能利用这些信息完成对相机的配置。 对于CoaxPress接口的Device相机而言,xml信息是如何存储的呢?Xml一般情形下都会存在bootstrap寄存器的用户区间内。1.1......
  • VisionPro相机掉线问题
    最近有一个项目用到visionpro,遇到一个问题记录一下。就是相机频繁掉线。报错信息:在网上查找原因,关闭防火墙、设置巨帧模式、调大接收缓存区都试过,没有改善。因为其他原因,我们中途换了海康相机。但是两款相机都有掉线的问题。所以排除相机的因素。 并且这个项目我们有两台......
  • 2/21 和 2/22 模拟赛总结
    2/21T1排序题意将$4n$个数分成$n$组,要求对于每组中的四个数$a,b,c,d$,求$\max\sum\lvertab-cd\rvert$。$n\le10^5$,$0\lea_i\le10^7$。解析找规律题,评红。将所有数从小到大排序,从中分成两半。小的一半“彩虹桥”式两两配对,大的一半大大配对、小小配对,即是最优答案。......
  • 人工智能水印技术入门:工具与技巧
    近几个月来,我们看到了多起关于“深度伪造(deepfakes)”或人工智能生成内容的新闻报道:从泰勒·斯威夫特的图片、汤姆·汉克斯的视频到美国总统乔·拜登的录音。这些深度伪造内容被用于各种目的,如销售产品、未经授权操纵人物形象、钓鱼获取私人信息,甚至制作误导选民的虚假资料,它......
  • 模拟ftp服务器
    client.cintget_cmd_type(char*cmd){//比较输入的指令,找到对应的就返回相对应的指令。if(!strcmp("ls",cmd))returnLS;if(!strcmp("lls",cmd))returnLLS;if(!strcmp("pwd",cmd))returnPWD;if(!strcmp("......
  • NFLS 省选模拟 过路费
    前言这道题正向思考是比较难以想出来的,蕴含了一类解题的思路,同时也可以当作一类板子题记忆。题面Link给定一个有向图,求\(s\)到\(t\)的最短路径。特殊点在于,对于一条路径,如果经过的边数小于等于\(k\),那么该路径总长度为构成该路径的所有边的长度之和;否则为该路径上最长的......
  • 模拟量系数计算方法(y = ax + b)
    客户测量范围是M—N(M < N),采集数据范围X—Y(X < Y)计算公式:y = ax + bM = Xa + b N = Ya + b 得:N - M = (Y - X)a 进一步可得:a = (N - M)/ (Y - X)利用二元一次方程可得 b 的值 测量水的深为例:(1)水的测量范围为0-10米,(2)模拟量转485为低精度(4-20mA,数据采......