在生成屏幕截图时如何避免闪烁? [英] How can i avoid the flickering while generating screenshots?

查看:670
本文介绍了在生成屏幕截图时如何避免闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的目标是定期生成屏幕截图并将其保存在文件夹中.

我正在使用以下代码生成屏幕截图.

 私有  void  timer1_Tick(对象发​​件人,EventArgs e)
      {
           .Hide();
          System.Drawing.Bitmap位图;
          图形抓图;
          位图= 位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);
          Graps = Graphics.FromImage(Bitmap);
          字符串 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( @"  + s + " ,ImageFormat.Jpeg);

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

          //  MessageBox.Show(Application.StartupPath.ToString()); 
          //  Bitmap.Save(@"Screenshots \" + s +".jpeg",ImageFormat.Jpeg); 跨度>
          //  MessageBox.Show(您的屏幕快照保存在桌面中"); 
           .Show();
      }



但是在生成每个屏幕截图时,都会出现屏幕闪烁.

解决方案

此代码仅用于闪烁.

如果由于Hide/Show而闪烁,则您无能为力.但是,这并不是真正的闪烁,这是您隐藏并再次显示窗口时的正常渲染.尝试运行任何行为正常的应用程序,然后使用系统任务栏上的单击快速隐藏和显示它.您称其为闪烁"吗?否.因此,您的应用程序不会闪烁.

您所能做的就是重新设计您的应用程序.

首先,我无法相信您确实需要定期捕获屏幕.即使您需要这么多的屏幕快照,我也会假设您需要在屏幕上进行一些更改时使屏幕变短.要触发截图,您需要使用一个全局的Window Hook.请参阅:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms632589%28v=vs.85%29.aspx [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms644990%28v=vs.85%29.aspx [ http://support.microsoft.com/kb/318804 [您的应用程序不需要用户界面.即使出于某种原因,您也可以最小化其窗口,并且在获取过程中从不显示.仅当用户使用Alt + TAB,Windows + Tab或任务栏明确还原窗口时,才再次显示该窗口.是的,就这么简单.

最后,我想告诉大家,我无法相信使用计时器或带有Sleep的定期线程制作一系列屏幕截图可能没有任何意义.您确实需要分享您的最终目标并解释为什么您需要做这样一件奇怪的事情.

-SA


  int  screenWidth = Screen.GetBounds(新建点( 0  0 )).宽度;
             int  screenHeight = Screen.GetBounds(新建 Point( 0  0 )).高度;
            位图bmpScreenShot = 位图(screenWidth,screenHeight);
            图形gfx = Graphics.FromImage((Image)bmpScreenShot);
            gfx.CopyFromScreen( 0  0  0  0  Size(screenWidth,screenHeight));
            bmpScreenShot.Save(" ,ImageFormat.Jpeg); 


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.

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?

解决方案

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


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);


这篇关于在生成屏幕截图时如何避免闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