المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : 'Screen Capture' Performance Improvement Suggestions...



C# Programming
07-17-2009, 11:01 AM
The main question I have here is how to improve CPU usage... Memory Usage is not an issue http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

The Client program connects to the server and sends screen shots (at a rate of 250ms) to the server. This all works well, but I need to use less CPU... The server side program is of no concern either http://www.barakasoft.com/script/Forums/Images/smiley_wink.gif

For brevity purposes, I will simply list what's used. First of all, a TcpClient and a NetworkStream are used. BinaryFormatter serialization is also used to de/serialize objects into byte arrays. The conversion of an object to a string is not quite a choice, since I intend to transfer more objects later on (without having to change much code). A timer with interval of 250ms triggers DoWork method of a BackgroundWorker to take and send a screen shot.

The method used to take the screen shot is:

private Image TakeShot()
{
Bitmap bmpScreenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics grcShotGraphic = Graphics.FromImage(bmpScreenShot);
grcShotGraphic.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

grcShotGraphic.Dispose();
return (Image)bmpScreenShot;
}

I just need to know what's causing such CPU usage and how to improve it. E.g. using Threads instead of BackgroundWorker or a more efficient method to take the screen shot. As I have said, Memory Usage and Network load is of no concern. Furthermore, I know that increasing the interval would result in better performance, but I am looking for something other than that. Current CPU usage ranges between 20-40% on an Intel Core 2 Duo E7300 http://www.barakasoft.com/script/Forums/Images/smiley_sigh.gif