程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#圖片水印類

C#圖片水印類

編輯:C#基礎知識

這個是學習用的呃,主要看一下水印在修改圖片中距左邊的寬度和高度是雜弄的就哦客了。



using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ECHX.BLL
{
    /// <summary>
    /// WaterMark 的摘要說明
    /// </summary>
    ///  
    /// <param name="strCopyright">要加入的文字</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;
        #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>
        /// <param name="copyright">要加入的文字</param>
        /// <param name="markPath">水印圖片路徑</param>
        /// <param name="photoPath">要加水印的圖片路徑</param>
        /// <param name="savePath">處理後的圖片路徑</param>
        public WaterMark(string copyright, string markPath, string photoPath, string savePath)
        {
            this.strCopyright = copyright;
            this.strMarkPath = markPath;
            this.strPhotoPath = photoPath;
            this.strSavePath = savePath;
            this.iDiaphaneity = 70;
            this.iMarkRightSpace = 0;
            this.iMarkButtomSpace = 0;
        }
        #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 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;
            }
        }

        /// <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 void createMarkPhoto()
        {
            Bitmap bmWatermark = null;
            Image 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, 0, 0, 0));

                    //將版權信息繪制到圖象上
                    grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat);

                    gPhoto = bitPhoto;
                    grPhoto.Dispose();
                }

 

                if (bShowMarkImage)
                {
                    //創建一個需要填充水銀的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();
                }
                gPhoto.Save(strSavePath, ImageFormat.Jpeg);
            }
            finally
            {

                if (bitPhoto != null)
                    bitPhoto.Dispose();

                if (bmWatermark != null)
                    bmWatermark.Dispose();

                gPhoto.Dispose();
            }

        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved