C#图片按比例缩放的实现代码

 更新时间:2014年01月08日 15:22:41   作者:  
这篇文章主要介绍了C#图片按比例缩放的实现代码,有需要的朋友可以参考一下

复制代码 代码如下:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Publics
{
    public class ImgHelper
    {
        public static void AdjustPhoto(int toWidth, int toHeight, string filePath, string fromFileName, string toFileName, int maxWidth, int maxHeight)
        {
            Image originalImage = Image.FromFile(filePath + "/" + fromFileName);
            //如果尺寸不够返回保存原图
            if (originalImage.Width < toWidth && originalImage.Height < toHeight)
            {
                originalImage.Save(filePath + "/" + toFileName);
                originalImage.Dispose();
                return;
            }

            //根据图片大小获取新图片从原图片截取的区域
            int x, y, w, h;
            if (toHeight > 0)
            {
                if (toWidth > 0)
                {
                    if (originalImage.Width > toWidth && originalImage.Height > toHeight)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;

                        if (h > toHeight)
                        {
                            h = toHeight;
                            w = toHeight * originalImage.Width / originalImage.Height;
                            x = (toWidth - w) / 2;
                            y = 0;
                        }
                        else
                        {
                            x = 0;
                            y = (toHeight - h) / 2;
                        }
                    }
                    else if (originalImage.Width > toWidth)
                    {
                        w = toWidth;
                        h = toWidth * originalImage.Height / originalImage.Width;
                        x = 0;
                        y = (toHeight - h) / 2;
                    }
                    else if (originalImage.Height > toHeight)
                    {
                        h = toHeight;
                        w = toHeight * originalImage.Width / originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = 0;
                    }
                    else
                    {
                        w = originalImage.Width;
                        h = originalImage.Height;
                        x = (toWidth - w) / 2;
                        y = (toHeight - h) / 2;
                    }
                }
                else
                {
                    if (originalImage.Height > maxHeight)
                    {
                        toWidth = toHeight * originalImage.Width / originalImage.Height;
                        x = 0;
                        y = 0;
                        w = toWidth;
                        h = toHeight;

                    }
                    else
                    {
                        x = 0;
                        y = 0;
                        w = originalImage.Width;
                        h = originalImage.Height;
                        toWidth = originalImage.Width;
                        toHeight = originalImage.Height;
                    }
                }
            }
            else
            {
                if (originalImage.Width > maxWidth)
                {
                    toHeight = toWidth * originalImage.Height / originalImage.Width;
                    x = 0;
                    y = 0;
                    w = toWidth;
                    h = toHeight;

                }
                else
                {
                    x = 0;
                    y = 0;
                    w = originalImage.Width;
                    h = originalImage.Height;
                    toWidth = originalImage.Width;
                    toHeight = originalImage.Height;
                }
            }
            Bitmap bm = new Bitmap(toWidth, toHeight);
            Graphics g = Graphics.FromImage(bm);

            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.Clear(Color.White);
            g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);

            long[] quality = new long[1];
            quality[0] = 80;

            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
            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)
            {

                //bm.Save(Server.MapPath(path + "/thumb_" + filename), jpegICI, encoderParams);
                bm.Save(filePath + "/" + toFileName, jpegICI, encoderParams);
            }

            bm.Dispose();
            originalImage.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 保持比例图像缩放简易算法
        /// </summary>
        /// <param name="spcWidth"></param>
        /// <param name="spcHeight"></param>
        /// <param name="orgWidth"></param>
        /// <param name="orgHeight"></param>
        /// <returns></returns>
        public static Dictionary<string, int> AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
        {
            Dictionary<string, int> size = new Dictionary<string, int>();
            // 原始宽高在指定宽高范围内,不作任何处理 
            if (orgWidth <= spcWidth && orgHeight <= spcHeight)
            {
                size["Width"] = orgWidth;
                size["Height"] = orgHeight;
            }
            else
            {
                // 取得比例系数 
                float w = orgWidth / (float)spcWidth;
                float h = orgHeight / (float)spcHeight;
                // 宽度比大于高度比 
                if (w > h)
                {
                    size["Width"] = spcWidth;
                    size["Height"] = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
                }
                // 宽度比小于高度比 
                else if (w < h)
                {
                    size["Height"] = spcHeight;
                    size["Width"] = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
                }
                // 宽度比等于高度比 
                else
                {
                    size["Width"] = spcWidth;
                    size["Height"] = spcHeight;
                }
            }
            return size;
        }
    }
}

相关文章

  • C#中List和数组之间转换的方法

    C#中List和数组之间转换的方法

    这篇文章主要介绍了C#中List和数组之间转换的方法,涉及比较简单的转换技巧,需要的朋友可以参考下
    2015-02-02
  • C#多线程之Thread中Thread.IsAlive属性用法分析

    C#多线程之Thread中Thread.IsAlive属性用法分析

    这篇文章主要介绍了C#多线程之Thread中Thread.IsAlive属性用法,实例分析了C#判断线程可用状态的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#绘制中国国旗的方法

    C#绘制中国国旗的方法

    这篇文章主要介绍了C#绘制中国国旗的方法,以实例形式较为详细的分析了C#图形绘制的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 基于C#动手实现网络服务器Web Server

    基于C#动手实现网络服务器Web Server

    这篇文章主要为大家详细介绍了基于C#动手实现网络服务器Web Server,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • WPF实现倒计时转场动画效果

    WPF实现倒计时转场动画效果

    这篇文章主要介绍了如何利用WPF实现倒计时转场动画效果,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-08-08
  • 详解c# AutoMapper 使用方式

    详解c# AutoMapper 使用方式

    本篇文章主要介绍了详解c# AutoMapper 使用方式 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • 使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

    使用C#实现基于TCP和UDP协议的网络通信程序的基本示例

    这篇文章主要介绍了使用C#实现基于TCP和UDP协议的网络通信程序的示例,文中分别编写了基本的服务器端和客户端,代码十分简单,需要的朋友可以参考下
    2016-04-04
  • C#中Thread(线程)和Task(任务)实例详解

    C#中Thread(线程)和Task(任务)实例详解

    .NET Framework在System.Threading命名空间中具有与线程相关的类,线程是一小组可执行指令,这篇文章主要给大家介绍了关于C#中Thread(线程)和Task(任务)的相关资料,需要的朋友可以参考下
    2022-03-03
  • C#中的两种debug方法介绍

    C#中的两种debug方法介绍

    这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹、利用宏定义两种方法,需要的朋友可以参考下
    2015-02-02
  • 基于Aforge摄像头调用简单实例

    基于Aforge摄像头调用简单实例

    这篇文章主要为大家详细介绍了基于Aforge摄像头调用的简单实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10

最新评论