zoukankan      html  css  js  c++  java
  • C#剪切生成高质量缩放图片

    /// <summary>
            /// 高质量缩放图片
            /// </summary>
            /// <param name="OriginFilePath">源图的路径</param>
            /// <param name="TargetFilePath">存储缩略图的路径</param>
            /// <param name="DestWidth">缩放后图片宽度</param>
            /// <param name="DestHeight">缩放后图片高度</param>
            /// <returns>表明此次操作是否成功</returns>
            public static bool ToSuolue(System.IO.Stream oStream, string TargetFilePath, int towidth, int toheight)
            {
                //System.Drawing.Image OriginImage = System.Drawing.Image.FromFile(OriginFilePath);
                System.Drawing.Image OriginImage = System.Drawing.Image.FromStream(oStream);
                System.Drawing.Imaging.ImageFormat thisFormat = OriginImage.RawFormat;
                //按比例缩放
                int x = 0, y = 0;
                int ow = OriginImage.Width;
                int oh = OriginImage.Height;
    
                if ((double)ow / (double)oh > (double)towidth / (double)toheight)
                {
                    oh = OriginImage.Height;
                    ow = OriginImage.Height * towidth / toheight;
                    y = 0;
                    x = (OriginImage.Width - ow) / 2;
                }
                else
                {
                    ow = OriginImage.Width;
                    oh = OriginImage.Width * toheight / towidth;
                    x = 0;
                    y = (OriginImage.Height - oh) / 2;
                }
    
                Bitmap bt = new Bitmap(towidth, toheight); //根据指定大小创建Bitmap实例
                using (Graphics g = Graphics.FromImage(bt))
                {
                    g.Clear(Color.White);
                    //设置画布的描绘质量
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(OriginImage, new Rectangle(0, 0, towidth, toheight),
                        new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);
                    g.Dispose();
                }
    
                System.Drawing.Imaging.EncoderParameters EncoderParams = new System.Drawing.Imaging.EncoderParameters(); //取得内置的编码器
                long[] Quality = new long[1];
                Quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter EncoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality);
                EncoderParams.Param[0] = EncoderParam;
    
                //try
                //{
                    //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象
                    System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                    System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
                    for (int i = 0; i < arrayICI.Length; i++)
                    {
                        if (arrayICI[i].FormatDescription.Equals("JPEG"))
                        {
                            jpegICI = arrayICI[i]; //设置为JPEG编码方式
                            break;
                        }
                    }
    
                    if (jpegICI != null) //保存缩略图
                    {
                        bt.Save(TargetFilePath, jpegICI, EncoderParams);
                    }
                    else
                    {
                        bt.Save(TargetFilePath, thisFormat);
                    }
                    OriginImage.Dispose();
                    return true;
                //}
                //catch (System.Runtime.InteropServices.ExternalException e1)  //GDI+发生一般错误
                //{
                //    return false;
                //}
                //catch (Exception e2)
                //{
                //    return false;
                //}
            }
  • 相关阅读:
    selenium+python自动化86-Chrome正在受到自动软件的控制
    python笔记6-%u60A0和u60a0类似unicode解码
    百度网页搜索部
    百度:替换和清除空格
    百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
    WLLCM这五个字母全排列数目
    r个有标志的球放进n个不同的盒子里,要求无一空盒,问有多少种不同的分配方案?
    从某一日期开始过day天的日期
    求从1到500的整数中能被3和5整除但不能被7整除的数的个数
    红、黄、蓝三色的球各8个,从中取出9个,要求每种颜色的球至少一个,问有多少种不同的取法?
  • 原文地址:https://www.cnblogs.com/paste/p/4191273.html
Copyright © 2011-2022 走看看