Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. 'Screen Capture' Performance Improvement Suggestions...

'Screen Capture' Performance Improvement Suggestions...

Scheduled Pinned Locked Moved C#
graphicssysadminperformancecssasp-net
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trapper Hell
    wrote on last edited by
    #1

    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:

    L L 2 Replies Last reply
    0
    • T Trapper Hell

      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:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      try this: [^]

      T 1 Reply Last reply
      0
      • L Lost User

        try this: [^]

        T Offline
        T Offline
        Trapper Hell
        wrote on last edited by
        #3

        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%.

        1 Reply Last reply
        0
        • T Trapper Hell

          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:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          T 1 Reply Last reply
          0
          • L Luc Pattyn

            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.


            T Offline
            T Offline
            Trapper Hell
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups