C#内存流图像失真 [英] C# memory stream image distortion

查看:103
本文介绍了C#内存流图像失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的代码项目社区,

尝试使用内存流将图像转换为byte []数组时,图像会失真,这是一个问题.我需要这样做,因为我正在尝试创建MJPEG中继服务器.使用BinaryWriter对象将byte []数组放在网络流上.图像的来源是远程IP摄像机.

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();



输入: http://img233.imageshack.us/i/image2ap.jpg/

输出: http://img594.imageshack.us/i/imagewnh.jpg/

关于发生这种情况的任何想法?

在此先感谢.



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.

推荐答案

我通过使用源网络流中的字节流直接解决了这个问题.不幸的是,我仍然不知道是什么原因导致框架撕裂.
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.


您好,
我认为这与图片的长度有关.我认为您首先通过逐字节发送图像并进行测试来查看结果.
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.


您好,

我不确定,但是您可能需要先将图像另存为BitMap,然后再压缩为JPEG.

不过,这是一个漫长的尝试,对不起,我帮不上什么忙.

劳伦斯.
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.


这篇关于C#内存流图像失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