一个给网站图片加上Stamp的C# Class

该博客介绍了用C#处理图片版权问题,实现给图片添加印章的功能。定义了StamperImage类,可指定文字或图片形式的印章,能设置印章位置、边距等。还给出了在ASP.NET页面中的使用范例,展示如何调用该功能处理图片。
部署运行你感兴趣的模型镜像

很多网站为了自己的图片版权,就需要给上载的图片加上版权所有的标志,这个C# class为了处理这样的问题的。这是昨天写的一个给图片加上文字c#过程的一个扩充,除了能够在图片上增加文字外,还可以指定图片形式的印章。

Stamper函数用来给图片加上印章,Save可以将图片保存到指定的文件中或者IO Stream中。

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace MySite
{
 /// <summary>
 /// StamperImage给图片加上印章。
 /// </summary>
 public class StamperImage: IDisposable

 {
  public enum Position : int
  {
   TopLeft = 1,
   TopRight = 2,
   BottomLeft = 3,
   BottomRight = 4,
   Center = 5
  }

  int _margin = 3;
  private Position _stampPos = Position.TopLeft;
  private string _originImageFile = "",_stampImageFile = "";
  private Bitmap _bmOriginImage,_bmStampImage;
  private string _stampText = "";
  private bool _textStampMode = false;
  private Font _textFont = new Font("Arial,宋体",10);
  private Color _textColor = Color.White;
  private bool _dispose = false;

  public StamperImage()
  {
   _dispose = true;
  }
  public StamperImage(string sOriginImageFile,string sStampImageFile)
  {
   _originImageFile = sOriginImageFile;
   _stampImageFile = sStampImageFile;
   try
   {
    _bmOriginImage = new Bitmap(_originImageFile);
    _bmStampImage = new Bitmap(_stampImageFile);
   }
   catch(Exception e)
   {
    throw e;
   }
  }
  public StamperImage(Bitmap sOrginImage,Bitmap sStampImage)
  {
   _bmOriginImage = sOrginImage;
   _bmStampImage = sStampImage;
  }
  public StamperImage(string sOriginImageFile,string sStampText,Font sStampTextFont,Color sStampTextColor)
  {
   _originImageFile = sOriginImageFile;
   try
   {
    _bmOriginImage = new Bitmap(_originImageFile);
   }
   catch(Exception e)
   {
    throw e;
   }
   _textStampMode = true;
   _stampText = sStampText;
   _textFont = sStampTextFont;
   _textColor = sStampTextColor;
  }
  public StamperImage(string sOriginImageFile,string sStampText,Font sStampTextFont)
  {
   _originImageFile = sOriginImageFile;
   try
   {
    _bmOriginImage = new Bitmap(_originImageFile);
   }
   catch(Exception e)
   {
    throw e;
   }
   _textStampMode = true;
   _stampText = sStampText;
   _textFont = sStampTextFont;
  }
  public StamperImage(string sOriginImageFile,string sStampText,Color sStampTextColor)
  {
   _originImageFile = sOriginImageFile;
   try
   {
    _bmOriginImage = new Bitmap(_originImageFile);
   }
   catch(Exception e)
   {
    throw e;
   }
   _textStampMode = true;
   _stampText = sStampText;
   _textColor = sStampTextColor;
  }

  private void Dispose(bool disposing)
  {
   if(!_dispose)
   {
    if(disposing)
    {
     if(_bmOriginImage!=null)_bmOriginImage.Dispose();
     if(_bmStampImage!=null)_bmStampImage.Dispose();
     _dispose = true;
    }
   }
  }

  public void Dispose()
  {
   Dispose(true);
   GC.SuppressFinalize(this);
  }
  /// <summary>
  /// 给图片加上印章
  /// </summary>
  public void Stamper()
  {
   int _iOImageWith,_iOImageHeight;
   Point _stampPoint;
   try
   {
    Graphics _graSettingImage = Graphics.FromImage(_bmOriginImage);
    _iOImageWith = _bmOriginImage.Width;
    _iOImageHeight = _bmOriginImage.Height;
    if(_textStampMode)
    {  
     int _iFontHeight = _textFont.Height;
     int _iFontWith = ((int)_textFont.SizeInPoints * _stampText.Length);
     switch(_stampPos)
     {
      case Position.TopLeft:_stampPoint = new Point(_margin,_margin);break;
      case Position.TopRight:_stampPoint = new Point(_iOImageWith - _margin - _iFontWith,_margin);break;
      case Position.BottomLeft:_stampPoint = new Point(_margin,_iOImageHeight - _iFontHeight - _margin);break;
      case Position.BottomRight:_stampPoint = new Point(_iOImageWith - _margin - _iFontWith,_iOImageHeight - _iFontHeight - _margin);break;
      case Position.Center:_stampPoint = new Point(_iOImageWith/2 - _iFontWith/2,_iOImageHeight/2 - _iFontHeight/2);break;
      default:_stampPoint = new Point(_margin,_margin);break;
     }
     _graSettingImage.DrawString(_stampText,_textFont,new SolidBrush(_textColor),_stampPoint);
    }
    else
    {
     int _iSImageWith = _bmStampImage.Width;
     int _iSImageHeight = _bmStampImage.Height;
     switch(_stampPos)
     {
      case Position.TopLeft:_stampPoint = new Point(_margin,_margin);break;
      case Position.TopRight:_stampPoint = new Point(_iOImageWith - _margin - _iSImageWith,_margin);break;
      case Position.BottomLeft:_stampPoint = new Point(_margin,_iOImageHeight - _iSImageHeight - _margin);break;
      case Position.BottomRight:_stampPoint = new Point(_iOImageWith - _margin - _iSImageWith,_iOImageHeight - _iSImageHeight - _margin);break;
      case Position.Center:_stampPoint = new Point(_iOImageWith/2 - _iSImageWith/2,_iOImageHeight/2 - _iSImageHeight/2);break;
      default:_stampPoint = new Point(_margin,_margin);break;
     }
     _graSettingImage.DrawImage(_bmStampImage,_stampPoint);
    }
    _graSettingImage.Dispose();
   }
   catch(Exception e)
   {
    throw e;
   }
  }
  public void Save(string _targetImageFile)
  {
   _bmOriginImage.Save(_targetImageFile);
  }
  public void Save(System.IO.Stream _stream)
  {
   _bmOriginImage.Save(_stream,ImageFormat.Jpeg);
  }


  /// <summary>
  /// 印章的边距离
  /// </summary>
  public int Margin
  {
   get
   {
    return _margin;
   }
   set
   {
    _margin = value;
   }
  }
  /// <summary>
  /// 印章的位置
  /// </summary>
  public Position StampPos
  {
   get
   {
    return _stampPos;
   }
   set
   {
    _stampPos = value;
   }
  }
  
  /// <summary>
  /// 需要加上印章的图像文件
  /// </summary>
  public string OriginImageFile
  {
   get
   {
    return _originImageFile;
   }
   set
   {
    try
    {
     _originImageFile = value;
     _bmOriginImage = new Bitmap(_originImageFile);
     _dispose = false;
    }
    catch(Exception e)
    {
     throw e;
    }
   }
  }
  /// <summary>
  /// 需要加上印章的图像
  /// </summary>
  public Bitmap OriginImage
  {
   get
   {
    return _bmOriginImage;
   }
   set
   {
    _bmOriginImage = value;
    _dispose = false;
   }
  }
  /// <summary>
  /// 印章图像文件路径
  /// </summary>
  public string StampImageFile
  {
   get
   {
    return _stampImageFile;
   }
   set
   {
    try
    {
     _stampImageFile = value;
     _bmStampImage = new Bitmap(_stampImageFile);
     _dispose = false;
    }
    catch(Exception e)
    {
     throw e;
    }

   }
  }
  /// <summary>
  /// 印章图像
  /// </summary>
  public Bitmap StampImage
  {
   get
   {
    return _bmStampImage;
   }
   set
   {
    _bmStampImage = value;
    _dispose = false;
   }
  }
  /// <summary>
  /// 印章文字
  /// </summary>
  public string StampText
  {
   get
   {
    return _stampText;
   }
   set
   {
    _stampText = value;
   }
  }
  /// <summary>
  /// 印章文字的字体
  /// </summary>
  public Font TextFont
  {
   get
   {
    return _textFont;
   }
   set
   {
    _textFont = value;
   }
  }
  /// <summary>
  /// 印章文字的颜色
  /// </summary>
  public Color TextColor
  {
   get
   {
    return _textColor;
   }
   set
   {
    _textColor = value;
   }
  }
  /// <summary>
  /// 印章模式,true加上text印章,否则为图像印章
  /// </summary>
  public bool TextStampMode
  {
   get
   {
    return _textStampMode;
   }
   set
   {
    _textStampMode = value;
   }
  }

 }
}

 

 

