CSDN博客

img smile2me27

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

发表于2004/7/6 15:37:00  2345人阅读

分类: WEB Develop

很多网站为了自己的图片版权,就需要给上载的图片加上版权所有的标志,这个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
 }
}

0 0

相关博文

我的热门文章

img
取 消
img