当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net生成缩略图及给原始图加水印的函数

ASP.NET
asp.net(c#)网页跳转七种方法小结
完美解决在ModalPopupExtender中使用CalendarExtender时被层遮挡的问题
ASP.NET、SharePoint中另存文件的长文件名被截断的原因及解决办法
查看Json输出的*最方便*的方法 (转)
asp.net 代码隐藏的编码模型
ajaxpro.dll 控件实现异步刷新页面
asp.net DbProviderFactory的使用-示例
一个简单的asp.net 单点登录实现
jQuery+Ajax用户登录功能的实现
asp.net 弹出对话框返回多个值
.NET 中英文混合验证码实现代码
一个完整的ASP.NET 2.0 URL重写方案[翻译]
asp.net关于onpropertychange和oninput事件实现代码
asp.net gridview指定某一列滚动
Equals和==的区别 公共变量和属性的区别小结
asp.net 合并GridView中某列相同信息的行(单元格)
ASP.NET(C#) 定时执行一段代码
asp.net 预防SQL注入攻击之我见
asp.net下将Excel转成XML档的实现代码
asp.net url分页类代码

ASP.NET 中的 asp.net生成缩略图及给原始图加水印的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 63 ::
收藏到网摘: n/a

在编写程序时大家经常遇到给图片生成缩略图以及给原始图片添加水印的问题,下面这个函数便是在.NET中给图片生成缩略图及给原始图片添加水印的。


usingSystem.IO;
usingSystem.Drawing.Imaging;

privatevoidButton1_ServerClick(objectsender,System.EventArgse)
{
Graphicsg=null;
System.Drawing.Imageupimage=null;
System.Drawing.Imagethumimg=null;
System.Drawing.Imagesimage=null;
Bitmapoutputfile=null;
try
{
stringextension=Path.GetExtension(File1.PostedFile.FileName).ToUpper();
stringfilename=DateTime.Now.ToString("yyyyMMddhhmmss");
stringsmallpath=Server.MapPath(".")+"/smallimg/";
stringbigpath=Server.MapPath(".")+"/bigimg/";
intwidth,height,newwidth,newheight;

System.Drawing.Image.GetThumbnailImageAbortcallb=newSystem.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
if(!Directory.Exists(smallpath))
Directory.CreateDirectory(smallpath);
if(!Directory.Exists(bigpath))
Directory.CreateDirectory(bigpath);

Streamupimgfile=File1.PostedFile.InputStream;
stringsimagefile=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=newBitmap(upimage);
g=Graphics.FromImage(outputfile);
g.DrawImage(simage,newRectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);

stringnewpath=bigpath+filename+extension;//原始图路径
stringthumpath=smallpath+filename+extension;//缩略图路径
outputfile.Save(newpath);
thumimg.Save(thumpath);
outputfile.Dispose();

}
catch(Exceptionex)
{
throwex;
}
finally
{
if(g!=null)
g.Dispose();
if(thumimg!=null)
thumimg.Dispose();
if(upimage!=null)
upimage.Dispose();
if(simage!=null)
simage.Dispose();
}
}
publicboolThumbnailCallback()
{
returnfalse;
}