下面是一个asp.net页面的使用范例:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MySite
{
 /// <summary>
 /// sampleImage 的摘要说明。
 /// </summary>
 public class sampleImage : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   Response.ContentType = "image/jpeg";
   string _sampleImageFile = Server.MapPath("sample.jpg");
   string _stampImageFile = Server.MapPath("stamp.jpg");

   string _stampText = Request.Params["Text"];
   string _stampPos = Request.Params["Pos"];
   string _stamper = Request.Params["Stamp"];
   StamperImage _stamperImage;

   if(_stamper==null)_stamper = "";
   if(_stampText==""||_stampText==null)_stampText = "Hello, World!";
   if(_stampPos==""||_stampPos==null)_stampPos = "1";
   if(_stamper.ToLower() != "on")
   {
    _stamperImage = new StamperImage(_sampleImageFile,_stampText,Color.White);
   }
   else
   {
    _stamperImage = new StamperImage(_sampleImageFile,_stampImageFile);
   }

   _stamperImage.StampPos = (StamperImage.Position)Convert.ToInt16(_stampPos);
   _stamperImage.Stamper();
   _stamperImage.Save(Response.OutputStream);
   _stamperImage.Dispose();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}

您可能感兴趣的与本文相关的镜像

Kotaemon

Kotaemon

