SoFunction
Updated on 2025-03-07

Specific methods to reduce photos to upload them to various spaces using C#

I usually rarely upload photos and other things that girls like to play with, but occasionally I still have to pass it on. Why? Because I rarely contact all kinds of former friends and classmates now, but as long as I post a Weibo or diary with a personal photo, I will attract all kinds of flowers and eggs.

I went to Xiyong camping with a few classmates on the weekend. It was a pity to share it without uploading photos on such a beautiful beach. However, the photos taken by the cameras nowadays are very large, especially the SLRs, and our network bandwidth is so pitiful, so compressing first and then uploading is a very good choice. However, it is too troublesome to compress so many photos one by one (I am a novice in making pictures and don’t know how to use picture making tools). But we are coders, so coders have to use the coder’s method, so I want to make a program.

OK, so much nonsense has started construction.

The first iteration begins, and the demo of the compression of single photos is completed first. I always believe that good code is refactored, so here I will use the idea of ​​iterative development to complete this program step by step. Let's look at the code for the first iteration

Copy the codeThe code is as follows:

static void Main(string[] args)
        {
string savePath = @"E:\2013 Photo\Before Upload\";
string sourcePath = @"E:\2013 Photo\QQ Space Usage\";
            string sourceName = "DSC_0216.JPG";
            int scale = 5;

            Image img = (sourcePath + sourceName);
            int width = / scale;
            int height = / scale;

            Image imgNew = new Bitmap(width, height);
            Graphics g = (imgNew);
            (img, new (0, 0, width, height),
            new (0, 0, , ),
            );

            (savePath + "", );
        }


This ensures that this code can already process a single photo.

The second iteration is refactored.

After analysis, the implementation of this function requires two steps. The first is to obtain the collection of images to be reduced, and the second is to reduce the logic of the image, so the following refactored code is obtained.

Copy the codeThe code is as follows:

class Program
    {
        static readonly string[] IMAGE_EXTNAME = new string[] { "jpg"};
        static void Main(string[] args)
        {
string savePath = @"E:\2013 Photo\Before Upload\";
string sourcePath = @"E:\2013 Photo\QQ Space Usage\";
            int scale = 5;

            List<string> imageNames = GetImageNames(sourcePath);
            if (imageNames != null && > 0)
            {
                foreach (var item in imageNames)
                {
                    SaveSmallPhoto(sourcePath + item, scale, savePath + item);
                }
            }
        }

        /// <summary>
/// Get all file names in the path that match the specified image suffix
        /// </summary>
/// <param name="path">path</param>
        /// <returns></returns>
        static List<string> GetImageNames(string path)
        {
            string[] fileNames = (path);
            if (fileNames == null || == 0)
            {
                return null;
            }

            List<string> imageNames = new List<string>();
            foreach (var item in fileNames)
            {
                if (ExistsInExtName(item, IMAGE_EXTNAME))
                {
                    ((('\\') + 1));
                }
            }

            return imageNames;
        }

        /// <summary>
/// Determine whether the file name meets the specified suffix name
        /// </summary>
/// <param name="name">File name</param>
/// <param name="extNames">Suffix name that meets the requirements</param>
        /// <returns></returns>
        static bool ExistsInExtName(string name, string[] extNames)
        {
            if ((name) || extNames == null || == 0)
            {
                return false;
            }

            foreach (var item in extNames)
            {
                if (().EndsWith(item))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
///Save the picture in scale
        /// </summary>
/// <param name="fromPath">original image path name</param>
/// <param name="scale">Reduce the scale</param>
/// <param name="toPath">Save path name after shrinking</param>
        static void SaveSmallPhoto(string fromPath, int scale, string toPath)
        {
            int width, height;
            using (Image img = (fromPath))
            {
                width = / scale;
                height = / scale;

                using (Image imgNew = new Bitmap(width, height))
                {
                    using (Graphics g = (imgNew))
                    {
                        (img, new Rectangle(0, 0, width, height),
                            new Rectangle(0, 0, , ), );
                    }

                    (toPath, );
                }
            }
        }
    }


Isn't it very simple? Some path proportion variables used here can be written in the configuration file and then compressed photos later. Just modify the corresponding configuration to easily achieve it.

After that, if you have a better way to compress photos, please share it!