您现在的位置是:首页 > 文章详情文章详情

C# 给图片、PDF 加文字水印

会飞的猪 2020-05-29 856人已围观

简介白码驿站是一个面向开发者的知识共享社区,专注于为开发者打造一个纯净的技术交流社区(源码下载,免费在线图片压缩,jquery插件,插件下载,JS/CSS在线压缩。)-白码驿站

        当工作中, 经常会将一些机密点的文档进行水印处理的,以下是对图片、PDF加文字水印处理的方法。


Demo下载https://pan.baidu.com/s/1c9ZfTWwnBOEx_SJr3ZKs9g

提取码:9wfg


图片水印

                                        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace Demo.Helper
{
    public class PicOperate
    {
        #region 调用示例
        /*
        调用示例:

                    //取的新图片文件的名称
                   string namestr = MakedownName();
                   newname = namestr + "." + objFile.ImageType;
                   objFile.SaveAsVirtual("/Function/UpLoadPic/Temp_Pic/" + newname);
                   string sourcepath = Server.MapPath("/Function/UpLoadPic/Temp_Pic/" + newname);
                   string smallimg = Server.MapPath("/Function/UpLoadPic/Temp_Pic/Small/" + namestr + ".jpg");
                   string bigimg = Server.MapPath("/Function/UpLoadPic/Temp_Pic/Big/" + namestr + ".jpg");
                   string waterlogo = Server.MapPath("/Function/UpLoadPic/sys_logo.gif");
                   //保存图片

                   //objFile.SaveAsVirtual(common.TmpProdImgVirtPath + newname);

                   //保存图片名称
                   if (Pic_type == "single")
                       Session["UploadPic"] = namestr + ".jpg";
                   else
                       Session["UploadPic" + Pic_type] = namestr + ".jpg";

                   //生成小缩略图
                   //WaterMarksmall(objFile.ImageWidth, objFile.ImageHeight);
                   ImgWater myimgwater = new ImgWater();
                   myimgwater.zzsResizeImg(sourcepath, smallimg, pic_width, pic_height, "white");
                   //添加水印效果   
            */
        #endregion

        /// <summary>
        /// 图片水印
        /// </summary>
        /// <param name="ImgFile">原图文件地址</param>
        /// <param name="WaterImg">水印图片地址</param>
        /// <param name="sImgPath">水印图片保存地址</param>
        /// <param name="Alpha">水印透明度设置</param>
        /// <param name="iScale">水印图片在原图上的显示比例</param>
        /// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
        public void zzsImgWater(string ImgFile, string WaterImg, string sImgPath, float Alpha, float iScale, int intDistance)
        {
            try
            {
                //装载图片
                FileStream fs = new FileStream(ImgFile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);

                //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);

                int imgPhotoWidth = imgPhoto.Width;
                int imgPhotoHeight = imgPhoto.Height;

                System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
                int imgWatermarkWidth = imgWatermark.Width;
                int imgWatermarkHeight = imgWatermark.Height;


                //计算水印图片尺寸

                decimal aScale = Convert.ToDecimal(iScale);
                decimal pScale = 0.05M;
                decimal MinScale = aScale - pScale;
                decimal MaxScale = aScale + pScale;

                int imgWatermarkWidthNew = imgWatermarkWidth;
                int imgWatermarkHeightNew = imgWatermarkHeight;

                if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
                    if (imgWatermarkWidth > imgWatermarkHeight)

                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                            imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
                        }
                    else
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                            imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
                        }
                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }

                if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
                    if (imgWatermarkWidth > imgWatermarkHeight)
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                            imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                        }
                    else
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                            imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                        }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }

                if (imgWatermarkHeightNew > imgWatermarkHeight || imgWatermarkWidthNew > imgWatermarkWidth)
                {
                    imgWatermarkHeightNew = imgWatermarkHeight;
                    imgWatermarkWidthNew = imgWatermarkWidth;

                }


                //将原图画出来

                Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);

                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gbmPhoto.Clear(Color.White);
                gbmPhoto.DrawImage(
                      imgPhoto
                    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
                    , 0
                    , 0
                    , imgPhotoWidth
                    , imgPhotoHeight
                    , GraphicsUnit.Pixel
                    );



                Bitmap bmWatermark = new Bitmap(bmPhoto);
                bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

                Graphics gWatermark = Graphics.FromImage(bmWatermark);

                //指定高质量显示水印图片质量
                gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


                System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();

                //设置两种颜色,达到合成效果

                System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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,  Alpha, 0.0f},
               new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
            };


                System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);

                imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);

                //确定水印边距
                int xPos = imgPhotoWidth - imgWatermarkWidthNew;
                int yPos = imgPhotoHeight - imgWatermarkHeightNew;
                int xPosOfWm = 0;
                int yPosOfWm = 0;

                if (xPos > intDistance)
                    xPosOfWm = xPos - intDistance;
                else
                    xPosOfWm = xPos;

                if (yPos > intDistance)
                    yPosOfWm = yPos - intDistance;
                else
                    yPosOfWm = yPos;

                gWatermark.DrawImage(
                      imgWatermark
                    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
                    , 0
                    , 0
                    , imgWatermarkWidth
                    , imgWatermarkHeight
                    , GraphicsUnit.Pixel
                    , imageAttributes
                    );

                imgPhoto = bmWatermark;



                //以jpg格式保存图片
                imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

                //销毁对象
                gbmPhoto.Dispose();
                gWatermark.Dispose();

                imgPhoto.Dispose();
                imgWatermark.Dispose();



            }
            catch
            {
            }
        }

        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="ImgFile">原图文件地址</param>
        /// <param name="TextFont">水印文字</param>
        /// <param name="sImgPath">文字水印图片保存地址</param>
        /// <param name="hScale">高度位置</param>
        /// <param name="widthFont">文字块在图片中所占宽度比例</param>
        /// <param name="Alpha">文字透明度 其数值的范围在0到255</param>
        public void zzsTextWater(string ImgFile, string TextFont, string sImgPath, float hScale, float widthFont, int Alpha)
        {
            try
            {

                FileStream fs = new FileStream(ImgFile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);



                //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);

                int imgPhotoWidth = imgPhoto.Width;
                int imgPhotoHeight = imgPhoto.Height;

                Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);

                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                gbmPhoto.DrawImage(
                      imgPhoto
                    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
                    , 0
                    , 0
                    , imgPhotoWidth
                    , imgPhotoHeight
                    , GraphicsUnit.Pixel
                    );

                //建立字体大小的数组,循环找出适合图片的水印字体

                int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
                System.Drawing.Font crFont = null;
                System.Drawing.SizeF crSize = new SizeF();
                for (int i = 0; i < 43; i++)
                {
                    crFont = new Font("arial", sizes[i], FontStyle.Bold);
                    crSize = gbmPhoto.MeasureString(TextFont, crFont);

                    if ((ushort)crSize.Width < (ushort)imgPhotoWidth * widthFont)
                        break;
                }

                //设置水印字体的位置
                int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
                float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
                float xCenterOfImg = (imgPhotoWidth * 1 / 2);

                //if (xCenterOfImg<crSize.Height)
                //    xCenterOfImg = (imgPhotoWidth * (1 - (crSize.Height)/ imgPhotoWidth));

                System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
                StrFormat.Alignment = System.Drawing.StringAlignment.Center;

                //
                System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 0, 0, 0));

                gbmPhoto.DrawString(
                      TextFont
                    , crFont
                    , semiTransBrush2
                    , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
                    , StrFormat
                    );

                System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 255, 255, 255));

                gbmPhoto.DrawString(
                      TextFont
                    , crFont
                    , semiTransBrush
                    , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
                    , StrFormat
                    );

                bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

                gbmPhoto.Dispose();
                imgPhoto.Dispose();
                bmPhoto.Dispose();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 文字和Logo图片水印
        /// </summary>
        /// <param name="ImgFile">原图文件地址</param>
        /// <param name="WaterImg">水印图片地址</param>
        /// <param name="TextFont">水印文字信息</param>
        /// <param name="sImgPath">生存水印图片后的保存地址</param>
        /// <param name="ImgAlpha">水印图片的透明度</param>
        /// <param name="imgiScale">水印图片在原图上的显示比例</param>
        /// <param name="intimgDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
        /// <param name="texthScale">水印文字高度位置,从图片底部开始计算,0-1</param>
        /// <param name="textwidthFont">文字块在图片中所占宽度比例 0-1</param>
        /// <param name="textAlpha">文字透明度 其数值的范围在0到255</param>
        public void zzsImgTextWater(string ImgFile, string WaterImg, string TextFont, string sImgPath, float ImgAlpha, float imgiScale, int intimgDistance, float texthScale, float textwidthFont, int textAlpha)
        {
            try
            {

                FileStream fs = new FileStream(ImgFile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);



                //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);


                int imgPhotoWidth = imgPhoto.Width;
                int imgPhotoHeight = imgPhoto.Height;

                Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);

                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                gbmPhoto.DrawImage(
                      imgPhoto
                    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
                    , 0
                    , 0
                    , imgPhotoWidth
                    , imgPhotoHeight
                    , GraphicsUnit.Pixel
                    );


                //建立字体大小的数组,循环找出适合图片的水印字体

                int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
                System.Drawing.Font crFont = null;
                System.Drawing.SizeF crSize = new SizeF();
                for (int i = 0; i < 43; i++)
                {
                    crFont = new Font("arial", sizes[i], FontStyle.Bold);
                    crSize = gbmPhoto.MeasureString(TextFont, crFont);

                    if ((ushort)crSize.Width < (ushort)imgPhotoWidth * textwidthFont)
                        break;
                }

                //设置水印字体的位置
                int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
                float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - (crSize.Height / 2));
                float xCenterOfImg = (imgPhotoWidth * 1 / 2);

                System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
                StrFormat.Alignment = System.Drawing.StringAlignment.Center;

                //
                System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 0, 0, 0));

                gbmPhoto.DrawString(
                      TextFont
                    , crFont
                    , semiTransBrush2
                    , new System.Drawing.PointF(xCenterOfImg + 1, yPosFromBottom + 1)
                    , StrFormat
                    );

                System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(textAlpha, 255, 255, 255));

                gbmPhoto.DrawString(
                      TextFont
                    , crFont
                    , semiTransBrush
                    , new System.Drawing.PointF(xCenterOfImg, yPosFromBottom)
                    , StrFormat
                    );


                System.Drawing.Image imgWatermark = new Bitmap(WaterImg);
                int imgWatermarkWidth = imgWatermark.Width;
                int imgWatermarkHeight = imgWatermark.Height;


                //计算水印图片尺寸

                decimal aScale = Convert.ToDecimal(imgiScale);
                decimal pScale = 0.05M;
                decimal MinScale = aScale - pScale;
                decimal MaxScale = aScale + pScale;

                int imgWatermarkWidthNew = imgWatermarkWidth;
                int imgWatermarkHeightNew = imgWatermarkHeight;

                if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
                    if (imgWatermarkWidth > imgWatermarkHeight)

                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                            imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
                        }
                    else
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                            imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
                        }
                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }

                if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
                    if (imgWatermarkWidth > imgWatermarkHeight)
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                            imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                        }
                    else
                        if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7) <= MaxScale))
                        {

                        }
                        else
                        {
                            imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                            imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                        }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }

                if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
                {
                    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
                    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                }

                if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
                {
                    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
                    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                }


                //将原图画出来


                Bitmap bmWatermark = new Bitmap(bmPhoto);
                bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

                Graphics gWatermark = Graphics.FromImage(bmWatermark);

                //指定高质量显示水印图片质量
                gWatermark.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gWatermark.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


                System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();

                //设置两种颜色,达到合成效果

                System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.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,  ImgAlpha, 0.0f},
                   new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
                };


                System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);

                imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);

                //确定水印边距
                int xPos = imgPhotoWidth - imgWatermarkWidthNew;
                int yPos = imgPhotoHeight - imgWatermarkHeightNew;
                int xPosOfWm = 0;
                int yPosOfWm = 0;

                if (xPos > intimgDistance)
                    xPosOfWm = xPos - intimgDistance;
                else
                    xPosOfWm = xPos;

                if (yPos > intimgDistance)
                    yPosOfWm = yPos - intimgDistance;
                else
                    yPosOfWm = yPos;

                gWatermark.DrawImage(
                      imgWatermark
                    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
                    , 0
                    , 0
                    , imgWatermarkWidth
                    , imgWatermarkHeight
                    , GraphicsUnit.Pixel
                    , imageAttributes
                    );

                imgPhoto = bmWatermark;

                //以jpg格式保存图片
                imgPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

                //销毁对象
                gbmPhoto.Dispose();
                gWatermark.Dispose();
                bmPhoto.Dispose();
                imgPhoto.Dispose();
                imgWatermark.Dispose();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 缩略图
        /// </summary>
        /// <param name="ImgFile">原图文件地址</param>
        /// <param name="sImgPath">缩略图保存地址</param>
        /// <param name="ResizeWidth">缩略图宽度</param>
        /// <param name="ResizeHeight">缩略图高度</param>
        /// <param name="BgColor">缩略图背景颜色,注意,背景颜色只能指定KnownColor中的值,如blue,red,green等</param>
        public void zzsResizeImg(string ImgFile, string sImgPath, int ResizeWidth, int ResizeHeight, string BgColor)
        {
            try
            {
                FileStream fs = new FileStream(ImgFile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);



                //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);


                int imgPhotoWidth = imgPhoto.Width;
                int imgPhotoHeight = imgPhoto.Height;
                int startX = 0;
                int StartY = 0;
                int NewWidth = 0;
                int NewHeight = 0;

                if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
                {
                    NewWidth = ResizeWidth;
                    NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
                    startX = 0;
                    StartY = (ResizeHeight - NewHeight) / 2;
                }

                if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
                {
                    NewHeight = ResizeHeight;
                    NewWidth = Convert.ToInt32(imgPhotoWidth * Math.Round(Convert.ToDecimal(ResizeHeight) / Convert.ToDecimal(imgPhotoHeight), 10));
                    startX = (ResizeWidth - NewWidth) / 2;
                    StartY = 0;
                }

                if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
                {
                    NewWidth = ResizeWidth;
                    NewHeight = Convert.ToInt32(imgPhotoHeight * Math.Round(Convert.ToDecimal(ResizeWidth) / Convert.ToDecimal(imgPhotoWidth), 10));
                    startX = 0;
                    StartY = (ResizeHeight - NewHeight) / 2;
                }

                if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
                {
                    NewWidth = imgPhotoWidth;
                    NewHeight = imgPhotoHeight;
                    startX = (ResizeWidth - imgPhotoWidth) / 2;
                    StartY = (ResizeHeight - imgPhotoHeight) / 2;
                }

                //计算缩放图片尺寸



                Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
                gbmPhoto.Clear(Color.FromName(BgColor));
                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                gbmPhoto.DrawImage(
                      imgPhoto
                    , new Rectangle(startX, StartY, NewWidth, NewHeight)
                    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
                    , GraphicsUnit.Pixel
                    );
                bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                imgPhoto.Dispose();
                gbmPhoto.Dispose();
                bmPhoto.Dispose();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 图片剪切
        /// </summary>
        /// <param name="ImgFile">原图文件地址</param>
        /// <param name="sImgPath">缩略图保存地址</param>
        /// <param name="PointX">剪切起始点 X坐标</param>
        /// <param name="PointY">剪切起始点 Y坐标</param>
        /// <param name="CutWidth">剪切宽度</param>
        /// <param name="CutHeight">剪切高度</param>
        public void zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
        {
            try
            {
                FileStream fs = new FileStream(ImgFile, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);

                //System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);
                //此处只能用filestream,用 System.Drawing.Image则会报多过进程访问文件的错误,会锁定文件
                Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                gbmPhoto.DrawImage(
                      imgPhoto
                    , new Rectangle(0, 0, CutWidth, CutHeight)
                    , new Rectangle(PointX, PointY, CutHeight, CutHeight)
                    , GraphicsUnit.Pixel
                    );
                bmPhoto.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                imgPhoto.Dispose();
                gbmPhoto.Dispose();
                bmPhoto.Dispose();
            }
            catch
            {
            }
        }
    }
}
                                


