473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCP Client/Server Image Transfer

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_Clic k(object sender, EventArgs e)
{
IPAddress ipAddress = IPAddress.Parse ("127.0.0.1" );
TcpClient client = new TcpClient();

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

while (client.Connect ed)
{
lblStatus.Text = "Connected. ..";
byte[] bytes = new byte[client.ReceiveB ufferSize];
int i;
if (nNetStream.Can Read)
{
nNetStream.Read (bytes, 0, bytes.Length);

Image returnImage =
Image.FromStrea m(nNetStream); //exception occurs here
pictureBox1.Ima ge = returnImage;
}
else
{
client.Close();
nNetStream.Clos e();
}
}
client.Close();
}
----------------------------------------------------------------------------------------------

server snippet
----------------------------------------------------------------------------------------------
try
{
IPAddress ipAddress =
Dns.Resolve("lo calhost").Addre ssList[0];
TcpListener server = new TcpListener(ipA ddress, 9500);
server.Start();
Console.WriteLi ne("Waiting for client to connect...");

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

while (true)
{
TcpClient client =
server.AcceptTc pClient();
Console.WriteLi ne("Connected") ;
while (client.Connect ed)
{
NetworkStream nStream =
client.GetStrea m();
nStream.Write(b Stream, 0,
bStream.Length) ;
}
}
}
}

}
catch (SocketExceptio n e1)
{
Console.WriteLi ne("SocketExcep tion: " + e1);
}
}
static byte[] ImageToByte(Sys tem.Drawing.Ima ge iImage)
{
MemoryStream mMemoryStream = new MemoryStream();
iImage.Save(mMe moryStream,
System.Drawing. Imaging.ImageFo rmat.Gif);
return mMemoryStream.T oArray();
}
Thanks a lot in advanced,
Clayton

Feb 28 '07 #1
3 20787
Hi,

"Clayton" <cl***********@ gmail.comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
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.
Feb 28 '07 #2

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

"Clayton" <cl***********@ gmail.comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
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.
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.AcceptTc pClient(); 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

Mar 1 '07 #3

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

"Clayton" <cl***********@ gmail.comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
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.
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.AcceptTc pClient(); 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

Mar 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7080
by: Kevin Vogler | last post by:
I have a subdirectory with it's own images folder. All the image paths in the pages in the sub directory are "image/image.jpg" When I Response.Redirect to a page in the subdirectory the images are fine but when I use Server.Transfer all the links are broken. I'd prefer to use the relative paths. Is there a way around this in the...
9
9803
by: Don | last post by:
Does anyone know where I can find a client-side function I can reference from within an HTML/JavaScript web page? I'm currently using a core PHP function to do this, but I'd rather do it on the client-side and not have to upload the large raw image to the server. Thanks in advance for your help. Don ----== Posted via Newsfeeds.Com -...
2
12997
by: Kumar | last post by:
Hi, I am trying to develop FTP client application using c#. Our application functinality should be like this: 1.our client uploads files,folders to our ftp server for every 20 minutes. 2.At the end of operation they will put one confirmation file (say Done.txt) to conirm their upload operation is completed in that cycle.
1
2921
by: Hal | last post by:
My most sincere gratitude to anyone who can help me work around this! I have work that needs to be done in javascript on the client whenever a page is unloaded. To this end, I subscribe to the unload event (client side not server side) of the <body> HTML element through javascript.
5
12487
by: Nate | last post by:
We are attempting to make a request to a web service (we will refer to it as XXXServices) hosted on a Web Logic server from a C# SOAP client. The server responds with a 401 Unauthorized error (that appears in plain text), and causes the client to crash. This C# code has been deployed both as an ASP.NET application and a WinForms app, each of...
2
6978
by: Jay | last post by:
Hi, I have some unique situations where i need to transfer a file from Server to client but without any Open or Save dialog box shown to the user. The file should save itself directly to client machine. I want to transfer word (.doc) file from server to client without open/save dialog box. Transfer of file should be silent and invisible...
5
8220
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the common process of connecting and of the server and client... here is the code i am trying to implement. this code snippet is for message transfer only...
5
1625
by: JX | last post by:
Is it possible to replace the address in client browser for server.transfer with the the real location?
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.