生成透明带水印的图片

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

本段代码只支持水印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;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值