• 我的类库之添加水印类


    /*
    * 名称: 个人程序
    * 版本: V1.0
    * 作者: 王海鹏(Jack)
    * 日期: 2011-05-17
    * CLR版本: 4.0
    * 命名空间名称: WHPFunction.Drawing
    * 文件名: AddImageWatermark.cs
    *
    *QQ : 383828353
    *Email: wh2004_1@qq.com
    *
    */



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

    namespace WHPFunction.Drawing
    {
    /// <summary>
    /// 该枚举用来识别是文字水印还是图片水印
    /// </summary>
    public enum MarkType
    {
    Text, Image
    }

    /// <summary>
    /// 实体类:实现图片水印功能:添加水印
    /// </summary>
    public class ImageWatermark
    {
    #region -------------成员变量------------

    private string _text = ""; //要添加到水印的文字

    /// <summary>
    /// 要添加到水印的文字
    /// </summary>
    public string Text
    {
    get { return _text; }
    set { _text = value; }
    }

    private string _imgPath = ""; //要添加到水印的图片

    /// <summary>
    /// 要添加到水印的图片
    /// </summary>
    public string ImgPath
    {
    get { return _imgPath; }
    set { _imgPath = value; }
    }
    private int _markX = 5; //是水印位置横坐标

    /// <summary>
    /// 水印横坐标
    /// </summary>
    public int MarkX
    {
    get { return _markX; }
    set { _markX = value; }
    }
    private int _markY = 5; //水印位置纵坐标
    /// <summary>
    /// 水印纵坐标
    /// </summary>
    public int MarkY
    {
    get { return _markY; }
    set { _markY = value; }
    }
    private float _transparency = 0.9f; //水印透明度
    /// <summary>
    /// 透明度
    /// </summary>
    public float Transparency
    {
    get { return _transparency; }
    set { _transparency = value; }
    }
    private string _fontFamily = "宋体"; //水印文字字体
    /// <summary>
    /// 字体
    /// </summary>
    public string FontFamily
    {
    get { return _fontFamily; }
    set { _fontFamily = value; }
    }
    private Color _textColor = Color.Black; //水印字体颜色
    /// <summary>
    /// 字体颜色
    /// </summary>
    public Color TextColor
    {
    get { return _textColor; }
    set { _textColor = value; }
    }
    private bool _textbold = false; //文字是否粗体
    /// <summary>
    /// 是否粗体
    /// </summary>
    public bool Textbold
    {
    get { return _textbold; }
    set { _textbold = value; }
    }
    int[] sizes = new int[] { 48, 32, 16, 8, 6, 4 };//
    private Image _image = null; //原图片
    /// <summary>
    /// 原图片
    /// </summary>
    public Image SourceImage
    {
    get { return _image; }
    set { _image = value; }
    }
    private Image _markedImage = null; //水印后图片
    /// <summary>
    /// 水印后图片
    /// </summary>
    public Image MarkedImage
    {
    get { return _markedImage; }
    }
    private MarkType _markType = MarkType.Text; //标记类型
    /// <summary>
    /// 水印类型
    /// </summary>
    public MarkType MarkType
    {
    get { return _markType; }
    set { _markType = value; }
    }
    #endregion

    #region -------------构造函数------------
    public ImageWatermark()
    {

    }

    /// <summary>
    /// 初始化一个只添加文字水印得实例
    /// </summary>
    /// <param name="text">水印文字</param>
    /// <param name="fontFamily">文字字体</param>
    /// <param name="bold">是否粗体</param>
    /// <param name="color">字体颜色</param>
    /// <param name="markX">标记位置横坐标</param>
    /// <param name="markY">标记位置纵坐标</param>
    public ImageWatermark(string text,string fontFamily,bool bold,Color color,int markX,int markY)
    {
    this._markType=MarkType.Text;
    this._text=text;
    this._fontFamily=fontFamily;
    this._textbold=bold;
    this._textColor=color;
    this._markX=markX;
    this._markY=markY;
    //this.Mark();
    }

    /// <summary>
    /// 实例化一个只添加图片水印的实例
    /// </summary>
    /// <param name="imagePath">水印图片路径</param>
    /// <param name="tranparence">透明度</param>
    /// <param name="markX">标记位置横坐标</param>
    /// <param name="markY">标记位置纵坐标</param>
    public ImageWatermark(string imagePath,float tranparence,int markX,int markY)
    {
    this._markType = MarkType.Image;
    this._imgPath = imagePath;
    this._markX = markX;
    this._markY = markY;
    this._transparency = tranparence;
    //this.Mark();
    }

    #endregion

    #region -------------方法事件------------

    //添加文字水印
    /// <summary>
    /// 文字水印
    /// </summary>
    /// <param name="img">原始图片</param>
    /// <param name="text">水印文字</param>
    /// <returns>添加水印后图片</returns>
    public Image AddImageWaterMark(Image img,string text)
    {
    this.Text = text;
    //首先判断图片是否是GIF动画,如果是动画则不对图片进行改动
    foreach (Guid guid in img.FrameDimensionsList)
    {
    FrameDimension dimension
    = new FrameDimension(guid);

    if (img.GetFrameCount(dimension) > 1)
    {
    return img;
    }
    }
    //根据bitmap对象作为作图区
    Bitmap newBitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppRgb);
    //设置新建位图得分辨率
    newBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    //创建Graphics对象,一对该位图进行操作
    Graphics g = Graphics.FromImage(newBitmap);

    //消除锯齿
    g.SmoothingMode = SmoothingMode.HighQuality;

    //将原图拷贝到作图区
    g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
    //声明字体对象
    Font cFont = null;

    //用来测试水印文本长度得尺子
    SizeF size = new SizeF();

    //探测出一个适合图片大小的字体大小,以适应水印文字大小的自适应
    for (int i = 0; i < 6; i++)
    {
    //创建一个字体对象
    cFont = new Font(_fontFamily, sizes[i], _textbold == true ? FontStyle.Bold : FontStyle.Regular);

    //测量文本大小
    size = g.MeasureString(this.Text, cFont);

    //匹配第一个符合要求的字体大小
    if ((ushort)size.Width < (ushort)img.Width)
    {
    break;
    }
    }
    Brush brush
    = new SolidBrush(_textColor);

    g.DrawString(text, cFont, brush, img.Width
    -size.Width-1, img.Height-size.Height-1);
    g.Dispose();
    System.IO.MemoryStream ms
    = new System.IO.MemoryStream();
    newBitmap.Save(ms, ImageFormat.Jpeg);

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

    return img;
    }

    //添加图片水印
    /// <summary>
    /// 添加图片水印到图片
    /// </summary>
    /// <param name="img">原图</param>
    /// <param name="waterImg">水印图</param>
    /// <returns></returns>
    public Image AddImageWaterMark(Image img, Image waterImg)
    {
    //首先判断图片是否是GIF动画,如果是动画则不对图片进行改动
    foreach (Guid guid in img.FrameDimensionsList)
    {
    FrameDimension dimension
    = new FrameDimension(guid);

    if (img.GetFrameCount(dimension) > 1)
    {
    return img;
    }
    }
    //获得水印图像
    Image markImg = waterImg;
    //创建颜色矩阵
    float[][] ptsArray ={
    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,_transparency,0},
    //此处为0.0f为完全透明,1.0f为完全不透明
    new float[]{0,0,0,0,1}};
    ColorMatrix colorMatrix
    = new ColorMatrix(ptsArray);
    //新建一个Image属性
    ImageAttributes imageAttributes = new ImageAttributes();
    //将严策矩阵添加到属性
    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
    //生成位图作图区
    Bitmap newBitmap = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppRgb);

    //设置分辨率
    newBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);

    //创建graphic
    Graphics g = Graphics.FromImage(newBitmap);
    //消除锯齿
    g.SmoothingMode = SmoothingMode.HighQuality;

    //拷贝原图到作图区
    g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);

    //如果原图过小
    if (markImg.Width > img.Width || markImg.Height > img.Height)
    {
    System.Drawing.Image.GetThumbnailImageAbort callb
    = null;

    System.Drawing.Image new_img
    = markImg.GetThumbnailImage(img.Width / 4, markImg.Height * img.Width / markImg.Width, callb, new System.IntPtr());

    g.DrawImage(new_img,
    new Rectangle(img.Width-new_img.Width-1, img.Height-new_img.Height-1, new_img.Width, new_img.Height), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imageAttributes);

    new_img.Dispose();
    g.Dispose();
    System.IO.MemoryStream ms
    = new System.IO.MemoryStream();
    //将图像写入到memorystream中。
    newBitmap.Save(ms, ImageFormat.Jpeg);
    img
    = Image.FromStream(ms);

    return img;
    }
    //原图足够大
    else
    {
    g.DrawImage(markImg,
    new Rectangle(img.Width-markImg.Width-1, img.Height-markImg.Width-1, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);

    g.Dispose();

    System.IO.MemoryStream ms
    = new System.IO.MemoryStream();
    newBitmap.Save(ms, ImageFormat.Jpeg);
    img
    = Image.FromStream(ms);
    return img;
    }

    }


    //private void Mark()
    //{
    // //this._markedImage = Mark(this.SourceImage, this.MarkType, this.Text, this._image, this.MarkX, this.MarkY, this._textbold, this._textColor, this._transparency, this._fontFamily);
    //}
    #endregion

    }
    }

  • 相关阅读:
    java web开发中会遇到的异步执行方案
    MySQL中进行树状所有子节点的查询
    node.js ----NPM使用介绍
    Node.js--学习笔记
    node.js介绍及Win7环境安装测试(转)
    Jmeter中Websocket协议支持包的使用(转)
    jmeter---将回应数据写入到文件
    JMeter 插件 Json Path 解析 HTTP 响应 JSON 数据(转)
    python + Pyglet ---播放视频
    转 RTSP客户端模拟器(TCP方式,Python实现)
  • 原文地址:https://www.cnblogs.com/netact/p/2050565.html
Copyright © 2020-2023  润新知