当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net实现生成缩略图及给原始图加水印的方法示例

asp.net实现生成缩略图及给原始图加水印的方法示例

2017年12月01日  | 移动技术网IT编程  | 我要评论

美丽女尸,漯河市政府网,中国最大的门户网站

本文实例讲述了asp.net实现生成缩略图及给原始图加水印的方法。分享给大家供大家参考,具体如下:

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;
}

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net操作json技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网