zoukankan      html  css  js  c++  java
  • 制作等比环切缩缩图

     /// <summary>
        /// 制作等比环切缩缩图
        /// </summary>
    
        public static bool MakeThumbnail(Image image, string savePath, int toWidth, int toHeight)
        {
            if (File.Exists(savePath))
            {
                File.SetAttributes(savePath, FileAttributes.Normal);
                File.Delete(savePath);
            }
    
            if (image == null) return false;
    
            int x = 0;
            int y = 0;
            int ow = image.Width;
            int oh = image.Height;
    
            if ((double)image.Width / image.Height > (double)toWidth / toHeight)
            {
                oh = image.Height;
                ow = image.Height * toWidth / toHeight;
                x = (image.Width - ow) / 2;
            }
            else
            {
                ow = image.Width;
                oh = image.Width * toHeight / toWidth;
                y = (image.Height - oh) / 2;
            }
    
            Bitmap bmPhoto = new Bitmap(toWidth, toHeight, PixelFormat.Format24bppRgb);
            Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
            gbmPhoto.InterpolationMode = InterpolationMode.High;
            gbmPhoto.SmoothingMode = SmoothingMode.HighQuality;
            gbmPhoto.Clear(Color.White);
            gbmPhoto.DrawImage(image, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
    
            ImageCodecInfo myImageCodecInfo;
            Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;
    
            myImageCodecInfo = GetEncoderInfo("image/jpeg");
            myEncoder = Encoder.Quality;
            myEncoderParameters = new EncoderParameters(1);
            myEncoderParameter = new EncoderParameter(myEncoder, 99L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmPhoto.Save(savePath, myImageCodecInfo, myEncoderParameters);
    
            gbmPhoto.Dispose();
            bmPhoto.Dispose();
            return true;
        }
    

      

  • 相关阅读:
    C# 与Sql server 获取数据和执行命令
    关于*.ashx 处理程序调试时 未能创建类型 错误
    winform 利用Http向服务器上传与下载文件
    CSS 使用absolute 是<div>居中
    C# int[,] 和 int[][]
    《Head First JavaScript》 学习笔记
    【单片机】关于头文件
    【单片机】【710】定时器
    【C#】委托
    【C#】关于接口的理解
  • 原文地址:https://www.cnblogs.com/gateluck/p/2938524.html
Copyright © 2011-2022 走看看