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

 更新时间:2017年10月09日 11:57:24   作者:happymagic  
这篇文章主要介绍了asp.net实现生成缩略图及给原始图加水印的方法,结合具体实例形式分析了asp.net图片的缩略图与水印操作相关实现技巧,需要的朋友可以参考下

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

相关文章

  • 网页WEB打印控件制作

    网页WEB打印控件制作

    这篇文章主要介绍了网页WEB打印控件制作 的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Asp.Net URL重写的具体实现

    Asp.Net URL重写的具体实现

    这篇文章主要介绍了Asp.Net URL重写的具体实现,有需要的朋友可以参考一下
    2014-01-01
  • .NET微信公众号客服接口

    .NET微信公众号客服接口

    这篇文章主要为大家详细介绍了.NET微信公众号客服接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Elasticsearch.Net使用入门教程(1)

    Elasticsearch.Net使用入门教程(1)

    这篇文章主要为大家详细介绍了Elasticsearch.Net使用入门教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • .Net Core和RabbitMQ限制循环消费的方法

    .Net Core和RabbitMQ限制循环消费的方法

    当消费者端接收消息处理业务时,如果出现异常或是拒收消息将消息又变更为等待投递再次推送给消费者,这样一来,则形成循环的条件,这篇文章主要介绍了.Net Core和RabbitMQ限制循环消费的方法,需要的朋友可以参考下
    2022-10-10
  • 简单好用的ASP.NET分页类(支持AJAX、自定义文字)

    简单好用的ASP.NET分页类(支持AJAX、自定义文字)

    这篇文章主要介绍了简单好用的ASP.NET分页类(支持AJAX、自定义文字),本文直接给出实现代码和使用方法,需要的朋友可以参考下
    2015-06-06
  • 透过ashx看浏览器服务器运行本质(图解)

    透过ashx看浏览器服务器运行本质(图解)

    一般处理程序(HttpHandler):是一个实现System.Web.IHttpHandler接口的特殊类。任何一个实现了IHttpHandler接口的类是作为一个外部请求的目标程序的前提,感兴趣的朋友可以了解下或许有所帮助
    2013-01-01
  • 使用Ajax更新ASP.Net MVC项目中的报表对象方法

    使用Ajax更新ASP.Net MVC项目中的报表对象方法

    下面小编就为大家分享一篇使用Ajax更新ASP.Net MVC项目中的报表对象方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • 如何在ASP.NET Core中使用ViewComponent

    如何在ASP.NET Core中使用ViewComponent

    这篇文章主要介绍了如何在ASP.NET Core中使用ViewComponent,帮助大家更好的理解和学习使用.net技术,感兴趣的朋友可以了解下
    2021-04-04
  • ASP.NET窗体身份验证详解

    ASP.NET窗体身份验证详解

    这篇文章主要介绍了ASP.NET窗体身份验证,感兴趣的小伙伴可以参考一下
    2015-09-09

最新评论