Set an image from a class to a picturebox in a form
<p>I have a class where I'm getting an image from a buffer and the image is set to a variable type image which will hold the image.</p>

<pre><code>class MyClass
{
    public Image MyImage;
    private void ReadingCallBack(IAsyncResult ar)
    {
        Socket CurrentSocket = null;

        try
        {
            CurrentSocket = (Socket)ar.AsyncState;
            int recvsize = CurrentSocket.EndReceive(ar);
            Array.Resize(ref buffer, recvsize);
            string stream = ASCIIEncoding.ASCII.GetString(buffer);
            switch (stream.Substring(stream.IndexOf('[') + 1, stream.IndexOf(']') - 1))
            {
                case "Screenshot":

                    byte[] imgbuff = new byte[buffer.Length - 12];
                    Buffer.BlockCopy(buffer, 12, imgbuff, 0, imgbuff.Length);

                    MemoryStream ms = new MemoryStream(imgbuff);
                    MyImage = Image.FromStream(ms);

                    ms.Close();
                    break;
            }

            buffer = new byte[1024 * 5000];
            CurrentSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReadingCallBack), CurrentSocket);
        }
        catch (Exception er)
        {
            //etc
        }
    }
}
</code></pre>

<p>In a WindowsForm I have the following code in the constructor:
The WindowsForm is called whenever the user wants to.</p>

<pre><code>MyClass Class = new MyClass();
while (Class.MyImage == null)
{
    System.Threading.Thread.Sleep(1);
    Application.DoEvents();
}

pictureBox1.Image = Class.MyImage;
</code></pre>

<p>BUT, the image is never assigned to the picturebox, the image variable is always null.</p>

<p>I've checked if the image from the buffer really exists by creating an image file on my HDD with the bytes from the buffer and the image was successfully created without any issues.</p>

<p>What have I done wrong?</p>
