Click here to Skip to main content
16,020,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginer in .net programming. I want to know how to transfer image through socket. For example if i want to transfer texts i.e strings through socket, i'll do something like this:

VB
Dim toSend As Byte() = System.Text.Encoding.ASCII.GetBytes("This is a sample text that i want to send")
Dim myNWS As NetworkStream = connectedTCPClient.GetStream()

myNWS.Write(toSend, 0, toSend.Length)

Whereas, if i want to transfer image file through tcpclient networkstream just like that of string, how do i do this?
I mean something like this:
VB
Dim img As Image = Image.FromFile("C:\myIMG.png")


then how do i get the byte encoding of the image so that i can transfer it to external socket.
Something like
VB
Dim imgbyte As Byte() = Encoding.GetBytes(img)

but ASCII encoding type only support strings. How do i turn this image to byte?
Posted
Updated 19-Oct-11 7:17am
v2

It is better to transfer your data as chunks as there is a limit to how much you can send across the tcp connection.

Check my article (in c# I'm afraid but you can compile it and use it in vb.net): WCF Killer[^]
 
Share this answer
 
Thanks mehdi for your quick responce. Your article is too advance above my programming knowledge, remember i'm still a beginer.

You said i'd better transfer it as chuncked. How am i gonna do this? Pls help me. Give me code samples.

Thanks
 
Share this answer
 
Thanks everybody. I found a solution how i can get the bytes of any file including pictures e.t.c.
The method is
Dim imgPath As String = "C:\MyImg.jpg"

Dim bytes As Byte() = IO.File.ReadAllBytes(imgPath)
< /pre>
thanks very much
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900