PDF水印

pdf水印需要引用iTextSharp.text.pdf 插件, 插件下载在Demo中

                                        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

namespace Demo.Helper
{
    /// <summary>
    /// pdf操作
    /// </summary>
    public class PdfOperate
    {
        /// <summary> 
        ///  PDF加水印 
        /// </summary> 
        /// <param name="inputfilepath">源PDF文件</param> 
        /// <param name="outputfilepath">加水印后PDF文件 </param> 
        /// <param name="ModelPicName">水印文件路径</param> 
        /// <param name="top">离顶部距离</param> 
        /// <param name="left">离左边距离,如果为负,则为离右边距离</param> 
        /// <param name="strMsg">返回信息</param> 
        /// <returns>返回</returns> 
        public bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left, ref string strMsg)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            try
            {
                pdfReader = new PdfReader(inputfilepath);

                int numberOfPages = pdfReader.NumberOfPages;

                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);

                float width = psize.Width;

                float height = psize.Height;

                pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));

                PdfContentByte waterMarkContent;

                /*********图片水印开始************************************************/
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);

                image.GrayFill = 20;//透明度,灰色填充
                //image.Rotation//旋转
                //image.RotationDegrees//旋转角度
                //水印的位置 
                if (left < 0)
                {
                    left = width - image.Width + left;
                }

                image.SetAbsolutePosition(left, (height - image.Height) - top);


                //每一页加水印,也可以设置某一页加水印 
                for (int i = 1; i <= numberOfPages; i++)
                {
                    waterMarkContent = pdfStamper.GetUnderContent(i);

                    waterMarkContent.AddImage(image);
                }
                /*********图片水印结束************************************************/
                strMsg = "success";
                return true;
            }
            catch (Exception ex)
            {
                strMsg = ex.Message.Trim();
                return false;
            }
            finally
            {

                if (pdfStamper != null)
                    pdfStamper.Close();

                if (pdfReader != null)
                    pdfReader.Close();
            }
        }

        /// <summary> 
        ///  PDF加水印 
        /// </summary> 
        /// <param name="inputfilepath">源PDF文件</param> 
        /// <param name="outputfilepath">加水印后PDF文件 </param> 
        /// <param name="WatermarkText">水印文本内容</param> 
        /// <param name="top">离顶部距离</param> 
        /// <param name="left">离左边距离,如果为负,则为离右边距离</param> 
        /// <param name="strMsg">返回信息</param> 
        /// <returns>返回</returns> 
        public bool PDFWatermarkText(string inputfilepath, string outputfilepath, string WatermarkText, float top, float left, ref string strMsg)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            try
            {
                pdfReader = new PdfReader(inputfilepath);

                int numberOfPages = pdfReader.NumberOfPages;

                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);

                float width = psize.Width;

                float height = psize.Height;

                pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));

                PdfContentByte waterMarkContent;


                BaseFont font = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体
                //每一页加水印,也可以设置某一页加水印 
                for (int i = 1; i <= numberOfPages; i++)
                {
                    waterMarkContent = pdfStamper.GetOverContent(i);
                    waterMarkContent.BeginText();
                    waterMarkContent.SetColorFill(new BaseColor(214, 214, 214));
                    waterMarkContent.SetFontAndSize(font, 80);
                    //waterMarkContent.MoveText(5f, 5f);
                    //float ta = 1f, tb = 0f, tc = 0f, td = 1f, tx = 0f, ty = 0f;
                    //ta = ta + 0.15f;
                    //td = td + 0.05f;
                    //ty = ty - 0.15f;

                    //tc = tc + 0.7f;
                    //waterMarkContent.SetTextMatrix(ta, tb, tc, td, tx, ty);

                    ////waterMarkContent
                    //waterMarkContent.SetTextRise(400);
                    //waterMarkContent.ShowText(WatermarkText);
                    //// 添加水印文字
                    //waterMarkContent.EndText();
                    int j = WatermarkText.Length;
                    string c = "";
                    int rise = 0;
                    int rise2 = 0;
                    int rise3 = 0;
                    int rise4 = 0;
                    int rise5 = 0;
                    int rise6 = 0;
                    int rise7 = 0;
                    int rise8 = 0;
                    int rise9 = 0;
                    int rise10 = 0;
                    if (false)
                    {
                        waterMarkContent.SetTextMatrix(50, 120);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise += 20;
                        }
                    }
                    else
                    {
                        waterMarkContent.SetTextMatrix(80, 100);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise += 18;
                        }

                        waterMarkContent.SetTextMatrix(80, 500);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise2);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise2 += 18;
                        }

                        waterMarkContent.SetTextMatrix(80, 900);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise3);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise3 += 18;
                        }

                        waterMarkContent.SetTextMatrix(80, 1300);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise4);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise4 += 18;
                        }
                        waterMarkContent.SetTextMatrix(80, 1700);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise5);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise5 += 18;
                        }


                        waterMarkContent.SetTextMatrix(700, 10);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise6);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise6 += 18;
                        }

                        waterMarkContent.SetTextMatrix(700, 350);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise7);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise7 += 18;
                        }

                        waterMarkContent.SetTextMatrix(700, 700);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise8);

                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise8 += 18;
                        }

                        waterMarkContent.SetTextMatrix(700, 1050);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise9);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise9 += 18;
                        }
                        waterMarkContent.SetTextMatrix(700, 1400);
                        for (int k = 0; k < j; k++)
                        {
                            waterMarkContent.SetTextRise(rise10);
                            c = WatermarkText.CharAt(k);
                            waterMarkContent.ShowText(c + "");
                            rise10 += 18;
                        }

                    }
                    // 添加水印文字
                    waterMarkContent.EndText();
                }
                strMsg = "success";
                return true;
            }
            catch (Exception ex)
            {
                strMsg = ex.Message.Trim();
                return false;
            }
            finally
            {

                if (pdfStamper != null)
                    pdfStamper.Close();

                if (pdfReader != null)
                    pdfReader.Close();
            }
        }
    }

    public static class CharAtExtention
    {
        public static string CharAt(this string s, int index)
        {
            if ((index >= s.Length) || (index < 0))
                return "";
            return s.Substring(index, 1);
        }
    }
}
                                


调用

                                        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Helper;

namespace Demo
{
    class Program
    {
        /// <summary>
        /// 白码驿站 图片、PDF加水印
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region 图片加水印
            PicOperate img = new PicOperate();
            //源文件地址
            string soureFile = AppDomain.CurrentDomain.BaseDirectory.ToString()+"File/img.png";
            //保存地址
            string savePath = AppDomain.CurrentDomain.BaseDirectory.ToString()+"File/img1.png";

            img.zzsTextWater(soureFile, "白码驿站",savePath, 0.5f, 0.5f, 100);

            #endregion

            #region pdf加水印
            PdfOperate pdf = new PdfOperate();
            //源文件地址
            soureFile = AppDomain.CurrentDomain.BaseDirectory.ToString() + "File/test.pdf";
            //保存地址
            savePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "File/test1.pdf";
            string msg = "";
            bool b = pdf.PDFWatermarkText(soureFile, savePath, "白码驿站", 100, 100, ref msg);

            #endregion
            

        }
    }
}
                                


Tags: C# 、 操作PDF

很赞哦! (0)

评 论

点击排行

系统处理中...