当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net上传图片同时生成高清晰缩略图

Asp.Net上传图片同时生成高清晰缩略图

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

yes黑客联盟,锦州公交,台州互联星空棋牌

在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果)

代码如下:

 /// <summary>
  /// asp.net上传图片并生成缩略图
  /// </summary>
  /// <param name="upimage">htmlinputfile控件</param>
  /// <param name="ssavepath">保存的路径,些为相对服务器路径的下的文件夹</param>
  /// <param name="sthumbextension">缩略图的thumb</param>
  /// <param name="intthumbwidth">生成缩略图的宽度</param>
  /// <param name="intthumbheight">生成缩略图的高度</param>
  /// <returns>缩略图名称</returns>
  public string uploadimage(htmlinputfile upimage, string ssavepath, string sthumbextension, int intthumbwidth, int intthumbheight)
  {
   string sthumbfile = "";
   string sfilename = "";  
   if (upimage.postedfile != null)
   {
    httppostedfile myfile = upimage.postedfile;
    int nfilelen = myfile.contentlength;
    if (nfilelen == 0)
     return "没有选择上传图片";   
    //获取upimage选择文件的扩展名
    string extendname = system.io.path.getextension(myfile.filename).tolower();
    //判断是否为图片格式
    if (extendname != ".jpg" && extendname != ".jpge" && extendname != ".gif" && extendname != ".bmp" && extendname != ".png")
     return "图片格式不正确";
    
    byte[] mydata = new byte[nfilelen];
    myfile.inputstream.read(mydata, 0, nfilelen);
    sfilename = system.io.path.getfilename(myfile.filename);
    int file_append = 0;
    //检查当前文件夹下是否有同名图片,有则在文件名+1
    while (system.io.file.exists(system.web.httpcontext.current.server.mappath(ssavepath + sfilename)))
    {
     file_append++;
     sfilename = system.io.path.getfilenamewithoutextension(myfile.filename)
      + file_append.tostring() + extendname;
    }
    system.io.filestream newfile
     = new system.io.filestream(system.web.httpcontext.current.server.mappath(ssavepath + sfilename),
     system.io.filemode.create, system.io.fileaccess.write);
    newfile.write(mydata, 0, mydata.length);
    newfile.close();
    //以上为上传原图
    try
    {
     //原图加载
     using (system.drawing.image sourceimage = system.drawing.image.fromfile(system.web.httpcontext.current.server.mappath(ssavepath + sfilename)))
     {
      //原图宽度和高度
      int width = sourceimage.width;
      int height = sourceimage.height;
      int smallwidth;
      int smallheight;
      //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
      if (((decimal)width) / height <= ((decimal)intthumbwidth) / intthumbheight)
      {
       smallwidth = intthumbwidth;
       smallheight = intthumbwidth * height / width;
      }
      else
      {
       smallwidth = intthumbheight * width / height;
       smallheight = intthumbheight;
      }
      //判断缩略图在当前文件夹下是否同名称文件存在
      file_append = 0;
      sthumbfile = sthumbextension + system.io.path.getfilenamewithoutextension(myfile.filename) + extendname;
      while (system.io.file.exists(system.web.httpcontext.current.server.mappath(ssavepath + sthumbfile)))
      {
       file_append++;
       sthumbfile = sthumbextension + system.io.path.getfilenamewithoutextension(myfile.filename) +
        file_append.tostring() + extendname;
      }
      //缩略图保存的绝对路径
      string smallimagepath = system.web.httpcontext.current.server.mappath(ssavepath) + sthumbfile;
      //新建一个图板,以最小等比例压缩大小绘制原图
      using (system.drawing.image bitmap = new system.drawing.bitmap(smallwidth, smallheight))
      {
       //绘制中间图
       using (system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap))
       {
        //高清,平滑
        g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
        g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
        g.clear(color.black);
        g.drawimage(
        sourceimage,
        new system.drawing.rectangle(0, 0, smallwidth, smallheight),
        new system.drawing.rectangle(0, 0, width, height),
        system.drawing.graphicsunit.pixel
        );
       }
       //新建一个图板,以缩略图大小绘制中间图
       using (system.drawing.image bitmap1 = new system.drawing.bitmap(intthumbwidth, intthumbheight))
       {
      //绘制缩略图 http://www.cnblogs.com/sosoft/
        using (system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap1))
        {
         //高清,平滑
         g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
         g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
         g.clear(color.black);
         int lwidth = (smallwidth - intthumbwidth) / 2;
         int bheight = (smallheight - intthumbheight) / 2;
         g.drawimage(bitmap, new rectangle(0, 0, intthumbwidth, intthumbheight), lwidth, bheight, intthumbwidth, intthumbheight, graphicsunit.pixel);
         g.dispose();
         bitmap1.save(smallimagepath, system.drawing.imaging.imageformat.jpeg);
        }
       }
      }
     }
    }
    catch
    {
     //出错则删除
     system.io.file.delete(system.web.httpcontext.current.server.mappath(ssavepath + sfilename));
     return "图片格式不正确";
    }
    //返回缩略图名称
    return sthumbfile;
   }
   return "没有选择图片";
  }

htmlinputfile控件我想大家都应该知道的,就是input type=file....

下面把调用代码也一起c上来

 <%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2" %>
 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
  <title>图片上传-柯乐义</title>
 </head>
 <body>
  <form id="form1" runat="server">
  <div>  
   <input id="file1" runat="server" type="file" /></div><asp:button id="button1" runat="server" onclick="button1_click" text="button" />
  </form>
 </body>
 </html>

 protected void button1_click(object sender, eventargs e)
  {
   string a = this.uploadimage(this.file1, "upload/", "thumb_", 118, 118); 
  }

这样就会在你的upload文件夹下多出两张图片,一张是原图,一张是缩略图。

提供一个更好的算法,由于没有时间去测试和调试,仅供参考

即,在第一步等比例缩小的时候,可以分多次,即把原图到上面代码的中间图以百分比缩小,

例如:原图为500*500 我要缩略成100*80,上面代码程序会先绘制一张100*100的中间图,再在这图片上绘制100*80的,在绘制100*100中间图之前如果先绘300*300的中间图,再在300*300的基础上再绘100*100然后再绘100*80这样会比我上面的代码效果更好,图片更清晰,即中间图越多,效果越好,大家可以去试试。

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

相关文章:

验证码:
移动技术网