Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I've written the code below in order to take numbers of images from desktop to make a video with images, it works, but CPU usage is about 80 or 90%. I tried to make it use lower CPU but I couldn't. Is there a way to optimize the code to not use that much of CPU?
My CPU is intel dual core 2200

Please help.

private int num;
private void timer1_Tick(object sender, EventArgs e)
{ 
SendKeys.Send("{PRTSC}");
Image i = Clipboard.GetImage(); 
i.Save("pics\\" + num, System.Drawing.Imaging.ImageFormat.Png); 
num++; 
}



I'v used code below with 100 as timer interval but cpu usage gets over 90% :


C#
int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
b.Save("pics//" + num++);



this is another code that uses 20% of the cpu , its okay,its memory usage is around 20 mb but after 1minute and suddenly it goes over 1.5 GB!!! why is that??!!
code :

C#
Int64 num;
Bitmap[] bit = new Bitmap[200000000];
int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
bit[num++] = b;





Thanks
Posted
Updated 5-Feb-12 22:27pm
v5
Comments
HaBiX 6-Feb-12 6:58am    
If you get 100% cpu usage, that is good, means you can utilize all of the cpu's power, but in your case, you took the wrong way to record a screen. Saving pictures over clipboard is not very efficient.

In your solution memory usage gets high, coz you dont dispose any objects.
mehdi_k 7-Feb-12 12:43pm    
Thanks. may you say which object and how to dispose it? my 3rd code has problem with memory, can you make an example with it?
HaBiX 8-Feb-12 16:07pm    
You're trying to hold all of the pictures in the memory (Bitmap[] bit = new Bitmap[200000000]) - because of this, you'll get out of memory eventually. Its better to save the picture to disk right after grabbing it from screen.


After using Bitmap and Graphics object, call Dispose method to free up the memory, or even better use the "using" keyword, this way, Dispose gets called automatically, when the code goes out of the using block:

using (var b = new Bitmap(w,h)) {
using (var g = Graphics.FromImage(b) {
..... get image
..... save image
}
}
mehdi_k 9-Feb-12 6:01am    
that is exactly my problem. when I try to save them directly to hard disk cpu goes over 90% and when I try to save them in a array , memory usage goes extremely high. it's like I'm moving around a circle. :(
HaBiX 9-Feb-12 6:11am    
You could lower the FPS at which you're "recording".. Add a "Thread.Sleep(500)" after saving image (this way, your program will sleep 0.5 seconds after capture, giving cpu to other processes at that time)

Note: you'll be recording at 2 frames per second

http://kishordgupta.wordpress.com/2011/02/01/step-by-step-to-create-a-desktop-video-recorder-in-c/[^]

Please check this link. I think this will help you

This app will definitely utilize your 100% RAM but use less CPU.

To improve you can use PFX or RX.
 
Share this answer
 
v2
Comments
Espen Harlinn 5-Feb-12 13:47pm    
Some good points there, my 5
[no name] 5-Feb-12 13:52pm    
Thanks
mehdi_k 5-Feb-12 15:12pm    
Thanks
question updated
There is no need to copy the image to the clipboard, the link given by 2irfanshaikh shows a simple way to avoid this.

I also noted that you are saving your image as a png file - this does wonders for the file size, but the compression requires cpu. It's probably where most of cpu time is spent. Try experimenting with other formats.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
LanFanNinja 5-Feb-12 13:55pm    
+5 Very good point.
Espen Harlinn 5-Feb-12 15:05pm    
Thank you, LanFanNinja!
Sergey Alexandrovich Kryukov 5-Feb-12 15:14pm    
Good points, my 5.
--SA
Espen Harlinn 5-Feb-12 15:15pm    
Thank you, Sergey!
mehdi_k 5-Feb-12 15:36pm    
Thank you for answer but no change with changing the formats.
Just to add, your code leaks memory. You should use 'using' statements where-ever an object has a Dispose method, in order to clean up memory automatically. That's both the Graphics and the Bitmap objects.
 
Share this answer
 
Hi,
If your whole purpose is to use less processor in order to keep it free for other processes then you can try using function.

C++
SetThreadAffinityMask


This function takes handle of the thread and bit vector. Pass bit vector set for one core only.
This function will take care that your program will use ONE core only .

I think it will solve your problem in multi-core machine at-least.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900