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

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

思路是:先生成一张文字水印图片的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. linux系统安装硬盘分区建议

      一.常见挂载点的情况说明一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区),其中,/ 是必须的: 详细内容见下文: 安装系统时选择creat custom ...

  2. 黑马vue---46、vue使用过渡类名实现动画

    黑马vue---46.vue使用过渡类名实现动画 一.总结 一句话总结: vue动画的过渡类名的时间点中没有设置样式的话就是默认的样式 使用 transition 元素,把 需要被动画控制的元素,包裹 ...

  3. ExtractIcon function Retrieves a handle to an icon from the specified executable file, DLL, or icon file.

    https://msdn.microsoft.com/zh-cn/vstudio/ms648068(v=vs.90)

  4. 启动elasticsearch的时候报出Exception in thread "main" SettingsException[Failed to load settings from /usr/local/elasticsearch/config/elasticsearch.yml]; nested: MarkedYAMLException[while scanning a simple ke

    故障现象: [elasticsearch@tiantianml- ~]$ /usr/local/elasticsearch/bin/elasticsearch Exception in thread ...

  5. python实现并发服务器实现方式(多线程/多进程/select/epoll)

    python实现并发服务器实现方式(多线程/多进程/select/epoll)   并发服务器开发 并发服务器开发,使得一个服务器可以近乎同一时刻为多个客户端提供服务.实现并发的方式有多种,下面以多进 ...

  6. Java Web之过滤器(Filter)

    转: Java Web之过滤器(Filter) 2018年07月31日 16:58:40 喻志强 阅读数 13705更多 所属专栏: Java Web入门   版权声明:本文为博主原创文章, 转载请注 ...

  7. linux简单命令3---帮助命令

    1:帮助命令:man 命令: 2:这个帮助用的比较多(还是中文):命令  --help 3:shell帮助 4:详细命令(比man更详细)帮助,用的少,比较麻烦:info

  8. [C++]数据结构:线性表之(单)链表

    一 (单)链表 ADT + Status InitList(LinkList &L) 初始化(单)链表 + void printList(LinkList L) 遍历(单)链表 + int L ...

  9. react中 如何异步展示后台接口的提示消息

    调用接口后,后台会返回这样的一段信息提示:{"errCode":400002,"errMsg":"字段校验异常","data&qu ...

  10. 使用STM32F103ZET霸道主板实现SD卡的读写(非文件系统)

    了解STM32F103ZET是高容量多管脚的芯片 了解SD读写线路图 了解SD的基地址 阅读STM32F10xx英文参考 SDIO那章,我们编写代码边看文档解析 建工程,打开包含所有包括外设库函数的样 ...