之前一篇给图片加水印的功能,加出来水印的图片位置有一点问题,并且如果图片分辨率有变动的话,水印会有层次不齐的问题。

目前只能优化到增加一条居中显示的斜水印,在不同分辨率不同大小的图片中,都能保证文字水印的字体大小从左下至右上能撑满整张图片。

思路是:先生成一张文字水印图片的PNG图片。

在你需要添加水印的图片上,把之前加的水印图片贴上去就可以了。

核心代码:

     //新建原始普通大小的bmp
Bitmap bmCanvas = new Bitmap(imgSrc.Width, imgSrc.Height, PixelFormat.Format24bppRgb);
Graphics gCanvas = Graphics.FromImage(bmCanvas);
gCanvas.Clear(Color.White);
gCanvas.SmoothingMode = SmoothingMode.HighQuality;
gCanvas.InterpolationMode = InterpolationMode.High;
//将原始图片加载入画布
gCanvas.DrawImage(imgSrc, , , imgSrc.Width, imgSrc.Height);
//计算图片对角线长度
double diagonal = Math.Sqrt(Math.Pow(width, ) + Math.Pow(height, )); //计算对角线倾角
double angle = Math.Asin(height / Math.Sqrt(Math.Pow(width, ) + Math.Pow(height, ))) / Math.PI * ; // 确定水印文字的字体大小
int[] sizes = new int[]
{
, , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , ,
};
Font crFont = null;
SizeF crSize = new SizeF(); for (int i = ; i < sizes.Length; i++)
{
crFont = new Font("微软雅黑", sizes[i], FontStyle.Bold);
crSize = gCanvas.MeasureString(watermarkText, crFont);
if ((int)crSize.Width < (int)diagonal * 0.9)
{
break;
}
}
// 生成水印图片(将文字写到图片中)
//Bitmap bmWaterMark = new Bitmap((int)crSize.Width + 3, (int)crSize.Height + 3, PixelFormat.Format32bppArgb);
Bitmap bmWaterMark = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics gWaterMark = Graphics.FromImage(bmWaterMark); gWaterMark.TranslateTransform(width / , height / );
gWaterMark.RotateTransform(-(int)angle);
gWaterMark.TranslateTransform(-crSize.Width / , -crSize.Height / ); PointF pt = new PointF(, );
// 画阴影文字
Brush transparentBrush0 = new SolidBrush(Color.FromArgb(, Color.Black));
Brush transparentBrush1 = new SolidBrush(Color.FromArgb(, Color.Black));
gWaterMark.DrawString(watermarkText, crFont, transparentBrush0, pt.X, pt.Y + );
gWaterMark.DrawString(watermarkText, crFont, transparentBrush0, pt.X + , pt.Y);
gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X + , pt.Y + );
gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X, pt.Y + );
gWaterMark.DrawString(watermarkText, crFont, transparentBrush1, pt.X + , pt.Y);
transparentBrush0.Dispose();
transparentBrush1.Dispose(); // 画文字
gWaterMark.SmoothingMode = SmoothingMode.HighQuality;
//Brush SolidBrush3 = new SolidBrush(Color.White);
Brush solidBrush3 = new SolidBrush(Color.FromArgb(, Color.White));
gWaterMark.DrawString(watermarkText, crFont, solidBrush3, pt.X, pt.Y, StringFormat.GenericDefault);
solidBrush3.Dispose(); // 保存刚才的操作
gWaterMark.Save();
gWaterMark.Dispose();
bmWaterMark.Save(_wmImgSavePath, ImageFormat.Jpeg); //// 将水印图片加到原图中
//AddWatermarkImage(gCanvas, new Bitmap(bmWaterMark), "WM_TOP_LEFT", width, height); using (var imageAttr = new ImageAttributes())
{
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(, , , );
colorMap.NewColor = Color.FromArgb(, , , );
ColorMap[] remapTable = { colorMap };
imageAttr.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements =
{
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gCanvas.DrawImage(bmWaterMark, new Rectangle(, , bmWaterMark.Width, bmWaterMark.Height), , ,
bmWaterMark.Width, bmWaterMark.Height, GraphicsUnit.Pixel, imageAttr);
gCanvas.Dispose();
}
bmWaterMark.Dispose();

代码已上传至GitHub,地址:https://github.com/hano7758/WaterMark

c# 图文添加文字斜水印 优化的更多相关文章

  1. c# 图文添加文字斜水印

    项目中有个添加水印的需求,需要给图片铺满斜水印. 网上搜了半天全是添加在图片上.下.左.右的案例. 于是按照网上的某一段案例自己修改了一些代码. Bitmap bitmap = new Bitmap( ...

  2. ios图片添加文字或者水印

    在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...

  3. php 使用GD库压缩图片,添加文字图片水印

    先上一个工具类,提供了压缩,添加文字.图片水印等方法: image.class.php <?php class Image { private $info; private $image; pu ...

  4. php 图片添加文字,水印

    因为工作需求,用到这个,网上找了很多,也没有找到好的方式,最后找到这种感觉比较简单的方式,记录下来,以备后用.   $im = imagecreatefrompng("img/yyk_bg. ...

  5. 用python给图片添加文字(水印)

    题目来源于:Python 练习册,每天一个小程序 第0000题 代码如下: #-*- coding:utf-8 -*- import PIL from PIL import Image from PI ...

  6. 给二维码(图片)添加文字(水印),让生成的二维码中间带logo

    <?php //生成二维码 require_once IA_ROOT . '/framework/library/qrcode/phpqrcode.php'; QRcode::png($url, ...

  7. javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作

    欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...

  8. php 图片添加文字水印 以及 图片合成(微信快码传播)

    1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...

  9. 利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

    1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image ...

随机推荐

  1. python 调用父类方法, 重写父类构造方法, 不显式调用,会报错

    子类不显式调用父类的构造方法,而父类构造函数初始化了一些属性,就会出现问题 如果子类和父类都有构造函数,子类其实是重写了父类的构造函数,如果不显式调用父类构造函数,父类的构造函数就不会被执行,导致子类 ...

  2. Eclipse的断点调试

    A:Debug的作用 调试程序 查看程序执行流程 B:如何查看程序执行流程 什么是断点: 就是一个标记,从哪里开始. 如何设置断点: 你想看哪里的程序,你就在那个有效程序的左边双击即可. 在哪里设置断 ...

  3. Qt编写数据可视化大屏界面电子看板9-曲线效果

    一.前言 为了编写数据可视化大屏界面电子看板系统,为了能够兼容Qt4和嵌入式linux系统,尤其是那种主频很低的,但是老板又需要在这种硬件上(比如树莓派.香橙派.全志H3.imx6)展示这么华丽的界面 ...

  4. k8s管理机密信息(9)

    一.启动应用安全信息的保护: Secret介绍: 应用启动过程中可能需要一些敏感信息,比如访问数据库的用户名密码或者秘钥.将这些信息直接保存在容器镜像中显然不妥,Kubernetes 提供的解决方案是 ...

  5. iOS 11适配

    1.http://www.cocoachina.com/ios/20170915/20580.html   简书App适配iOS 11   2.http://www.jianshu.com/p/efb ...

  6. [转]MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法

    原文链接:https://blog.csdn.net/qq_29672495/article/details/72668008

  7. 配置zabbix监控windows,cmd运行报错cannot connect to Service Manager: [0x00000005]

    错误原因: cmd运行没有管理员权限 解决: 找到cmd.exe的位置C:\Windows\System32,选中--右键--使用管理员身份运行 再通过cmd进入到zabbix_agentd文件夹进行 ...

  8. 01.03 vim编辑器使用

    ==========linux基础命令的使用==========================绝对路径:由根目录(/)开始写起的文件名或目录名称相对路径:相对于目前路径的文件名写法(开头不是/就属于 ...

  9. Python学习笔记——esle和with 语句

    1. else与while组合 def showMaxFactor(num): count = num // 2 while count > 1: if num % count == 0: pr ...

  10. get_object_var 返回一个数组

    语法:get_object_var($object),返回一个数组.获取$object对象中的属性,组成一个数组 实例: <?php class person{ public $name=&qu ...