-1
private void Form2_Load(object sender, EventArgs e)
    {
        con1.Open();

        Form1 home = new Form1();
        home.MdiParent = this.MdiParent ;

        System.Timers.Timer timer1 = new System.Timers.Timer();
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)  + @"\\server1\KamalSingh\ScreenCaptures");
        t.Interval  = 500;
        t.Tick += new EventHandler(StartThread);
        t.Start();
     }


   System.Windows.Forms.Timer t = new  System.Windows.Forms.Timer();
   //Thread tt;   

   private static Bitmap bmpscreenshot;
   private static Graphics gfxscreenshot;

    void TakeScreenShot()
    {

        bmpscreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);
        gfxscreenshot = Graphics.FromImage(bmpscreenshot );
        gfxscreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy );
        bmpscreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) +@"\\server1\KamalSingh\ScreenCaptures\.bmp", ImageFormat.Png);
      //  tt.Abort();
    }

     protected  void StartThread(object sender, EventArgs e)
     {
         Thread th  = new Thread(new ThreadStart(TakeScreenShot));
         th.SetApartmentState(ApartmentState.STA );
         th.Start();
         Thread.Sleep(100);
         th.Join();

     }

我想修复在执行程序时发生的这个错误。错误会在一段时间后出现。

4

1 回答 1

0

尝试这个:

void TakeScreenShot()
{

    using(Bitmap bmpscreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb))
    {
        using(Graphics gfxscreenshot = Graphics.FromImage(bmpscreenshot))
        {
            gfxscreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y,0,0,Screen.PrimaryScreen.Bounds.Size,CopyPixelOperation.SourceCopy );
            bmpscreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) +@"\\server1\KamalSingh\ScreenCaptures\.bmp", ImageFormat.Png);
        }
    }
}

希望这可以帮助!

于 2012-09-24T21:32:28.677 回答