asp.net生成缩略图及给原始图加水印
2008-02-22 09:31:19来源:互联网 阅读 ()
using System.IO;
using System.Drawing.Imaging;
private void Button1_ServerClick(object sender, System.EventArgs e)
{
Graphics g=null;
System.Drawing.Image upimage=null;
System.Drawing.Image thumimg=null;
System.Drawing.Image simage=null;
Bitmap outputfile=null;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
string smallpath = Server.MapPath(".") "/smallimg/";
string bigpath = Server.MapPath(".") "/bigimg/";
int width,height,newwidth,newheight;
System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
if(!Directory.Exists(smallpath))
Directory.CreateDirectory(smallpath);
if(!Directory.Exists(bigpath))
Directory.CreateDirectory(bigpath);
Stream upimgfile = File1.PostedFile.InputStream;
string simagefile = Server.MapPath("a8logo.jpg"); //要加水印的文件
simage=System.Drawing.Image.FromFile(simagefile);
upimage= System.Drawing.Image.FromStream(upimgfile); //上传的图片
width = upimage.Width;
height = upimage.Height;
if(width>height)
{
newwidth=200;
newheight =(int)((double)height/(double)width * (double)newwidth);
}
else
{
newheight=200;
newwidth=(int)((double)width/(double)height * (double)newheight);
}
thumimg = upimage.GetThumbnailImage(newwidth,newheight,callb,IntPtr.Zero);
outputfile=new Bitmap(upimage);
g=Graphics.FromImage(outputfile);
g.DrawImage(simage,new Rectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);
string newpath = bigpath filename extension; //原始图路径
string thumpath = smallpath filename extension; //缩略图路径
outputfile.Save(newpath);
thumimg.Save(thumpath);
outputfile.Dispose();
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(g!=null)
g.Dispose();
if(thumimg!=null)
thumimg.Dispose();
if(upimage!=null)
upimage.Dispose();
if(simage!=null)
simage.Dispose();
}
}
public bool ThumbnailCallback()
{
return false;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- asp.net生成缩略图及给原始图加水印的函数 2009-05-12
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash