Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Dear Code Project community,

I have a problem with images getting distorted when I try and convert them to a byte[] array using a memory stream. I need this to work because I am trying to create an MJPEG relay server. Using a BinaryWriter object to put the byte[] array on the network stream. The source of the images is a remote IP camera.

BinaryWriter bw = new BinaryWriter(clientStream);
MemoryStream ms = new MemoryStream();
                
Jpgs.image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //Image gets distorted here! Jpgs.image is just an image property that returns an image from the buffer array on get{}
byte[] imageData = ms.ToArray(); //byte array that contains distorted image

ms.Flush();
ms.Dispose();

bw.Write(encoder.GetBytes("--BoundaryString\r\n" +
                          "Content-type: image/jpg\r\n" +
                          "Content-Length: " +
                          imageData.Length.ToString() +
                          "\r\n\r\n"));
                
bw.Write(imageData);
bw.Write("\r\n\r\n");
bw.Flush();


input: http://img233.imageshack.us/i/image2ap.jpg/

output: http://img594.imageshack.us/i/imagewnh.jpg/

Any ideas as to why this is happening?

Thanks in advance.
Posted
Updated 15-Feb-11 16:23pm
v4
Comments
Albin Abel 15-Feb-11 5:11am    
Give more details @ the client stream and the encoder you are using
Xcelsion 15-Feb-11 5:28am    
Clientstream is just a networkstream to the client (TcpClient.GetStream()) the encoder is ASCIIEncoding encoder = new ASCIIEncoding();

I solves this problem by using the byte stream from the source network stream directly works great. Unfortunately I still do not know exactly what causes the issue of the frame tearing.
 
Share this answer
 
Hello,
I think it has to do with the lenght of the image. i sugest that you first test by sending your image byte by byte and see the result.
 
Share this answer
 
Comments
Xcelsion 15-Feb-11 5:31am    
If I do Image image tmp = Image.FromStream(ms, true); the image is already distorted so I think the problem isn't with the data transmission.
Hi,

I'm not entirely sure, but you may want to save the image as a BitMap first then compress into JPEG.

That's a long shot though, sorry I can't be more helpful.

Laurence.
 
Share this answer
 
Comments
Xcelsion 15-Feb-11 7:15am    
I tried it and it doesn't seem to work either. I think I have a different idea tough, instead of converting the input stream to images putting them in a buffer and then converting them back to a byte array I might as well just save the stream byte array to a buffer and use that. see if that works. Should shave a little of resource usage as well.
Laurence1234 15-Feb-11 7:21am    
Sorry my suggestion didn't work.
I think that could be a good idea to try, and as you say may reduce your resource usage.
You may want to try some comparisons between your original image and the resultant image. I'm not entirely sure what metric is best to use, as something like Mean Squared Error will only give you a measure of disparity, but it could help you to track down where the image is being altered.

Perhaps write a brief function that will compare your images and after each operation you perform that produces a comparable specimen, call your metric.

Cheers

Laurence.

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