How can I send an Image over a socket connection?
-
I wish to know, if there is any way to send an image from one form to another using a socket conection. Say like one form can send an image to another form on a remote computer that recieves it and views it. Can it be done just using sockets? Thanks in advanced :)
-
I wish to know, if there is any way to send an image from one form to another using a socket conection. Say like one form can send an image to another form on a remote computer that recieves it and views it. Can it be done just using sockets? Thanks in advanced :)
One of the overloads for the Image.Save() method takes a System.IO.Stream. You could stream it memory, then send those bits via socket. Something like... using System.IO; using System.Net.Sockets; Bitmap bmp = new Bitmap(@"c:\mypic.gif"); System.IO.MemoryStream memImage = new MemoryStream(); bmp.Save(memImage,System.Drawing.Imaging.ImageFormat.Gif); byte[] data = memImage.ToArray(); Socket socket = new Socket(...your options here...); socket.Send(data,0,data.Length,SocketFlags.None); Hope that gets you started! Michael Developer, Author, Chef
-
One of the overloads for the Image.Save() method takes a System.IO.Stream. You could stream it memory, then send those bits via socket. Something like... using System.IO; using System.Net.Sockets; Bitmap bmp = new Bitmap(@"c:\mypic.gif"); System.IO.MemoryStream memImage = new MemoryStream(); bmp.Save(memImage,System.Drawing.Imaging.ImageFormat.Gif); byte[] data = memImage.ToArray(); Socket socket = new Socket(...your options here...); socket.Send(data,0,data.Length,SocketFlags.None); Hope that gets you started! Michael Developer, Author, Chef
Michael's suggestion is correct but you will also have to make sure that the client and server both send/receive the entire image. This means either putting a delimiter at the end of the image data or sending a 4 byte 'image data size' value before the image data. This is a basic socket principle so you don't not send or not receive the entire application level message.