C# 添加图片水印的代码示例

发布时间:2020-05-27编辑:脚本学堂
本文介绍下,用c#实现的添加水印的一段完整代码,很经典,好多人都在用。有需要的朋友参考下吧。

代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Imaging;
namespace MyWaterMark
{
    /// <summary> 
    /// WaterMark 的摘要说明 
    /// </summary> 
    /// edit by www.jb200.com
    /// <param name="strCopyright">要加入的文字 </param> 
    /// <param name="strCopyrightColor">要加入的文字颜色 </param> 
    /// <param name="strMarkPath">水印图片路径 </param> 
    /// <param name="strPhotoPath">要加水印的图片路径 </param> 
    /// <param name="strSavePath">处理后的图片路径 </param> 
    /// <param name="iMarkRightSpace">水印在修改图片中距左边的宽度 </param> 
    /// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度 </param> 
    /// <param name="iDiaphaneity">水印图片的透明度 </param> 
    /// <param name="iFontRightSpace">文字 </param> 
    /// <param name="iFontButtomSpace">文字 </param> 
    /// <param name="iFontDiaphaneity">文字 </param> 
    /// <param name="bShowCopyright">是否显示文字 </param> 
    /// <param name="bShowMarkImage">是否显示水印图片 </param>
    public class WaterMark
    {
        #region param
        private string strCopyright, strMarkPath, strPhotoPath, strSavePath;
        private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;
        private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80;
        private int iFontSize = 10;
        private bool bShowCopyright = true, bShowMarkImage = true;
        private int red = 0, greed = 0, blue = 0;
        Image imageBit=null;

        #endregion
        #region WaterMark
        public WaterMark()
        {
            this.strCopyright = "";
            this.strMarkPath = null;
            this.strPhotoPath = null;
            this.strSavePath = null;
            this.iDiaphaneity = 70;
            this.iMarkRightSpace = 0;
            this.iMarkButtomSpace = 0;
        }
        /// <summary> 
        /// 主要用两样都加的 
        /// </summary> 
        public WaterMark(string copyright, string copyrightColor, string markPath, string photoPath,Image imageBitT, string savePath,int apha)
        {
            this.strCopyright = copyright;
            this.strMarkPath = markPath;
            this.strPhotoPath = photoPath;
            this.strSavePath = savePath;
            this.iDiaphaneity = apha;
            this.iMarkRightSpace = 0;
            this.iMarkButtomSpace = 0;
            this.imageBit = imageBitT;
            try
            {
                Color my = new Color();
                if (copyrightColor.IndexOf("#") >= 0)
                {
                    my = ColorTranslator.FromHtml(copyrightColor);
                }
                else
                {
                    my = Color.FromName(copyrightColor);
                }
                this.red = my.R;
                this.greed = my.G;
                this.blue = my.B;
            }
            catch (Exception) { }
        }
        #endregion
        #region property
        /// <summary> 
        /// 设置是否显示水印文字 
        /// </summary> 
        public bool ShowCopyright
        {
            set { this.bShowCopyright = value; }
        }
        /// <summary> 
        /// 设置是否显示水印图片 
        /// </summary> 
        public bool ShowMarkImage
        {
            set { this.bShowMarkImage = value; }
        }
        /// <summary> 
        /// 获取或设置要加入的文字 
        /// </summary> 
        public string Copyright
        {
            set { this.strCopyright = value; }
        }
        /// <summary> 
        /// 设置要加入的文字颜色
        /// </summary> 
        public bool strCopyrightColor
        {
            set
            {
                Color my = new Color();
                if (value.ToString().IndexOf("#") >= 0)
                {
                    my = ColorTranslator.FromHtml(value.ToString());
                }
                else
                {
                    my = Color.FromName(value.ToString());
                }
                this.red = my.R;
                this.greed = my.G;
                this.blue = my.B;
            }
        }
        /// <summary> 
        /// 获取或设置加水印后的图片路径 
        /// </summary> 
        public string SavePath
        {
            get { return this.strSavePath; }
            set { this.strSavePath = value; }
        }
        /// <summary> 
        /// 获取或设置水印图片路径 
        /// </summary> 
        public string MarkPath
        {
            get { return this.strMarkPath; }
            set { this.strMarkPath = value; }
        }
        /// <summary> 
        /// 获取或设置要加水印图片的路径 
        /// </summary> 
        public string PhotoPath
        {
            get { return this.strPhotoPath; }
            set { this.strPhotoPath = value; }
        }
        /// <summary> 
        /// 设置水印图片的透明度 
        /// </summary> 
        public int Diaphaneity
        {
            set
            {
                if (value > 0 && value <= 100)
                    this.iDiaphaneity = value;
            }
        }
        /// <summary> 
        /// 设置水印字体的透明度0-255 
        /// </summary> 
        public int FontDiaphaneity
        {
            set
            {
                if (value >= 0 && value <= 255)
                    this.iFontDiaphaneity = value;
            }
        }
        public int FontSize
        {
            set
            {
                iFontSize = Convert.ToInt32(value);
            }
        }
        /// <summary> 
        /// 设置水印图片在修改图片中距左边的宽度 
        /// </summary> 
        public int MarkRightSpace
        {
            set { this.iMarkRightSpace = value; }
        }
        /// <summary> 
        /// 设置水印图片在修改图片中距底部的高度 
        /// </summary> 
        public int MarkButtomSpace
        {
            set { this.iMarkButtomSpace = value; }
        }
        /// <summary> 
        /// 设置水印字体在修改图片中距左边的距离 
        /// </summary> 
        public int FontRightSpace
        {
            set { iFontRightSpace = value; }
        }
        /// <summary> 
        /// 设置水印字体在修改图片中距底部的高度 
        /// </summary> 
        public int FontButtomSpace
        {
            set { iFontButtomSpace = value; }
        }
        #endregion

