c#图片添加水印的实例代码

 更新时间:2013年07月23日 10:10:11   作者:  
这篇文章介绍了c#图片添加水印的实例代码,有需要的朋友可以参考一下
复制代码 代码如下:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageDrawing
{
 /// <summary>
 /// 图片修改类,主要是用来保护图片版权的
 /// </summary>
 public class ImageModification
 {
  #region "member fields"
  private string modifyImagePath=null;
  private string drawedImagePath=null;
  private int rightSpace;
  private int bottoamSpace;
  private int lucencyPercent=70;
  private string outPath=null;
  #endregion
  public ImageModification()
  {
  }
  #region "propertys"
  /// <summary>
  /// 获取或设置要修改的图像路径
  /// </summary>
  public string ModifyImagePath
  {
   get{return this.modifyImagePath;}
   set{this.modifyImagePath=value;}
  }
  /// <summary>
  /// 获取或设置在画的图片路径(水印图片)
  /// </summary>
  public string DrawedImagePath
  {
   get{return this.drawedImagePath;}
   set{this.drawedImagePath=value;}
  }
  /// <summary>
  /// 获取或设置水印在修改图片中的右边距
  /// </summary>
  public int RightSpace
  {
   get{return this.rightSpace;}
   set{this.rightSpace=value;}
  }
  //获取或设置水印在修改图片中距底部的高度
  public int BottoamSpace
  {
   get{return this.bottoamSpace;}
   set{this.bottoamSpace=value;}
  }
  /// <summary>
  /// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
  /// </summary>
  public int LucencyPercent
  {
   get{return this.lucencyPercent;}
   set
   {
    if(value>=0&&value<=100)
     this.lucencyPercent=value;
   }
  }
  /// <summary>
  /// 获取或设置要输出图像的路径
  /// </summary>
  public string OutPath
  {
   get{return this.outPath;}
   set{this.outPath=value;}
  }
  #endregion
  #region "methods"
  /// <summary>
  /// 开始绘制水印
  /// </summary>
  public void DrawImage()
  {
   Image modifyImage=null;
   Image drawedImage=null;
   Graphics g=null;
   try
   {   
    //建立图形对象
    modifyImage=Image.FromFile(this.ModifyImagePath);
    drawedImage=Image.FromFile(this.DrawedImagePath);
    g=Graphics.FromImage(modifyImage);
    //获取要绘制图形坐标
    int x=modifyImage.Width-this.rightSpace;
    int y=modifyImage.Height-this.BottoamSpace;
    //设置颜色矩阵
    float[][] matrixItems ={
             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, (float)this.LucencyPercent/100f, 0},
             new float[] {0, 0, 0, 0, 1}};
    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
    ImageAttributes imgAttr=new ImageAttributes();
    imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
    //绘制阴影图像
    g.DrawImage(
     drawedImage,
     new Rectangle(x,y,drawedImage.Width,drawedImage.Height),
     0,0,drawedImage.Width,drawedImage.Height,
     GraphicsUnit.Pixel,imgAttr);
    //保存文件
    string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
    FileInfo file=new FileInfo(this.ModifyImagePath);
    ImageFormat imageType=ImageFormat.Gif;
    switch(file.Extension.ToLower())
    {
     case ".jpg":
      imageType=ImageFormat.Jpeg;
      break;
     case ".gif":
      imageType=ImageFormat.Gif;
      break;
     case ".png":
      imageType=ImageFormat.Png;
      break;
     case ".bmp":
      imageType=ImageFormat.Bmp;
      break;
     case ".tif":
      imageType=ImageFormat.Tiff;
      break;
     case ".wmf":
      imageType=ImageFormat.Wmf;
      break;
     case ".ico":
      imageType=ImageFormat.Icon;
      break;
     default:
      break;
    }
    MemoryStream ms=new MemoryStream();
    modifyImage.Save(ms,imageType);
    byte[] imgData=ms.ToArray();
    modifyImage.Dispose();
    drawedImage.Dispose();
    g.Dispose();
    FileStream fs=null;
    if(this.OutPath==null || this.OutPath=="")
    {
     File.Delete(this.ModifyImagePath);
     fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
    }
    else
    {
     fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write);
    }
    if(fs!=null)
    {
     fs.Write(imgData,0,imgData.Length);
     fs.Close();
    }
   }
   finally
   {
    try
    {
     drawedImage.Dispose();
     modifyImage.Dispose();
     g.Dispose();
    }
    catch{;}
   }
  }
  #endregion
 }
}

相关文章

  • C# 创建控制台应用程序

    C# 创建控制台应用程序

    这篇文章主要介绍了C# 创建控制台应用程序,在学习C#语言的时候,首先要学习控制台的应用程序,这样才能专注于语言的学习,减少学习的梯度,也有利于输出自己需要输出的内容,一定要先使用控制台的应用程序的方式,下面就和小编一起学习该内容吧
    2021-10-10
  • Unity延时执行的多种方法小结

    Unity延时执行的多种方法小结

    本文主要介绍了4种延时执行的方法,主要包括Update计时器,Invoke,协程,DoTween,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • DevExpress实现禁用TreeListNode CheckBox的方法

    DevExpress实现禁用TreeListNode CheckBox的方法

    这篇文章主要介绍了DevExpress实现禁用TreeListNode CheckBox的方法,在项目开发中有应用价值,需要的朋友可以参考下
    2014-08-08
  • WPF实现自定义Panel面板的示例详解

    WPF实现自定义Panel面板的示例详解

    WPF中的Panel(面板),是继承自FrameworkElement的抽象类,表示一个可以用来排列子元素的面板,本文主要来和大家聊聊WPF如何实现自定义Panel,感兴趣的可以了解下
    2023-09-09
  • C#快速实现IList非泛型类接口的自定义类作为数据源

    C#快速实现IList非泛型类接口的自定义类作为数据源

    本文主要介绍了C#快速实现IList非泛型类接口的自定义类作为数据源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • C# Winform实现圆角无锯齿按钮

    C# Winform实现圆角无锯齿按钮

    这篇文章主要为大家详细介绍了C# Winform实现圆角无锯齿按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • C#嵌套类的访问方法

    C#嵌套类的访问方法

    这篇文章主要介绍了C#嵌套类的访问方法,本文给出了嵌套类代码和访问方法代码,不会的同学照搬对照中的方法即可,需要的朋友可以参考下
    2015-04-04
  • C#实现简单的五子棋游戏

    C#实现简单的五子棋游戏

    这篇文章主要为大家详细介绍了C#实现简单的五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • C#手动操作DataGridView使用各种数据源填充表格实例

    C#手动操作DataGridView使用各种数据源填充表格实例

    本文主要介绍了C#手动操作DataGridView使用各种数据源填充表格实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • C#中使用Spire.XLS来操作Excel数据的实现

    C#中使用Spire.XLS来操作Excel数据的实现

    本文主要介绍了C#中使用Spire.XLS来操作Excel数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04

最新评论