广告位联系
返回顶部
分享到

.Net 图片缩略图上传通用方法教程

asp.net 来源:互联网搜集 作者:酷站 发布时间:2018-08-05 16:12:09 人浏览
摘要

今天小编给大家带来.Net实现 图片缩略图上传通用方法教程 就直接上代码啦! 代码如下: /// summary /// 生成缩略图或质量压缩 /// /summary /// param name=sourcePath源图路径(物理路径)/param /// param name=targetPath缩略图路径(物理路径)/param /

今天小编给大家带来.Net实现 图片缩略图上传通用方法教程

就直接上代码啦!

代码如下:


/// <summary>
    /// 生成缩略图或质量压缩
    /// </summary>
    /// <param name="sourcePath">源图路径(物理路径)</param>
    /// <param name="targetPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度,如果宽度为0则不缩略</param>
    /// <param name="height">缩略图高度,如果高度为0则不缩略</param>
    /// <param name="mode">生成缩略图的方式,默认为空,为空则不缩略高宽[HW 指定高宽缩放(不变形);W 指定宽,高按比例;H 指定高,宽按比例;CUT 指定高宽裁减(不变形)]</param> 
    /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
    /// <param name="size">压缩后图片的最大大小,0为不限制大小</param>
    public static void MakeThumbnail(string sourcePath, string targetPath, int width = 0, int height = 0, string mode = "", int flag = 100, int size = 0)
    {
      Image sourceImage = null;
      Image bitmap = null;
      Graphics g = null;
      EncoderParameters ep = null;
      EncoderParameter eParam = null;
      try
      {
        sourceImage = Image.FromFile(sourcePath);
        int toWidth = 0;
        if (width > 0)
        {
          toWidth = width;
        }
        else
        {
          toWidth = sourceImage.Width;
        }
        int toHeight = 0;
        if (height > 0)
        {
          toHeight = height;
        }
        else
        {
          toHeight = sourceImage.Height;
        }
        int x = 0;
        int y = 0;
        int ow = sourceImage.Width;
        int oh = sourceImage.Height;
        if (width > 0 && height > 0 && !string.IsNullOrWhiteSpace(mode))
        {
          switch (mode.ToUpper())
          {
            case "HW"://指定高宽缩放(不变形)
              int tempheight = sourceImage.Height * width / sourceImage.Width;
              if (tempheight > height)
              {
                toWidth = sourceImage.Width * height / sourceImage.Height;
              }
              else
              {
                toHeight = sourceImage.Height * width / sourceImage.Width;
              }
              break;
            case "W"://指定宽,高按比例          
              toHeight = sourceImage.Height * width / sourceImage.Width;
              break;
            case "H"://指定高,宽按比例
              toWidth = sourceImage.Width * height / sourceImage.Height;
              break;
            case "CUT"://指定高宽裁减(不变形)        
              if ((double)sourceImage.Width / (double)sourceImage.Height > (double)toWidth / (double)toHeight)
              {
                oh = sourceImage.Height;
                ow = sourceImage.Height * toWidth / toHeight;
                y = 0;
                x = (sourceImage.Width - ow) / 2;
              }
              else
              {
                ow = sourceImage.Width;
                oh = sourceImage.Width * height / toWidth;
                x = 0;
                y = (sourceImage.Height - oh) / 2;
              }
              break;
          }
        }
        //新建一个bmp图片
        bitmap = new Bitmap(toWidth, toHeight);
        //新建一个画板
        g = Graphics.FromImage(bitmap);
        g.CompositingQuality = CompositingQuality.HighQuality;
        //设置高质量插值法
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = SmoothingMode.HighQuality;
        //清空画布并以透明背景色填充
        g.Clear(Color.Transparent);
        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(sourceImage, new Rectangle(0, 0, toWidth, toHeight),
          new Rectangle(x, y, ow, oh),
          GraphicsUnit.Pixel);
        //以下代码为保存图片时,设置压缩质量
        ep = new EncoderParameters();
        long[] qy = new long[1];
        qy[0] = flag;//设置压缩的比例1-100
        eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
        ep.Param[0] = eParam;
        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获取图像编码器的信息
        ImageCodecInfo jpegICIinfo = null;
        for (int i = 0; i < arrayICI.Length; i++)
        {
          if (arrayICI[i].FormatDescription.Equals("JPEG"))
          {
            jpegICIinfo = arrayICI[i];
            break;
          }
        }
        if (jpegICIinfo != null)
        {
          bitmap.Save(targetPath, jpegICIinfo, ep);
          FileInfo fiTarget = new FileInfo(targetPath);
          if (size > 0 && fiTarget.Length > 1024 * size)
          {
            flag = flag - 10;
            MakeThumbnail(sourcePath, targetPath, width, height, mode, flag, size);
          }
        }
        else
        {
          //以jpg格式保存缩略图
          bitmap.Save(targetPath, ImageFormat.Jpeg);
        }
      }
      catch (System.Exception ex)
      {
        throw ex;
      }
      finally
      {
        if (sourceImage != null)
        {
          sourceImage.Dispose();
        }
        if (bitmap != null)
        {
          bitmap.Dispose();
        }
        if (g != null)
        {
          g.Dispose();
        }
        if (ep != null)
        {
          ep.Dispose();
        }
        if (eParam != null)
        {
          eParam.Dispose();
        }
      }
    }

