C#(.net)水印图片的生成
编辑器加载中...
 C#(.net)水印图片的生成
/*
* 
*    使用说明:
*  建议先定义一个WaterImage实例
*  然后利用实例的属性,去匹配需要进行操作的参数
*  然后定义一个WaterImageManage实例
*  利用WaterImageManage实例进行DrawImage(),印图片水印
*  DrawWords()印文字水印
* 
-*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace ABC
{
    /// 
    /// 图片位置
    ///  
    public enum ImagePosition
    {
        LeftTop,        //左上
        LeftBottom,    //左下
        RightTop,       //右上
        RigthBottom, //右下
        TopMiddle,     //顶部居中
        BottomMiddle, //底部居中
        Center           //中心
    }
    /// 
    /// 水印图片的操作管理 Design by Gary Gong From Demetersoft.com
    ///  
    public class WaterImageManage
    {
        /// 
        /// 生成一个新的水印图片制作实例
        ///  
        public WaterImageManage()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /// 
        /// 添加图片水印
        ///  
        /// 源图片文件名
        /// 水印图片文件名
        /// 透明度(0.1-1.0数值越小透明度越高)
        /// 位置
        /// 图片的路径
        /// 返回生成于指定文件夹下的水印文件名 
        public string DrawImage(string sourcePicture,
                                          string waterImage,
                                          float alpha, 
                                          ImagePosition position,
                                          string PicturePath)
        {
            //
            // 判断参数是否有效
            //
            if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
            {
                return sourcePicture;
            } 
            //
            // 源图片,水印图片全路径
            //
            string sourcePictureName = PicturePath + sourcePicture;
            string waterPictureName = PicturePath + waterImage;
            string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
            string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
            //
            // 判断文件是否存在,以及类型是否正确
            //
            if (System.IO.File.Exists(sourcePictureName) == false ||
                System.IO.File.Exists(waterPictureName) == false || (
                fileSourceExtension != ".gif" &&
                fileSourceExtension != ".jpg" &&
                fileSourceExtension != ".png") || (
                fileWaterExtension != ".gif" &&
                fileWaterExtension != ".jpg" &&
                fileWaterExtension != ".png")
                )
            {
                return sourcePicture;
            }
            // 
            // 目标图片名称及全路径
            //
            string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_1101.jpg"; 
            //
            // 将需要加上水印的图片装载到Image对象中
            //
            Image imgPhoto = Image.FromFile(sourcePictureName);
            //
            // 确定其长宽
            //
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;
            //
            // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
            //
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
            //
            // 设定分辨率
            // 
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //
            // 定义一个绘图画面用来装载位图
            //
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            //
            //同样,由于水印是图片,我们也需要定义一个Image来装载它
            //
            Image imgWatermark = new Bitmap(waterPictureName);
            //
            // 获取水印图片的高度和宽度
            //
            int wmWidth = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;
            //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
            // 成员名称   说明 
            // AntiAlias      指定消除锯齿的呈现。 
            // Default        指定不消除锯齿。 
            // HighQuality 指定高质量、低速度呈现。 
            // HighSpeed   指定高速度、低质量呈现。 
            // Invalid        指定一个无效模式。 
            // None          指定不消除锯齿。 
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias; 
            //
            // 第一次描绘,将我们的底图描绘在绘图画面上
            //
            grPhoto.DrawImage(imgPhoto,
                                        new Rectangle(0, 0, phWidth, phHeight),
                                        0,
                                        0,
                                        phWidth,
                                        phHeight,
                                        GraphicsUnit.Pixel);
            //
            // 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
            //
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //
            // 继续,将水印图片装载到一个绘图画面grWatermark
            //
            Graphics grWatermark = Graphics.FromImage(bmWatermark);
            //
            //ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。
            //       
            ImageAttributes imageAttributes = new ImageAttributes(); 
            //
            //Colormap: 定义转换颜色的映射
            //
            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}, // red红色
           new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
           new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色       
           new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度     
           new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//
            // ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。
            // ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
             ColorAdjustType.Bitmap);
            //
            //上面设置完颜色,下面开始设置位置
            //
            int xPosOfWm;
            int yPosOfWm;
            switch (position)
            {
                case ImagePosition.BottomMiddle:
                    xPosOfWm = (phWidth - wmWidth) / 2;
                    yPosOfWm = phHeight - wmHeight - 10;
                    break; 
                case ImagePosition.Center:
                    xPosOfWm = (phWidth - wmWidth) / 2;
                    yPosOfWm = (phHeight - wmHeight) / 2;
                    break;
                case ImagePosition.LeftBottom:
                    xPosOfWm = 10;
                    yPosOfWm = phHeight - wmHeight - 10;
                    break;
                case ImagePosition.LeftTop:
                    xPosOfWm = 10;
                    yPosOfWm = 10;
                    break;
                case ImagePosition.RightTop:
                    xPosOfWm = phWidth - wmWidth - 10;
                    yPosOfWm = 10;
                    break;
                case ImagePosition.RigthBottom:
                    xPosOfWm = phWidth - wmWidth - 10;
                    yPosOfWm = phHeight - wmHeight - 10;
                    break;
                case ImagePosition.TopMiddle:
                    xPosOfWm = (phWidth - wmWidth) / 2;
                    yPosOfWm = 10;
                    break;
                default:
                    xPosOfWm = 10;
                    yPosOfWm = phHeight - wmHeight - 10;
                    break;
            } 
            // 
            // 第二次绘图,把水印印上去
            //
            grWatermark.DrawImage(imgWatermark,
             new Rectangle(xPosOfWm,
                                 yPosOfWm,
                                 wmWidth,
                                 wmHeight),
                                 0,
                                 0,
                                 wmWidth,
                                 wmHeight,
                                 GraphicsUnit.Pixel,
                                 imageAttributes); 
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();
            //
            // 保存文件到服务器的文件夹里面
            //
            imgPhoto.Save(targetImage, ImageFormat.Jpeg);
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            return targetImage.Replace(PicturePath, "");
        }
/*
* 
* 使用说明:
*  建议先定义一个WaterImage实例
*  然后利用实例的属性,去匹配需要进行操作的参数
*  然后定义一个WaterImageManage实例
*  利用WaterImageManage实例进行DrawImage(),印图片水印
*  DrawWords()印文字水印
* 
-*/
        /// 
        /// 在图片上添加水印文字
        ///  
        /// 源图片文件(文件名,不包括路径) 
        /// 需要添加到图片上的文字
        /// 透明度
        /// 位置
        /// 文件路径
        /// 
    /// 装载水印图片的相关信息
    ///  
    public class WaterImage
    {
        public WaterImage()
        {
        }
        private string m_sourcePicture;
        /// 
        /// 源图片地址名字(带后缀) 
        ///  
        public string SourcePicture
        {
            get { return m_sourcePicture; }
            set { m_sourcePicture = value; }
        } 
        private string m_waterImager;
        /// 
        /// 水印图片名字(带后缀)
        ///  
        public string WaterPicture
        {
            get { return m_waterImager; }
            set { m_waterImager = value; }
        }
        private float m_alpha;
        /// 
        /// 水印图片文字的透明度
        ///  
        public float Alpha
        {
            get { return m_alpha; }
            set { m_alpha = value; }
        }
        private ImagePosition m_postition;
        /// 
        /// 水印图片或文字在图片中的位置
        ///  
        public ImagePosition Position
        {
            get { return m_postition; }
            set { m_postition = value; }
        }
        private string m_words;
        /// 
        /// 水印文字的内容
        ///  
        public string Words
        {
            get { return m_words; }
            set { m_words = value; }
        }
    }
}
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号