0

To keep things simple, I decided to follow this approach. Server when launched and clicked on the Start button, captures desktop every sec and saves an image file. Client when launched and clicked on the View Desktop button, reads the image into a picturebox every 500 millisec.

But I've a problem displaying image in the Client form. It displays the initial image but not any other subsequent images, just stays still.

I've tried Refresh() and Invalidate() but nothing works. Can you guys help me solve this

Server Code

using System;
using System.Windows.Forms;
using System.IO;
using System.Timers;

namespace SMSServer
{
    public partial class SMSServerForm : Form
    {
        private static System.Timers.Timer txtTimer;
        private CaptureDesktop cd;
        public SMSServerForm()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
           cd = new CaptureDesktop();
           txtTimer  = new System.Timers.Timer(1000);
           txtTimer.Elapsed += txtTimer_Elapsed;
           txtTimer.Enabled = true;                      
        }

        private void txtTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            cd.CaptureDesktopAsImage();
        }        
    }
}

Client Code

using System;
using System.Windows.Forms;
using System.IO;
using System.Timers;
using System.Drawing;

namespace SMSClient
{
    public partial class SMSClientForm : Form
    {
        private static System.Timers.Timer txtReadTimer;
        public SMSClientForm()
        {
            InitializeComponent();
        }


        private void txtReadTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            desktopPictureBox.Invalidate();
            desktopPictureBox.Image =  Image.FromFile(Directory.GetCurrentDirectory() + @"\CurrentImage.png");
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            txtReadTimer = new System.Timers.Timer(500);
            txtReadTimer.Elapsed += txtReadTimer_Elapsed;
            txtReadTimer.Enabled = true;
        }
    }
}

To capture desktop, I've used the method described here: Capture desktop C# including semitransparent

Community
  • 1
  • 1
kunaguvarun
  • 703
  • 1
  • 10
  • 29

2 Answers2

0

Image.FromFile() locks the file, so your capture program is probably not able to update it on disk. Open Image from file, then release lock?

Here's some code that can be used to read a file and convert it to Image without locking the file. How to convert image in byte array

See in particular the bit at the end where I write:

Bitmap newBitmap = GetImageFromByteArray(File.ReadAllBytes(fileName));
Community
  • 1
  • 1
RenniePet
  • 11,420
  • 7
  • 80
  • 106
-1

Try the method PictureBox.Refresh() (inherited from Control) after you updated the image.

Live
  • 120
  • 9