C#.NET 图片水印添加代码

 更新时间:2016年07月25日 15:20:16   作者:熊哥  
这篇文章主要为大家详细介绍了C#.NET 图片水印添加代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#.NET添加 图片水印的方法,供大家参考,具体内容如下

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Pub.Class
{
 /// <summary>
 /// 添加水印类 只支持添加图片水印
 /// </summary>
 public class Watermark
 {
  #region 私有成员
  private string modifyImagePath=null;
 private string drawedImagePath=null;
 private int rightSpace;
 private int bottoamSpace;
 private int lucencyPercent=70;
 private string outPath=null;
  #endregion

  #region 构造器
  /// <summary>
  /// 构造函数
  /// </summary>
  public Watermark() { }
  #endregion

  #region 属性
  /// <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;}
 }
 /// <summary>
  /// 获取或设置水印在修改图片中距底部的高度
 /// </summary>
 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 开始绘制水印 DrawImage
  /// <summary>
 /// 开始绘制水印
 /// </summary>
  /// <example>
  /// <code>
  ///  Watermark wm = new Watermark();
  ///  wm.DrawedImagePath= Server.MapPath("") + "/upfile/" + "backlogo.gif";
  ///  wm.ModifyImagePath=path; 
  ///  wm.RightSpace=184;
  ///  wm.BottoamSpace=81;
  ///  wm.LucencyPercent=50;
  ///  wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension;
  ///  wm.DrawImage();
  ///  
  ///  //保存加水印过后的图片,删除原始图片 
  ///  mFileName=fileName + "_new" + extension;
  ///  if(File.Exists(path)) { File.Delete(path); } 
  /// </code>
  /// </example>
 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
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Unity实现绕任意轴任意角度旋转向量

    Unity实现绕任意轴任意角度旋转向量

    这篇文章主要为大家详细介绍了Unity实现绕任意轴任意角度旋转向量,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • WCF实现双向通信

    WCF实现双向通信

    这篇文章介绍了WCF实现双向通信的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#(4.0)不常见的语法

    C#(4.0)不常见的语法

    今天工作遇到4.0新增语法 不解 请教 遂知,简单的整理下,需要的朋友可以参考下
    2012-02-02
  • C#超市收银系统设计

    C#超市收银系统设计

    这篇文章主要为大家详细介绍了C#超市收银系统设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#如何给PDF文件添加水印

    C#如何给PDF文件添加水印

    这篇文章主要为大家详细介绍了C#如何给PDF文件添加水印的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Unity3D实现控制摄像机移动

    Unity3D实现控制摄像机移动

    这篇文章主要为大家详细介绍了Unity3D实现控制摄像机移动 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#函数式程序设计之用闭包封装数据的实现代码

    C#函数式程序设计之用闭包封装数据的实现代码

    如果一个程序设计语言能够用高阶函数解决问题,则意味着数据作用域问题已十分突出。当函数可以当成参数和返回值在函数之间进行传递时,编译器利用闭包扩展变量的作用域,以保证随时能得到所需要的数据
    2014-03-03
  • c# 实现位图算法(BitMap)

    c# 实现位图算法(BitMap)

    这篇文章主要介绍了c# 如何实现位图算法(BitMap),文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#串口接收程序的实现

    C#串口接收程序的实现

    本文主要介绍了C#串口接收程序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • C#使用Interlocked实现线程同步

    C#使用Interlocked实现线程同步

    今天小编就为大家分享一篇关于C#使用Interlocked实现线程同步,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10

最新评论