我如何定期显示图片框中的屏幕截图图片? [英] how can i show screenshot picture in picturebox on regular interval ?

查看:71
本文介绍了我如何定期显示图片框中的屏幕截图图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在图片框中显示屏幕截图图片?
如何将这些屏幕截图存储在数据库中?
我想定期在图片框中显示我的图片,我的代码写在下面,有人可以帮我..........?


how can i show screenshot picture in picture box ???
how can store these screenshots in database ??
i want to show my picture in picturebox on regular interval my code is below written can anybody help me ..........?


 private void Form2_Load(object sender, EventArgs e)
        {
            

           con1.Open();

       

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

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

//get the screenshot of the image 


       System.Windows.Forms.Timer t = new  System.Windows.Forms.Timer();
       //Thread tt;   
       int  i;
      
        
       private static Bitmap bmpscreenshot;
       private static Graphics gfxscreenshot;

        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\" + i  + ".jpeg", ImageFormat.Jpeg );
                 }
              
            }
            i++;
            //tt.Abort();
        }

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

            string[] images = Directory.GetFiles(@"\\server1\KamalSingh\ScreenCaptures\", "*jpeg");
            foreach (string image in images)
            {
                pictureBox2.Image = new Bitmap(image);
               // Thread.Sleep(10000);
            }
             imagee = Image.FromFile(images[counter]);
            pictureBox2.Width = imagee.Width;
            pictureBox2.Image = imagee;

            //// move image to new location
            Random rand = new Random();
           
           // pictureBox2.Left = rand.Next(Math.Max(0, Bounds.Width - pictureBox1.Width));
           // pictureBox2.Top = rand.Next(Math.Max(0, Bounds.Height - pictureBox2.Height));
            if (counter < images.Count() - 1)
            {
                counter = counter + 1;
            }
            else
            {
                counter = 0;
            }
        }    

推荐答案

首先要注意的是,您有两个计时器:
The first thing to notice is that you have two timers:
            System.Timers.Timer timer = new System.Timers.Timer();
            Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)  + @"\\server1\KamalSingh\ScreenCaptures");
            t.Interval  = 5000;
            t.Tick += new EventHandler(StartThread);
            t.Start();
         }
 
//get the screenshot of the image 

 
       System.Windows.Forms.Timer t = new  System.Windows.Forms.Timer();

删除第二个,并将第一个移到专用类级别的变量-您希望它在整个表单存在的范围内,而不是局部于表单甚至加载处理程序.这不会对您现有的代码产生任何影响,但是值得您稍后在项目中进行.

其次,为什么要保存在一个文件夹中,然后从另一个文件夹中加载呢?

Delete the second one, and move the first to a private class level variable - you want it in scope for the whole of the forms existance, rather than local to the form Load even handler. That won''t make any difference to your existing code, but it is worth doing for later in your poject.

Secondly, why are you saving in one folder, and loading from another?

bmpscreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + @"\\server1\KamalSingh\ScreenCaptures\" + i  + ".jpeg", ImageFormat.Jpeg );


string[] images = Directory.GetFiles(@"\\server1\KamalSingh\ScreenCaptures\", "*jpeg");

我怀疑如果解决此问题,您的问题可能会消失...


但就我个人而言,我会保留一个图像列表",而不是每次都重新读取文件夹内容并从文件中加载图像-效率更高,并且可以防止在文件正在使用时(由您的应用程序)出现问题因为垃圾回收器尚未处理同一文件的先前图像.

I suspect that if you fix this, your problem might go away...


But personally, I would keep a List of Images instead of re-reading the folder contents and loading the image from the file each time - it''s more efficient, and prevents future problems where the file is in use (by your app) because the Garbage collector has not Disposed of your previous image of the same file yet.


这篇关于我如何定期显示图片框中的屏幕截图图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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