Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
my goal is to generate screenshots in regular interval of time and save it in a folder.

i am generating screenshots using the following code.
C#
private void timer1_Tick(object sender, EventArgs e)
      {
          this.Hide();
          System.Drawing.Bitmap Bitmap;
          Graphics Graps;
          Bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
          Graps = Graphics.FromImage(Bitmap);
          string s = comboBox1.SelectedItem.ToString()+" "+ DateTime.Now.ToString();
          s = s.Replace(':', '.');
          Graps.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
          Bitmap.Save(@"\\192.168.1.10\Temparea\Screenshotseg\" + s + ".Jpeg", ImageFormat.Jpeg);

          //Bitmap.Save(@"Z:\\Screenshotseg\" + s + ".jpeg", ImageFormat.Jpeg);

          //MessageBox.Show(Application.StartupPath.ToString());
          //Bitmap.Save(@"Screenshots\" + s + ".jpeg", ImageFormat.Jpeg);
          //MessageBox.Show("Your Screen Shot Save in Desktop");
          this.Show();
      }


but when each screenshot is generated flickering of the screen occurs.
How can i avoid the flickering?
Posted
Updated 22-Jan-12 19:59pm
v3
Comments
Sergey Alexandrovich Kryukov 23-Jan-12 0:34am    
Just unbelievable! Why doing so? Why?!
--SA
koolprasad2003 23-Jan-12 2:00am    
added <pre> tag only.

This code is just designed to flicker.

If flickers due to Hide/Show and there is nothing you can do. This is not a real flicker though, this is normal rendering of a window when you hide and show it again. Try to run any normally behaving application and quickly hide and show it using the click on the system taskbar. Do you call it "flicker"? No. So, what your application does is not a flicker.

All you can do is re-design your application.

First, I cannot believe you really need to capture a screen periodically. Even if you need so many screen shots, I would assume you only need to make the screen short which are different, when something is changed on the screen. To trigger taking the screenshot, you need to use a global Window Hook. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx[^],
http://support.microsoft.com/kb/318804[^].

Now, you cannot install the global Windows Hook in .NET code. According to Microsoft documentation, it should be a native DLL. It can communicate with your application. Some simple approach could be this: your application can run a polling thread taking the screenshot (by the way, since this moment, forget the idea of writing such application without extensive use of threading). The thread should be kept in the wait state by the call to a event wait handle WaitOne method, and a hook handler should simply set this handle (change it to its signaled state).

This part is not easy to implement at all. The detailed description of it would take a really big CodeProject article. You don't have much luck — it is not interesting enough to waste time on it. Your purpose is not clear, please see above.

Finally, let's address what you called "flicker". Your application does not need UI. Even if by some reason it does, you can minimize its window and never show during the acquisition. Show the window again only when a user restores it explicitly using Alt+TAB, Windows+Tab or taskbar. Yes, as simple as that.

At the end, I would like to tell that I cannot believe making a sequence of screenshots using a timer or a periodic thread with Sleep may make any sense. You really need to share your ultimate goal and explain why would you need to do such a weird thing.

—SA
 
Share this answer
 
Comments
jasen.selvaraj 23-Jan-12 1:32am    
hitnx for ur explanation

my goal is to generate screenshots in regular interval of time and save it in a folder
C#
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
            Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
            Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
            gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
            bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 27-Jan-12 1:37am    
Added pre tag

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