在合同中需要每一页都加上水印,我们生成一张透明带水印的图片,把这张透明带水印的图片放到报表中来实现,

本段代码只支持水印45度倾斜。

 public class DrawWaterText{public DrawWaterText(){}private readonly int intAngle = -45;/// <summary>/// /// </summary>/// <param name="strCaption">标题名称</param>/// <param name="strNo">合同号</param>/// <param name="intCaptionSize">标题名称字体大小</param>/// <param name="intNoSize">合同号字体大小</param>/// <param name="fontCaptionName">标题名字字体名称</param>/// <param name="fontNoName">合同号字体名称</param>/// <param name="intWidth">生成图片的宽度</param>/// <param name="intHeight">生成图片的宽度</param>/// <param name="opcity">透明度</param>/// <param name="intDis">合同名称和合同号之间的距离</param>/// <returns></returns>public Image Draw(string strCaption, string strNo, int intCaptionSize = 120, int intNoSize = 60, string fontCaptionName = "黑体", string fontNoName = "黑体",int intWidth = 1100, int intHeight = 1600, int opcity = 10, int intDis = 10){Rectangle rotateRect = GetRotateRectangle(intWidth, intHeight, intAngle);int rotaeEdge = rotateRect.Width;Bitmap bitmap = new Bitmap(rotaeEdge, rotaeEdge);Graphics graphics = Graphics.FromImage(bitmap);Font fontState = new Font(fontCaptionName, intCaptionSize, GraphicsUnit.Pixel);SizeF sfState = graphics.MeasureString(strCaption, fontState);Font fontNo = new Font(fontNoName, intNoSize, GraphicsUnit.Pixel);SizeF sfNo = graphics.MeasureString(strNo, fontNo);Rectangle rotateRectFont;if (sfState.Width > sfNo.Width){rotateRectFont = GetRotateRectangle((int)sfState.Width, (int)sfState.Height, 0);}else{rotateRectFont = GetRotateRectangle((int)sfNo.Width, (int)sfNo.Height, 0);}int x = 0;int y = 0;int m = (int)Math.Floor((double)rotaeEdge / rotateRectFont.Width);int n = (int)Math.Floor((double)rotaeEdge / rotateRectFont.Height);for (int i = 0; i < m; i++){x = i * rotateRectFont.Width;for (int j = 0; j < n; j++){y = j * rotateRectFont.Width;graphics.DrawString(strCaption, fontState, new SolidBrush(Color.LightGray), x, y);graphics.DrawString(strNo, fontNo, new SolidBrush(Color.LightGray), x + 10, y + sfState.Height + intDis);}}graphics.RotateTransform(intAngle);graphics.Save();graphics.Dispose();bitmap.MakeTransparent(Color.Transparent);Image image = GetRotateImage(bitmap, intAngle);Point leftPnt = GetLeftPoint(image.Width, image.Height, intWidth, intHeight);Bitmap bmpa4 = new Bitmap(intWidth, intHeight);graphics = Graphics.FromImage(bmpa4);ImageAttributes imageAtt = GetAlphaImgAttr(opcity);graphics.DrawImage(image, new Rectangle(0, 0, intWidth, intHeight), leftPnt.X, leftPnt.Y, intWidth, intHeight, GraphicsUnit.Pixel, imageAtt);bmpa4.MakeTransparent(Color.Transparent);return bmpa4 as Image;}private Point GetLeftPoint(int srcWidth, int srcHeight, int desWidth, int desHeight){int x = srcWidth / 2 - desWidth / 2;int y = srcHeight / 2 - desHeight / 2;Point pnt = new Point(x, y);return pnt;}private Image GetRotateImage(Image srcImage, int angle){angle = angle % 360;//原图的宽和高int srcWidth = srcImage.Width;int srcHeight = srcImage.Height;//图像旋转之后所占区域宽和高Rectangle rotateRec = GetRotateRectangle(srcWidth, srcHeight, angle);int rotateWidth = rotateRec.Width;int rotateHeight = rotateRec.Height;//目标位图Bitmap destImage = null;Graphics graphics = null;try{//定义画布,宽高为图像旋转后的宽高destImage = new Bitmap(rotateWidth, rotateHeight);//graphics根据destImage创建,因此其原点此时在destImage左上角graphics = Graphics.FromImage(destImage);//要让graphics围绕某矩形中心点旋转N度,分三步//第一步,将graphics坐标原点移到矩形中心点,假设其中点坐标(x,y)//第二步,graphics旋转相应的角度(沿当前原点)//第三步,移回(-x,-y)//获取画布中心点Point centerPoint = new Point(rotateWidth / 2, rotateHeight / 2);//将graphics坐标原点移到中心点graphics.TranslateTransform(centerPoint.X, centerPoint.Y);//graphics旋转相应的角度(绕当前原点)graphics.RotateTransform(angle);//恢复graphics在水平和垂直方向的平移(沿当前原点)graphics.TranslateTransform(-centerPoint.X, -centerPoint.Y);//此时已经完成了graphics的旋转//计算:如果要将源图像画到画布上且中心与画布中心重合,需要的偏移量Point Offset = new Point((rotateWidth - srcWidth) / 2, (rotateHeight - srcHeight) / 2);//将源图片画到rect里(rotateRec的中心)graphics.DrawImage(srcImage, new Rectangle(Offset.X, Offset.Y, srcWidth, srcHeight));//重至绘图的所有变换graphics.ResetTransform();graphics.Save();}catch (Exception ex){throw ex;}finally{if (graphics != null)graphics.Dispose();}return destImage;}private Rectangle GetRotateRectangle(int width, int height, float angle){double radian = angle * Math.PI / 180; ; double cos = Math.Cos(radian);double sin = Math.Sin(radian);//只需要考虑到第四象限和第三象限的情况取大值(中间用绝对值就可以包括第一和第二象限)int newWidth = (int)(Math.Max(Math.Abs(width * cos - height * sin), Math.Abs(width * cos + height * sin)));int newHeight = (int)(Math.Max(Math.Abs(width * sin - height * cos), Math.Abs(width * sin + height * cos)));return new Rectangle(0, 0, newWidth, newHeight);}private ImageAttributes GetAlphaImgAttr(int opcity){if (opcity < 0 || opcity > 100){throw new ArgumentOutOfRangeException("opcity 值为 0~100");} //颜色矩阵float[][] matrixItems = { new float[]{1,0,0,0,0}, new float[]{0,1,0,0,0}, new float[]{0,0,1,0,0}, new float[]{0,0,0,(float)opcity / 100,0},new float[]{0,0,0,0,1} };ColorMatrix colorMatrix = new ColorMatrix(matrixItems);ImageAttributes imageAtt = new ImageAttributes();imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);return imageAtt;}}

生成透明带水印的图片相关推荐

  1. Android如何做出带有复杂水印的图片

    最近项目中存在图片加水印效果的需求,具体效果如下: 然后做出来的效果如下: 原图 水印图 点击可以查看大图:大图 那么针对这种比较复杂的水印图片,应该如何去做呢?下面我分享一下自己的思路. 如果没有使 ...

  2. 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图

    本文借助vs2005中自带的FileUpload控件实现图片文件的上传并生成缩略图. 实现过程:选择图片上传成功后,取得已经存在服务器的文件生成缩略图,并且判断是否是图片类型的文件,这个的判断可以在程 ...

  3. asp.net生成缩略图、文字图片水印

    1 /// <summary> 2 /// 会产生graphics异常的PixelFormat 3 /// </summary> 4 private static PixelF ...

  4. php 缩略图增加水印,php 图片上传代码(具有生成缩略图与增加水印功能)

    class upfile { public $filepath = "www.111com.net/"; //上传文件存放文件夹 public $filesize = 100000 ...

  5. php 上传加水印,PHP 图片上传加水印实例

    function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$t ...

  6. ThinkPHP中文水印和图片水印结合

    相对于PHP刚入门的菜鸟们对于图片和水印的结合是一个很苦恼的事情,有一些人的蠢办法是在图片上下手,利用图片上用ps加上自己想要的水印.那样的话,你将是一个合格的Ul并不是一个合格的程序员了!话到这里, ...

  7. C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)

    对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一.在PPT文档中同样也可以设置水印,包括文本水印和图片水印,本文将讲述如何通过Spire.Presentation for .NET来对P ...

  8. python给图片加半透明水印_图片添加半透明文字水印 Python

    效果 python marker.py -f ./input/test.png -m '添加水印' 功能 使用知名python图片处理库PIL,为图片添加文字水印. 实现 步骤:根据文字生成水印图片: ...

  9. java gif图片_java 如何生成动态的gif图片

    网上找了很多资料都没有生成gif图片的例子.但是生成水印的文件到不少,如果我们把gif图片合成水印后,图片就不动了,所以我写了个小例子供大家参考.(如果你想在某个图片上打印个动感图标,这个类就能很好地 ...

  10. python朋友圈教程_Python 生成你的朋友圈九宫格图片

    关于微信之前写过以下文章,有兴趣可以点击查看: 如何导出你的微信语音 c 盘空间又满了?微信清理神器帮你释放空间 微信撤回的消息也能看到! 如何备份可能被删的公众号文章和网页 如何在电脑上登陆多个微信 ...

最新文章

  1. 计算机基础2多媒体,《计算机基础》第2章-多媒体技术.pptx
  2. CodeArt SharePoint Permission Extension 1.0 beta publish
  3. LeetCode Power of Three
  4. linux 可执行文件权限不够,root执行/media可执行文件权限不够,chmod修改权限无效...
  5. Py之distance:distance的简介、安装、使用方法之详细攻略
  6. POJ1655 Balancing Act
  7. HDU - 6746 Civilization(贪心+模拟)
  8. zabbix自定义key监控mysql主从同步超简单!
  9. 数据库知识:SQLServer变量相关知识介绍
  10. javascriptjquery 判断滚动到页面底部
  11. Java 内存泄露总结
  12. Java数据库篇7——数据库设计
  13. 热乎的宇宙条总部面经,已拿offer,速来围观
  14. Spring Boot 学习(一) 快速搭建SpringBoot 项目
  15. 在github网页版里新建文件夹,放置很多子文件:添加斜杠自动生成文件夹
  16. 设计窗口模拟教室座位表java,基于web的考研自习教室座位管理(完整源码+论文全套+教学视频)...
  17. 有个空间,名叫 Gamma
  18. 【华为机试】HJ16 购物单
  19. HashMap 与 TreeMap
  20. 内存、主存、外存、主存储器、外存储器等之间的区别

热门文章

  1. 中国区块链专利申请数破万:阿里巴巴居首位 网心科技晋身前十
  2. 5G基本原理/5G NR的关键技术
  3. linux服务器密码策略设置:登录密码错误次数限制
  4. 一款比较简单的PDF解密工具注册码分析
  5. MD5加密算法原理及实现
  6. 怎么用PS替换图片背景色?这个方法你需要知道
  7. 计算机学院三下乡,重庆理工大学计算机学院”三下乡“教师情牵故乡
  8. Java 面试真题 【继承静态代码块执行时机】
  9. 怎样让Windows10系统的时间显示到秒——且可手动修改系统的时间
  10. Ignite SQL网格