简体   繁体   中英

Compress Image faster than Bitmap.save C#

I have a project that using split bitmap 10000x10000 pixel to 100x100 pixel, then I compress it to png. My code:

    Bitmap bitmap = new Bitmap("file name");
    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            Bitmap cropped = bitmap.Clone(new Rectangle(i*100, j*100, 100, 100), bitmap.PixelFormat);
            MemoryStream ms = new MemoryStream();
            crop.Save(ms, ImageFormat.Png);
            Bitmap NewSmallBitmap = new Bitmap(ms);
            //work something with new bitmap
        }
    }

But it run very slowly - about 1ms for a loop, and 10 seconds for all loop. Do anyone have a solution with beter performance? Thanks

You should look to use Tasks;

TaskFactory.StartNew allows you to start a task, and you can call it several times. Each call returns a Task object; save that in a list or array.

Once you've started all your jobs, call Task.WaitAll(Task[]) to wait for all the jobs to finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM