'Screen Capture' Performance Improvement Suggestions...
-
The main question I have here is how to improve CPU usage... Memory Usage is not an issue :) 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 ;) 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 :sigh:
-
The main question I have here is how to improve CPU usage... Memory Usage is not an issue :) 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 ;) 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 :sigh:
-
Whilst I would like to thank you for taking the time to read and attempt to resolve the problem, that actually made the situation worse. Since on average, CPU usage increased by another 2%.
-
The main question I have here is how to improve CPU usage... Memory Usage is not an issue :) 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 ;) 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 :sigh:
Hi, there is two parts to my answer: 1. so those five lines of code are executed over and over in some loop. Now which lines do you not really need all the time? I can name three, possibly four, depending on what happens to the resulting bitmap. I guess taking advantage of that would make it less expensive by a factor of I'd say 3. 2. remote PC access utilities normally do NOT transmit the entire screen over and over; instead the current screen is compared with the previous one, and only the differences are transmitted. That would be a medium-complexity challenge as the .NET image classes don't really support that AFAIK. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, there is two parts to my answer: 1. so those five lines of code are executed over and over in some loop. Now which lines do you not really need all the time? I can name three, possibly four, depending on what happens to the resulting bitmap. I guess taking advantage of that would make it less expensive by a factor of I'd say 3. 2. remote PC access utilities normally do NOT transmit the entire screen over and over; instead the current screen is compared with the previous one, and only the differences are transmitted. That would be a medium-complexity challenge as the .NET image classes don't really support that AFAIK. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Thanks about that... I'll check Part 1 you mentioned. However, with regards to Part 2, that would only improve the Server or the network bandwidth, but not the processing, since a lot of time is spent comparing... However, thanks for taking the time to answer... Please provide me with the possibilities of reducing the lines in the ScreenCapture method. Thanks!