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

asp.net生成缩略图及给原始图加水印

点击次数:24 次 发布日期:2008-11-26 23:50:56 作者:源代码网
源代码网推荐

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;
源代码网推荐}


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华