Bitmap constructor from stream doesnt work

P

pomparoee

Hi everyone.
I'm currently working on a project in c# where the client is a smart
device (wm5). The basic idea is remote access.
here's the code:

private void button1_Click(object sender, EventArgs e)
{
try
{
server = new TcpClient("192.168.1.7", 9050);
}
catch (SocketException se)
{
MessageBox.Show(se.Message+"Unable to connect to
server");
return;
}
//MessageBox.Show("Connection established! =)");
ns = server.GetStream();
while (true)
{
try
{
byte[] imgSize = new byte[4];
ns.Read(imgSize, 0, 4);
int len = BitConverter.ToInt32(imgSize, 0);
byte[] buffer = new byte[len];

ns.Read(buffer, 0, buffer.Length);
using (MemoryStream ms = new MemoryStream(buffer))
{
using (Bitmap res = new Bitmap(ms))
{
pictureBox1.Image = res;
pictureBox1.Invalidate();
}
}
buffer = null;
}
catch(Exception e1)
{
string s=e1.Message;
s=e1.StackTrace;
}

}
}


Anyway, the server sends images (in png format) at 1 fps (just for
testing, wanted to see if lower frequency will work) by first sending
the size and then the image itself. When i transfer a single image it
works fine. when the code specified runs i get an unknown exception
(when i tried using a try block i saw the exception was of the
exception class and had no message) when i construct the bitmap from
the stream.

here's the stacktrace:
StackTrace " at Microsoft.AGL.Common.MISC.HandleAr
(PAL_ERROR ar)\r\n at System.Drawing.Bitmap._InitFromMemoryStream
(MemoryStream mstream)\r\n at System.Drawing.Bitmap..ctor(Stream
stream)\r\n at SmartDeviceProject1.Form1.button1_Click(Object
sender, EventArgs e)\r\n at System.Windows.Forms.Control.OnClick
(EventArgs e)\r\n at System.Windows.Forms.Button.OnClick(EventArgs e)
\r\n at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam,
Int32 lParam)\r\n at System.Windows.Forms.Control._InternalWnProc(WM
wm, Int32 wParam, Int32 lParam)\r\n at
Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n at
System.Windows.Forms.Application.Run(Form fm)\r\n at
SmartDeviceProject1.Program.Main()\r\n" string

anyone have any idea how i may resolve this? Also, it would be nice to
see an open source bitmap streamer sample application, can anyone give
me a reference?
 
J

Jesse Houwing

Hello (e-mail address removed),

I don't have the answer to your problem, but I do have a few siggestions.

First... why do you use a try/catch around the connection creation, without
a try/catch around anything that reads or writes to the TCP connection. Every
call can result in exceptions.

Second... Why not just stream the contents of anything you receive into the
MemoryStream (memorystream.write), that also prevents the need of a buffer
array the exact size of the image. The code you provides looks awkward to
me and isn't really self explanatory.

The TCPClient is IDisposable just as the Bitmap is, why not put a using around
that, (or a try/catch/finally)...

And finally to get this to work, I'd forget the TcpClient for a while and
see if you can get this to work in a local only example. Open a bitmap, write
it to a memorystream, copy it to another memorystream and recreate the bitmap.
That makes for much easier debugging.

I also remember that Bitmap is device dependent and that it's better to use
Image.FromStream/Image.Save to read/write the image to a memorystream for
serialization.

Maybe it helps.

