C#Image.FromStream无效参数 [英] C# Image.FromStream Invalid Parameter

查看:261
本文介绍了C#Image.FromStream无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端/服务器设置,当客户端发送屏幕消息时/服务器截取屏幕截图,将其转换为byte [](数组)并将其发送给我的客户端.接收到字节数组后,客户端使用Image.FromStream将其转换为流,并转换回图像文件,并将其显示在图片框中.测试此图片框时,它会显示图片顶部的大约1英寸,而其下方的所有内容都变为深灰色,然后出现一个错误,提示参数无效".我希望图片框显示整个图像.我的代码如下.

I have a client/server setup that when the client sends the message of screen/ the server takes a screenshot, converts it to a byte[] (array) and sends it to my client. Upon recieving the byte array the client converts it to a stream and back to an image file using Image.FromStream and displays it in a picturebox. When testing this the picturebox shows about and inch of the top of the picture while everything under it becomes a dark grey and then it gives me an error saying "Parameter is not valid". I would like for the picture box to show the entire image. My code is below.

public void UpdateArray(byte[] array)  // update array of bytes
{
    MemoryStream ms = new MemoryStream(array);
    Image returnImage = Image.FromStream(ms);  // <-- Parameter Error comes from here.
    pictureBox1.Image = returnImage;
}


谢谢:)


Thank you :)

推荐答案

问题在于您将数据加载到字节数组中.显示您在其中加载的内容,我相信我们会为您提供帮助.应该是保存到文件中的图像数据(因此,包括标头信息等).
The problem is with the data you load into the byte array. Show what you load into that and I''m sure we can help you. It should be the image data as it would be saved to a file (so, includes header information and such).


感谢您的回复.下面显示了我用于将图像加载到服务器端的字节数组中的代码.
Thanks for the reply. The code I used to load the image into a byte array on the server side is shown below.
if (str == "screen/")
                {
                    int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
                    int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
                    Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
                    Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
                    gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
                    {
                        try
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                bmpScreenShot.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                byte[] array = ms.ToArray();
                                server.Send(array);
                            }
                        }


如果我在单个Windows窗体中都使用这些相同的代码段,而无需通过套接字发送给客户端,则可以正常工作.因此,我认为这与服务器到客户端之间的传输有关.帮助非常有用.谢谢.


If I use those same code snippets all in a single windows form without sending through a socket and to a client it works fine. So im assuming it has something to do with the transfer between server to client. Help is greatly appriciated. Thank you.


听起来像是您的网络代码存在问题.在传输过程中,您没有获得所有数据的可能性更大.这是很常见的.这将使它仅显示图像的一部分,因为只有部分数据通过导线显示.检查数组的长度,这应该确认(数组比您期望的要短).
Sounds like the problem is with your network code. More than likely, you are not getting all of the data during the transfer. This is pretty common. This would make it so only part of your image gets displayed, because only part of the data made it across the wire. Check the length of the array and that should confirm this (the array will be shorter than you expect).


这篇关于C#Image.FromStream无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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