0

I am creating an application that runs on a timer, and each tick I want to take a screenshot and save it to the disk. This should be running in the background, so I want it to take up as little CPU time as possible. However, when I ran a performance analysis, I found that around 40% of the time was spent in Bitmap.Save and my CPU usage is up to around 10%-15% on average (it's an old computer)

So my question is, is there any way to throttle the image saving process so that Bitmap.Save won't use so much processor time?

I have tried setting Thread.CurrentThread.Priority = ThreadPriority.Lowest; but that did not change much, and neither did using a thread pool.

Here's what I'm doing:

    public static void SaveScreenshot(string path)
    {
        Bitmap bmp = TakeScreenshot();
        Directory.CreateDirectory(Path.GetDirectoryName(path));
        bmp.Save(path);
        bmp.Dispose();
    }
4

1 回答 1

0

You can't change the amount of CPU usage required to save the image (while keeping compression ratio and image type). There should be no problem having the compression use all available CPU as long as the thread has low priority. If there is another thread with higher priority that wants to run, your compression will be throttled automatically.
If you run a heavy calculation in another application with normal priority you should see low CPU usage for the compression.

However image compression is not necessarily CPU bound. It could be I/O bound, that is waiting to read/write from/to disk. This is unfortunately trickier to control than CPU bound tasks.

于 2013-06-05T15:24:18.743 回答