资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用.Net怎么上传图片缩略图-创新互联

本篇文章给大家分享的是有关使用.Net怎么上传图片缩略图,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联是一家集网站建设,安宁企业网站建设,安宁品牌网站建设,网站定制,安宁网站建设报价,网络营销,网络优化,安宁网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
/// 
    /// 生成缩略图或质量压缩
    /// 
    /// 源图路径(物理路径)
    /// 缩略图路径(物理路径)
    /// 缩略图宽度,如果宽度为0则不缩略
    /// 缩略图高度,如果高度为0则不缩略
    /// 生成缩略图的方式,默认为空,为空则不缩略高宽[HW 指定高宽缩放(不变形);W 指定宽,高按比例;H 指定高,宽按比例;CUT 指定高宽裁减(不变形)] 
    /// 压缩质量(数字越小压缩率越高)1-100
    /// 压缩后图片的较大大小,0为不限制大小
    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();
        }
      }
    }

以上就是使用.Net怎么上传图片缩略图,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章标题:使用.Net怎么上传图片缩略图-创新互联
标题URL:http://cdkjz.cn/article/dcgpoc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220