AI应用

Kotaemon 是由Cinnamon 开发的开源项目,是一个RAG UI页面,主要面向DocQA的终端用户和构建自己RAG pipeline

asp.net生成PDF详解 asp.net生成PDF PDF详解 用C#制作PDF文件全攻略 丽水市汽车运输集团有限公司信息中心 苟安廷 目 录 前 言 3 第一部分 iText的简单应用 4 第一 创建一个Document 4 第一步 创建一个Document实例: 5 第二步 创建Writer实例 6 第三步 打开Document 6 第四步 添内容 10 第五步,关闭 document 11 第二 块、短句和段落 11 块 11 短句 12 段落 12 字体的延续 13 第三 锚点、列表和注释 14 锚点 14 列表 14 注释 15 第四 页眉页脚、节、区域和绘图对象 16 页眉页脚 16 节和区域 17 图形 17 第五 表格 18 一些简单的表格 18 一些表格参数 18 大表格 20 内存管理 20 嵌套表格 21 表格偏移 21 表格的绝对位置 21 第六 图片 21 Image对象 21 图片的位置 22 缩放和旋转图片 23 原始图片数据 23 System.Drawing.Bitmap 23 TIFF和CCITT 24 图片和其他对象 24 第二部分 其他文档格式 25 第七 XML和 (X)HTML 25 第八 RTF文件 25 RTF包 25 创建一个RTF文档 25 不支持的特性 26 RTF中扩展的页眉和页脚 26 第三部分 iText的高级应用 27 第九 字体 27 TrueType字体应用 27 TruType字体集合的应用 28 第十 图象和文本的绝对位置 28 pdfContentByte 28 简单图形 29 文本 29 模板(Form xObjects) 30 分栏 31 PdfTable 32 颜色(SpotColors)和图案(Patterns) 33 第十一 本地和异地转向、目标和概要 33 本地转向 33 异地转向 33 第十二 页面和表格事件 34
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值