TCP Client/Server Image Transfer

C

Clayton

Hi all,

I'm trying to send an image using a TCP socket. The client connects to
the server without any problems and start to receive the data. The
problem is when I try to convert the stream to an image using
FromStream() method, I get an OutOfMemory Exception. Can anyone help
me out? Really important!! Here is the code;

client snippet
----------------------------------------------------------------------------------------------
private void btnConnect_Click(object sender, EventArgs e)
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
TcpClient client = new TcpClient();

client.Connect(ipAddress, 9500);
NetworkStream nNetStream = client.GetStream();

while (client.Connected)
{
lblStatus.Text = "Connected...";
byte[] bytes = new byte[client.ReceiveBufferSize];
int i;
if (nNetStream.CanRead)
{
nNetStream.Read(bytes, 0, bytes.Length);

Image returnImage =
Image.FromStream(nNetStream); //exception occurs here
pictureBox1.Image = returnImage;
}
else
{
client.Close();
nNetStream.Close();
}


}
client.Close();
}
----------------------------------------------------------------------------------------------

server snippet
----------------------------------------------------------------------------------------------
try
{
IPAddress ipAddress =
Dns.Resolve("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddress, 9500);
server.Start();
Console.WriteLine("Waiting for client to connect...");

while (true)
{
if (server.Pending())
{
Bitmap tImage = new Bitmap(Image URL goes
here);
byte[] bStream = ImageToByte(tImage);

while (true)
{
TcpClient client =
server.AcceptTcpClient();
Console.WriteLine("Connected");
while (client.Connected)
{
NetworkStream nStream =
client.GetStream();
nStream.Write(bStream, 0,
bStream.Length);
}
}
}
}

}


catch (SocketException e1)
{
Console.WriteLine("SocketException: " + e1);
}
}
static byte[] ImageToByte(System.Drawing.Image iImage)
{
MemoryStream mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream,
System.Drawing.Imaging.ImageFormat.Gif);
return mMemoryStream.ToArray();
}


Thanks a lot in advanced,
Clayton
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Clayton said:
Hi all,

I'm trying to send an image using a TCP socket. The client connects to
the server without any problems and start to receive the data. The
problem is when I try to convert the stream to an image using
FromStream() method, I get an OutOfMemory Exception. Can anyone help

You cannot use FromStream while you are reading the image.

Do this, save the image in a MemoryStream, then after you get ALL the image
data, use FromStream.

Additionally you can save the data in a temp file.
 
C

Clayton

Ignacio said:
Hi,



You cannot use FromStream while you are reading the image.

Do this, save the image in a MemoryStream, then after you get ALL the image
data, use FromStream.

Additionally you can save the data in a temp file.

Ok thanks a lot. I've found another solution and it's working too. But
I've got another problem now, that is, when I'm using the TcpClient
client = server.AcceptTcpClient(); in a windows form it is blocking
all the application (since it is a blocking method). How can I prevent
this? Do I need to use asynch calls? Or use a thread?

Thanks a lot for your help,
Clayton
 
C

Clayton

Ignacio said:
Hi,



You cannot use FromStream while you are reading the image.

Do this, save the image in a MemoryStream, then after you get ALL the image
data, use FromStream.

Additionally you can save the data in a temp file.

Ok thanks a lot. I've found another solution and it's working too. But
I've got another problem now, that is, when I'm using the TcpClient
client = server.AcceptTcpClient(); in a windows form it is blocking
all the application (since it is a blocking method). How can I prevent
this? Do I need to use asynch calls? Or use a thread?

Thanks a lot for your help,
Clayton
 

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