SoFunction
Updated on 2025-03-07

Implementation code for scaling C# pictures


using ;
using .Drawing2D;
using ;

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 = (filePath + "/" + fromFileName);
//If the size is not enough, return to save the original image
            if ( < toWidth && < toHeight)
            {
                (filePath + "/" + toFileName);
                ();
                return;
            }

//Get the area where the new image is intercepted from the original image according to the image size
            int x, y, w, h;
            if (toHeight > 0)
            {
                if (toWidth > 0)
                {
                    if ( > toWidth && > toHeight)
                    {
                        w = toWidth;
                        h = toWidth * / ;

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

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

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

            = ;
            = ;

            ();
            (originalImage, new Rectangle(x, y, w, h), 0, 0, , , );

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

            EncoderParameters encoderParams = new EncoderParameters();
            EncoderParameter encoderParam = new EncoderParameter(, quality);
            [0] = encoderParam;
ImageCodecInfo[] arrayICI = ();//Get the ImageCodecInfo object containing information about the built-in image encoding decoder.
            ImageCodecInfo jpegICI = null;
            for (int i = 0; i < ; i++)
            {
                if (arrayICI[i].("JPEG"))
                {
jpegICI = arrayICI[i];//Set JPEG encoding
                    break;
                }
            }
            if (jpegICI != null)
            {

                //((path + "/thumb_" + filename), jpegICI, encoderParams);
                (filePath + "/" + toFileName, jpegICI, encoderParams);
            }

            ();
            ();
            ();
        }

        /// <summary>
/// Simple algorithm for maintaining proportional image scaling
        /// </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>();
// The original width and height are within the specified width and height range, and no treatment will be made.
            if (orgWidth <= spcWidth && orgHeight <= spcHeight)
            {
                size["Width"] = orgWidth;
                size["Height"] = orgHeight;
            }
            else
            {
// Obtain the proportional coefficient
                float w = orgWidth / (float)spcWidth;
                float h = orgHeight / (float)spcHeight;
// The width ratio is greater than the height ratio
                if (w > h)
                {
                    size["Width"] = spcWidth;
                    size["Height"] = (int)(w >= 1 ? (orgHeight / w) : (orgHeight * w));
                }
// The width ratio is less than the height ratio
                else if (w < h)
                {
                    size["Height"] = spcHeight;
                    size["Width"] = (int)(h >= 1 ? (orgWidth / h) : (orgWidth * h));
                }
// Width ratio equals height ratio
                else
                {
                    size["Width"] = spcWidth;
                    size["Height"] = spcHeight;
                }
            }
            return size;
        }
    }
}