0

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.

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
        }
    }
}

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

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

pictureBox1.Image = Class.MyImage;

BUT, the image is never assigned to the picturebox, the image variable is always null.

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.

What have I done wrong?

jszigeti
  • 373
  • 4
  • 11
Dan Bursuc
  • 15
  • 3
  • you're given code won't compile. Get your real code with correct `MyClass` definition – Ilya Ivanov May 23 '13 at 17:20
  • I suggest you post **more code**. From that snippets it's even hard to understand the logic of your program... – Adriano Repetti May 23 '13 at 17:25
  • Just a second. I will update the code – Dan Bursuc May 23 '13 at 17:26
  • 1
    There's a lot wrong with this code. You'll need to improve your catch handler so that exceptions don't just fall off the radar. You blindly trust that the first callback supplies the entire image. It won't. You correctly keep reading but you don't know when to stop. You need to know the image size. Only call Image.FromStream() when you got all the bytes. And raise an event instead of that very dangerous DoEvents loop. – Hans Passant May 23 '13 at 19:01

2 Answers2

1

I think you might be suffering from the issue of image memory being freed when it's instantiating object is disposed. Are you trapping errors in that try/catch? I'd think that when that MemoryStream object is disposed, so would be the image it was used to create.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • Have a look at this: http://stackoverflow.com/questions/11741719/saving-image-to-memorystream-generic-gdi-error – DonBoitnott May 23 '13 at 18:29
0

Your image creation in MyClass should be in a method, and then you need to call that method with buffer as argument.

Janez Lukan
  • 1,439
  • 1
  • 12
  • 28