欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-10 08:06:51
本文实例讲述了asp.net实现生成缩略图及给原始图加水印的方法。分享给大家供大家参考,具体如下: using system.io; using system....

本文实例讲述了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程序设计有所帮助。