欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-16 23:14:04
复制代码 代码如下:using system.drawing;using system.drawing.drawing2d;using system.drawing.ima...

复制代码 代码如下:

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;
        }
    }
}