C#.NET 添加图片水印

简介: using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.
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
    }
}

转自:http://www.cnblogs.com/livexy/archive/2010/07/05/1771714.html

目录
相关文章
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
765 0
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
607 0
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
592 18
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
743 12
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
295 6
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
308 6
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
349 6
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
1079 13
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
1138 5
|
Java 物联网 编译器
C#一分钟浅谈:.NET Core 与 .NET 5 区别
本文对比了 .NET Core 和 .NET 5,从历史背景、主要区别、常见问题及易错点等方面进行了详细分析。.NET Core 侧重跨平台支持和高性能,而 .NET 5 在此基础上统一了 .NET 生态系统,增加了更多新特性和优化。开发者可根据具体需求选择合适的版本。
724 7