Jesse
Hi everyone.
I'm currently working on a project in c# where the client is a smart
device (wm5). The basic idea is remote access.
here's the code:
private void button1_Click(object sender, EventArgs e)
{
try
{
server = new TcpClient("192.168.1.7", 9050);
}
catch (SocketException se)
{
MessageBox.Show(se.Message+"Unable to connect to
server");
return;
}
//MessageBox.Show("Connection established! =)");
ns = server.GetStream();
while (true)
{
try
{
byte[] imgSize = new byte[4];
ns.Read(imgSize, 0, 4);
int len = BitConverter.ToInt32(imgSize, 0);
byte[] buffer = new byte[len];
ns.Read(buffer, 0, buffer.Length);
using (MemoryStream ms = new MemoryStream(buffer))
{
using (Bitmap res = new Bitmap(ms))
{
pictureBox1.Image = res;
pictureBox1.Invalidate();
}
}
buffer = null;
}
catch(Exception e1)
{
string s=e1.Message;
s=e1.StackTrace;
}
}
}
Anyway, the server sends images (in png format) at 1 fps (just for
testing, wanted to see if lower frequency will work) by first sending
the size and then the image itself. When i transfer a single image it
works fine. when the code specified runs i get an unknown exception
(when i tried using a try block i saw the exception was of the
exception class and had no message) when i construct the bitmap from
the stream.

here's the stacktrace:
StackTrace " at Microsoft.AGL.Common.MISC.HandleAr
(PAL_ERROR ar)\r\n at System.Drawing.Bitmap._InitFromMemoryStream
(MemoryStream mstream)\r\n at System.Drawing.Bitmap..ctor(Stream
stream)\r\n at SmartDeviceProject1.Form1.button1_Click(Object
sender, EventArgs e)\r\n at System.Windows.Forms.Control.OnClick
(EventArgs e)\r\n at System.Windows.Forms.Button.OnClick(EventArgs
e)
\r\n at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam,
Int32 lParam)\r\n at System.Windows.Forms.Control._InternalWnProc(WM
wm, Int32 wParam, Int32 lParam)\r\n at
Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n at
System.Windows.Forms.Application.Run(Form fm)\r\n at
SmartDeviceProject1.Program.Main()\r\n" string
anyone have any idea how i may resolve this? Also, it would be nice to
see an open source bitmap streamer sample application, can anyone give
me a reference?
 
P

pomparoee

The partial try/catch and using blocks are only results of the fact
that the code was rewritten several times, and laziness :).
Im not sure i understand what you meant, do you suggest constructing
the memorystream directly from the networkstream?
I tried constructing the bitmap directly from the networkstream, but
that just made the program stuck.
Ill try using the Image methods, at this point im willing to try
anything.

thanks for your help
 
M

matt

Hi

You should check the return value from the read method call matches
the number of bytes you want, the read call may not read all the
required data in one go so you may need to call read a few times
before creating the bitmap.

HTH

Matt




Hi everyone.
I'm currently working on a project in c# where the client is a smart
device (wm5). The basic idea is remote access.
here's the code:

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                server = new TcpClient("192.168.1.7", 9050);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message+"Unable to connect to
server");
                return;
            }
            //MessageBox.Show("Connection established! =)");
            ns = server.GetStream();
            while (true)
            {
                try
                {
                    byte[] imgSize = new byte[4];
                    ns.Read(imgSize, 0, 4);
                    int len = BitConverter.ToInt32(imgSize, 0);
                    byte[] buffer = new byte[len];

                    ns.Read(buffer, 0, buffer.Length);
                    using (MemoryStream ms = new MemoryStream(buffer))
                    {
                        using (Bitmap res = newBitmap(ms))
                        {
                            pictureBox1.Image= res;
                            pictureBox1.Invalidate();
                        }
                    }
                    buffer = null;
                }
                catch(Exception e1)
                {
                    string s=e1.Message;
                    s=e1.StackTrace;
                }

            }
        }

 Anyway, the server sends images (in png format) at  1 fps (just for
testing, wanted to see if lower frequency will work) by first sending
the size and then the image itself. When i transfer a single image it
works fine. when the code specified runs i get an unknown exception
(when i tried using a try block i saw the exception was of the
exception class and had no message) when i construct the bitmap from
the stream.

here's the stacktrace:
        StackTrace    "   at Microsoft.AGL.Common.MISC.HandleAr
(PAL_ERROR ar)\r\n   at System.Drawing.Bitmap._InitFromMemoryStream
(MemoryStream mstream)\r\n   at System.Drawing.Bitmap..ctor(Stream
stream)\r\n   at SmartDeviceProject1.Form1.button1_Click(Object
sender, EventArgs e)\r\n   at System.Windows.Forms.Control.OnClick
(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)
\r\n   at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam,
Int32 lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM
wm, Int32 wParam, Int32 lParam)\r\n   at
Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n   at
System.Windows.Forms.Application.Run(Form fm)\r\n   at
SmartDeviceProject1.Program.Main()\r\n"    string

anyone have any idea how i may resolve this? Also, it would be nice to
see an open source bitmap streamer sample application, can anyone give
me a reference?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top