开发者

Attempting to Convert Byte[] into Image... but is there platform issues involved

开发者 https://www.devze.com 2022-12-25 18:08 出处:网络
Greetings, Current, I\'m attempting to develop an application that takes a Byte Array that is streamed to us from a Linux C language program across a TCPClient (stream) and reassemble it back into an

Greetings,

Current, I'm attempting to develop an application that takes a Byte Array that is streamed to us from a Linux C language program across a TCPClient (stream) and reassemble it back into an image/jpg. The "sending" application was developed by a off-site developer who claims that the image reassembles back into an image without any problems or errors in his test environment (all Linux)...

However开发者_Go百科, we are not so fortunate. I (believe) we successfully get all of the data sent, storing it as a string (lets us append the stream until it is complete) and then we convert it back into a Byte[]. This appears to be working fine...

But, when we take the byte[] we get from the streaming (and our string assembly) and try to convert it into an image using the System.Drawing.Image.FromStream() we get errors.... Anyone have any idea what we're doing wrong? Or, does anyone know if this is a cross-platform issue? We're developing our app for Windows XP and C# .net, but the off-site developer did his work in c and Linux... perhaps there's some difference as to how each Operating System Coverts Images into Byte Arrays?

Anyway, here's the code for converting our received ByteArray (from the TCPClient Stream) into an image. This code works when we send an image from a test machine we built that RUNS on XP, but not from the Linux box...

       System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
       byte[] imageBytes = encoding.GetBytes(data);

       MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

       // Convert byte[] to Image
       ms.Write(imageBytes, 0, imageBytes.Length);
       System.Drawing.Image image = System.Drawing.Image.FromStream(ms, false); 
               <-- DIES here, throws a {System.ArgumentException: Parameter is not valid.} error

Any advice, suggestions, theories, or HELP would be GREATLY appreciated! Please let me know???

Best wishes all! Thanks in advance!

Greg


Firstly, if you use a string to hold your binary data then you are converting your byte values into Unicode, and then converting the unicode back into bytes, so you could be corrupting the data. At the very least you're being rather inefficient (in time and space). Ditch the string and the ASCII encoding - write the data bytes directly into a memory stream as you receive them and it will deal with them properly to assemble a binary buffer.

Secondly, you are creating a MemoryStream from the buffer you received. There is no need to then write that data into the MemoryStream, as it's already been copied there by the constructor - i.e. delete the ms.Write() line of your example code.

Finally, depending on how/if you do the two things outlined above, you may need to Seek() the MemoryStream back to the beginning before trying to read the image data out of it, otherwise you'll try to read the image from the end of the stream and find no data there.


The image data is most like sent as a Base64 string. To confirm this you'd need to send the first bit of string data.

If it is base64, this is the way to convert to bytes:

byte[] imageBytes = Convert.FromBase64String(data);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);

Also make sure to flush set MemoryStream.Position to the beginning:

ms.Flush();
ms.Position = 0;
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, false); 
0

精彩评论

暂无评论...
验证码 换一张
取 消