        /// <summary> 
        /// 生成水印图片 
        /// </summary> 
        /// <returns> </returns> 
        public Image createMarkPhoto()
        {
            Image gPhoto=null;
            Bitmap bmWatermark = null;
            if (this.strPhotoPath == "" || this.strPhotoPath == null)
            {
                gPhoto = this.imageBit;
            }
            else {
                 gPhoto = Image.FromFile(this.strPhotoPath);
            }
            
            int PhotoWidth = gPhoto.Width;
            int PhotoHeight = gPhoto.Height;
            Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb);
            bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);
            try
            {
                if (bShowCopyright)
                {
                    Graphics grPhoto = Graphics.FromImage(bitPhoto);
                    grPhoto.SmoothingMode = SmoothingMode.Antialias;
                    grPhoto.DrawImage(gPhoto, new Rectangle(0, 0, PhotoWidth, PhotoHeight), 0, 0, PhotoWidth, PhotoHeight, GraphicsUnit.Pixel);
                    Font crFont = new Font("楷体", iFontSize, FontStyle.Bold);
                    SizeF crSize = grPhoto.MeasureString(strCopyright, crFont);
                    //设置字体在图片中的位置 
                    float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height);
                    //float xCenterOfImg = (phWidth/2); 
                    float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / 2);
                    //设置字体居中
                    StringFormat StrFormat = new StringFormat();
                    StrFormat.Alignment = StringAlignment.Center;
                    //设置绘制文本的颜色和纹理 (Alpha=153) 
                    SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, this.red, this.greed, this.blue));
                    //将版权信息绘制到图象上 
                    grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);
                    gPhoto = bitPhoto;
                    grPhoto.Dispose();
                }
                if (bShowMarkImage && strMarkPath != "" && strMarkPath != null)
                {
                    //创建一个需要填充水银的Image对象 
                    Image imgWatermark = new Bitmap(strMarkPath);
                    int iMarkWidth = imgWatermark.Width;
                    int iMarkmHeight = imgWatermark.Height;
                    Graphics grWatermark = null;
                    if (bShowCopyright)
                    {
                        //在原来修改过的bmPhoto上创建一个水银位图 
                        bmWatermark = new Bitmap(bitPhoto);
                        bmWatermark.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);
                    }
                    else
                    {
                        bmWatermark = new Bitmap(gPhoto);
                    }
                    //将位图bmWatermark加载到Graphics对象 
                    grWatermark = Graphics.FromImage(bmWatermark);
                    ImageAttributes imageAttributes = new ImageAttributes();
                    ColorMap colorMap = new ColorMap();
                    colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                    colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                    ColorMap[] remapTable = { colorMap };
                    imageAttributes.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, (float)iDiaphaneity/100f, 0.0f}, 
                    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
                    ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
                    imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    grWatermark.DrawImage(imgWatermark, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / 2)), 
(PhotoHeight - iMarkButtomSpace - (iMarkmHeight / 2)), iMarkWidth, iMarkmHeight), 0, 0, iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, 
imageAttributes);
                    gPhoto = bmWatermark;
                    grWatermark.Dispose();
                    imgWatermark.Dispose();
                }
                if (strSavePath == "" || strSavePath == null)
                {
                    //if (bitPhoto != null)
                     //   bitPhoto.Dispose();
                    //if (bmWatermark != null)
                    //    bmWatermark.Dispose();
                    return gPhoto;
                }
                else
                {
                    gPhoto.Save(strSavePath, ImageFormat.Jpeg );
                    if (bitPhoto != null)
                        bitPhoto.Dispose();
                    if (bmWatermark != null)
                        bmWatermark.Dispose();
                    gPhoto.Dispose();
                    return null;
                }
            }
            finally
            {
            }         
        }
    }
}