asp.net生成缩略图及给原始图加水印和版权信息

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


namespace A8.Watermark
{
 /// <summary>
 ///将上传的图片增加水印及生成缩略图
 /// </summary>
 public class watermark
 {
  /// <summary>
  /// 水印图
  /// </summary>
  public Bitmap LogoImage
  {
   get{return this._logoImage;}
  }
  /// <summary>
  /// 缩略图
  /// </summary>
  public Bitmap ThumbImage
  {
   get{return this._thumbImage;}
  }
  /// <summary>
  /// 叠加在图片上的文字
  /// </summary>
  public string TextImage
  {
   get{return this._textImage;}
   set{this._textImage=value ;}
  }
       /// <summary>
  /// 上传的图片流
  /// </summary>
  public Stream Upstream
  {
   get{ return this._upstream;}
   set{this._upstream = value;}
  }

  public string LogoFile
  {
   get{ return this._logoFile;}
   set{this._logoFile = value;}
  }  
  public int PicWidth
  {
   get{ return this._picwidth;}   
  }


  private Bitmap _logoImage;
  private Bitmap _thumbImage;
  private string _textImage;
  private Stream _upstream;
  
  private string _logoFile;
  private int _picwidth;

        /// <summary>
        /// 生成水印图
        /// </summary>
        /// <param name="txtImage">版权信息</param>
        /// <param name="upstream">上传文件流</param>
        /// <param name="logofile">水印图Logo的绝对路径</param>
  public watermark(string txtImage,Stream upstream,string logofile)
  {
     this.TextImage = txtImage;
     this.Upstream = upstream;
     this._logoFile = logofile;
     AddLogoPic();    
  }
        /// <summary>
        /// 生成水印图和缩略图
        /// </summary>
        /// <param name="txtImage">版权信息</param>
        /// <param name="upstream">上传文件流</param>
        /// <param name="logofile">水印图Logo的绝对路径</param>
        /// <param name="picwidth">缩略图的大小</param>
  public watermark(string txtImage,Stream upstream,string logofile,int picwidth)
  {
   this.TextImage = txtImage;
   this.Upstream = upstream;
   this._logoFile = logofile;
   this._picwidth = picwidth;
   AddLogoPic();
   BuildThumbPic();
  }

        /// <summary>
        /// 加水印
        /// </summary>
  private void AddLogoPic()
  {   
   try
   {
     Image upimage = Image.FromStream(this.Upstream);
     Image simage = Image.FromFile(this.LogoFile);

              //logo上填充文字
              Bitmap bitlogo=new Bitmap(simage);
     Graphics sg= Graphics.FromImage(bitlogo);
     Font textFont = new Font("arial",8);
     RectangleF rectangle = new RectangleF(0,51,simage.Width,14);
     StringFormat StrFormat = new StringFormat();
     StrFormat.Alignment = StringAlignment.Far;

     sg.DrawString(this.TextImage,textFont,new SolidBrush(Color.FromArgb(153, 0, 0, 0)), rectangle,StrFormat);
     SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); //设透明字体
     sg.DrawString(this.TextImage,textFont,semiTransBrush, rectangle,StrFormat);
     sg.Flush();    

     Bitmap bitimage = new Bitmap(upimage);    
     Graphics g = Graphics.FromImage(bitimage);
     g.DrawImage(bitlogo,new Rectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);
     g.Flush();
     this._logoImage =bitimage;
     bitlogo.Dispose();
     upimage.Dispose();
     simage.Dispose();
   
   }
   catch(Exception ex)
   {
     throw ex;
   }
  }
  /// <summary>
  /// 生成缩略图
  /// </summary>
  private void BuildThumbPic()
  {
   try
   { int width,height,newwidth,newheight;
    Image upimage = Image.FromStream(this.Upstream);
    Image simage = Image.FromFile(this.LogoFile);
    width = upimage.Width;
    height = upimage.Height;
    if(width>height)
    {
     newwidth=this.PicWidth;
     newheight =(int)((double)height/(double)width * (double)newwidth);
    }
    else
    {
     newheight=this.PicWidth;
     newwidth=(int)((double)width/(double)height * (double)newheight);   
    }
    if (newwidth <= 0)
     throw new ArgumentOutOfRangeException("width", newwidth, "缩略图片宽度不能为0");
    if (newheight <= 0)
     throw new ArgumentOutOfRangeException("height", newheight, "缩略图片高度不能为0");

    this._thumbImage =new Bitmap(upimage.GetThumbnailImage(newwidth,newheight,null,IntPtr.Zero));
    upimage.Dispose();
    simage.Dispose();
   }
   catch(Exception ex)
   {
      throw ex;
   } 
   
  }

  ~watermark()
  {
   Dispose(false);
  }
  
  public void Dispose()
  {
   GC.SuppressFinalize(this);
   this.Dispose(true);
  }
  protected virtual void Dispose(bool disposing)
  {
   if (disposing) 
   {
    if(this._logoImage!=null)
    this._logoImage.Dispose();
    if(this._thumbImage!=null)
    this._thumbImage.Dispose();
   }
  }

  

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值