好了,教程就到这了,有需要的自己去试试吧。如有不同意见,欢迎下方留言。


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • ASP.NET MVC使用Identity增删改查用户

    ASP.NET MVC使用Identity增删改查用户
    源码在这里:https://github.com/darrenji/UseIdentityCRUDUserInMVC,本地下载 在VS2013中创建一个MVC项目,用默认的无身份验证作为身份验证机制。 通过
  • WPF实现雷达扫描图的绘制介绍

    WPF实现雷达扫描图的绘制介绍
    实现一个雷达扫描图。 源代码在TK_King/雷达 (gitee.com) https://gitee.com/TK_King/radar,自行下载就好了 制作思路 绘制圆形(或者称之轮) 绘制分割
  • .Net Core之JWT授权介绍

    .Net Core之JWT授权介绍
    JSON Web令牌(JWT)是一个开放标准(RFC 7519),它定义了一种紧凑且自包含的方式,用于在各方之间安全地传输信息作为JSON对象。由于此信息
  • ASP.NET Core使用Middleware设置有条件允许访问路由

    ASP.NET Core使用Middleware设置有条件允许访问路由
    1.简介 有时,我们可能在Web API中包含一些具有调试功能的请求。比如我们上次的文章中为什么ASP.NETCore数据库连接串的值和appsettings.json配的
  • ASP.NET Core使用功能开关控制路由访问操作

    ASP.NET Core使用功能开关控制路由访问操作
    前言: 在前面的文章,我们介绍了使用Middleware有条件地允许访问路由(《ASP.NETCore使用Middleware设置有条件允许访问路由》)。 而对于一些
  • ASP.NET Core使用功能开关控制路由访问操作(续)

    ASP.NET Core使用功能开关控制路由访问操作(续)
    前言: 在前面的文章,我们介绍了? ?使用功能开关控制路由访问??。 但其实我们使用了2个条件做的判断: 1 2 3 4 var isDebugEndpoint = context.Re
  • 详解MediatR的使用
    环境: .NET 5 ASP.NET Core MVC (project) 1. MediatR MediatR .NET中的简单中介者模式实现,一种进程内消息传递机制(无其他外部依赖)。支持以同步或
  • .NET Core 3.0里新的JSON API介绍
    为什么需要新的 JSON API ? JSON.NET 大家都用过,老版本的 ASP.NET Core 也依赖于 JSON.NET 。 然而这个依赖就会引起一些版本问题:例如 ASP .NET
  • Net Core Web Api项目与在NginX下发布的方法
    前言 本文将介绍Net Core的一些基础知识和如何NginX下发布Net Core的WebApi项目。 测试环境 操作系统:windows 10 开发工具:visualstudio 2019 框架:
  • ASP.NET Core中的Http缓存使用
    Http响应缓存可减少客户端或代理对web服务器发出的请求数。响应缓存还减少了web服务器生成响应所需的工作量。响应缓存由Http请求中的he
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计