SoFunction
Updated on 2025-03-08

c# Use Grahics to clip images

Used first

Copy the codeThe code is as follows:

/// <summary>
/// Crop the picture
        /// </summary>
        /// <param name="imagePath"/>
        /// <param name="savePath">"c:\images\"</param>
        private List<string> DefClipImage(string imagePath, string savePath)
        {

            var fileInfo = new FileInfo(imagePath);
            if (!)
throw new Exception("Picture" + imagePath + "Not exist!");
            var savePathList = new List<string>();
            var spath = savePath + (, );
            try
            {
                var bitmap = new Bitmap(imagePath);
                var format = ;
                Bitmap cloneBitmap = (_cloneRect1, format);
                var tempPath = spath + "_1.jpg";
                (tempPath);
                (tempPath);
                ();
                cloneBitmap = (_cloneRect2, format);
                tempPath = spath + "_2.jpg";
                (tempPath);
                (tempPath);
                ();
                cloneBitmap = (_cloneRect3, format);
                tempPath = spath + "_3.jpg";
                (tempPath);
                (tempPath);
                ();
                cloneBitmap = (_cloneRect4, format);
                tempPath = spath + "_4.jpg";
                (tempPath);
                (tempPath);
                ();
                ();
                return savePathList;

            }
            catch
            {
throw new Exception("Picture" + imagePath + "Processing failed!");

            }

        }


But the speed is too slow.

Later I found out that using grahics would be much faster

Copy the codeThe code is as follows:

   private void test()
        {

            Bitmap bitmap = new Bitmap( + @"\Image\");
            var bt = new Bitmap(7500, 3750);

            var grahics = (bt);
            (bitmap, _cloneRect1, _cloneRect1,);
            ( + "");

            (bitmap, _cloneRect1, _cloneRect2, );
            ( + "");

            (bitmap, _cloneRect1, _cloneRect3, );
            ( + "");

            (bitmap, _cloneRect1, _cloneRect4, );
            ( + "");

            ();
            ();
        }