欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net生成缩略图及给原始图加水印的函数】,下面是详细的分享!
asp.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;
}
以上所分享的是关于asp.net生成缩略图及给原始图加水印的函数,下面是编辑为你推荐的有价值的用户互动:
相关问题:谁有 ASP.NET 上传头像 裁剪 生成 缩略图的 demo?...
答:这是个.net MVC的,希望是你要的 http://www.cnblogs.com/hinton/archive/2012/03/01/2375465.html >>详细
相关问题:asp.net 生成缩略图的最好解决方案
答:矢量图永远不失真 >>详细
相关问题:asp.net 上传文件能不要原图,直接生成缩略图吗
答:SaveAs前生成缩略图就可以了啊 Image.GetThumbnailImage 方法 还有其他很多方法都可以 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
