Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
hello guys ... i am trying to send image converted to Base46String to Server ..

After converting the image to bytes then converting bytes to Base46String i am trying to write Base46String to packet to send it to server but i am getting freeze at

C#
Writer.WriteString(ImageString, 4, Buffer);


and here is my codes :

C#
public byte[] ToArray()
{
Buffer = new byte[ImageString.Length + 4];
Writer.WriteUInt16(1003, 0, Buffer);
Writer.WriteUInt16((ushort)Buffer.Length, 4, Buffer);
Writer.WriteString(ImageString, 4, Buffer);
return Buffer;
}


C#
static string ImageToBase46String(Systim.Drawing.Image image)
{
     using (MemoryStream ms = new MemoryStream())
     {
      image.Save(ms, System.Drawing.Imaging.ImageFormat.jpeg);
      byte[] imagebytes = ms.ToArray();
      string base46string = Convert.ToBase46String(imagebytes);
      return base46string;
      }
}


any idea about why i am getting it freeze while writing image string to packet ?
Posted
Comments
[no name] 16-Jan-15 15:08pm    
I did not dive into your code, but whether you send an encoded Image or anything else, I think the Problem is "simply" the communication part...you can check this by sending a text file and see whether same Problem occurs.
Zoltán Zörgő 16-Jan-15 15:12pm    
1) What type is Writer object?
2) Why aren't you sending in binarry format, what's the advantage of Base64 string? It will be larger.
Sergey Alexandrovich Kryukov 16-Jan-15 15:50pm    
Okay, base64 now. But why? I remember that your past question was about images. They use binary streams; and base64 would take extra time and considerably more bandwidth, so why?
—SA
Sergey Alexandrovich Kryukov 16-Jan-15 15:56pm    
Look, you should use clipboard and not just type code here. It's not Base46, it's ToBase64String...
—SA

1 solution

Please see my comment to the question. As Zoltán Zörgő and I suggested: always use binary streams to send images. Just always.

You did not show how your ImageToBase46String and ToArray work together, and how you use the stream on the other side, how you deserialize it. Anyway, the only proper way of getting the bytes to send them over the network is this:
C#
System.Text.Encoding encoding = // some encoding...
byte[] bytesToSend = encoding.GetBytes("some string");

What encoding to take? It depends. The most important thing is to use the same on the other end of your network channel. But base64 (why? why?!) uses only ASCII, so it would be save to do
C#
System.Drawing.Image myImage = //...
byte[] bytesToSend =
   System.Text.Encoding.ASCII.GetBytes(
       ImageToBase46String(myImage));

—SA
 
Share this answer
 
v3

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