|
|
Posted on
2005-12-21 13:15
狂野都城
阅读( 958)
评论()
收藏
举报
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace XdCMS.UI.Image
  {
 /**//// <summary>
///将上传的图片增加水印及生成缩略图
/// </summary>
public class ThumImage
 {
 /**//// <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 ThumImage(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 ThumImage(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
 {
System.Drawing.Image upimage = System.Drawing.Image.FromStream(this.Upstream);
System.Drawing.Image simage = System.Drawing.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;
System.Drawing.Image upimage = System.Drawing.Image.FromStream(this.Upstream);
System.Drawing.Image simage = System.Drawing.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;
}
}

~ThumImage()
 {
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();
}
}


}
}
|