C# Image.FromStream(MemoryStream)中的参数无效
我正在尝试通过网络流发送图像,我有sendData和Getdata函数 当使用Image.FromStream函数时,我总是得到一个无效的参数 这是我的代码: 我从屏幕上获取图片,然后将其转换为字节[] 将其插入我通过网络流发送的内存流C# Image.FromStream(MemoryStream)中的参数无效,c#,image,memorystream,networkstream,C#,Image,Memorystream,Networkstream,我正在尝试通过网络流发送图像,我有sendData和Getdata函数 当使用Image.FromStream函数时,我总是得到一个无效的参数 这是我的代码: 我从屏幕上获取图片,然后将其转换为字节[] 将其插入我通过网络流发送的内存流 private void SendData() { StreamWriter swWriter = new StreamWriter(this._nsClient); // BinaryFormatter bfFo
private void SendData()
{
StreamWriter swWriter = new StreamWriter(this._nsClient);
// BinaryFormatter bfFormater = new BinaryFormatter();
// this method
lock (this._secLocker)
{
while (this._bShareScreen)
{
// Check if you need to send the screen
if (this._bShareScreen)
{
MemoryStream msStream = new MemoryStream();
this._imgScreenSend = new Bitmap(this._imgScreenSend.Width, this._imgScreenSend.Height);
// Send an image code
swWriter.WriteLine(General.IMAGE);
swWriter.Flush();
// Copy image from screen
this._grGraphics.CopyFromScreen(0, 0, 0, 0, this._sizScreenSize);
this._imgScreenSend.Save(msStream, System.Drawing.Imaging.ImageFormat.Jpeg);
msStream.Seek(0, SeekOrigin.Begin);
// Create the pakage
byte[] btPackage = msStream.ToArray();
// Send its langth
swWriter.WriteLine(btPackage.Length.ToString());
swWriter.Flush();
// Send the package
_nsClient.Write(btPackage, 0, btPackage.Length);
_nsClient.Flush();
}
}
}
}
private void ReciveData()
{
StreamReader srReader = new StreamReader(this._nsClient);
string strMsgCode = String.Empty;
bool bContinue = true;
//BinaryFormatter bfFormater = new BinaryFormatter();
DataContractSerializer x = new DataContractSerializer(typeof(Image));
// Lock this method
lock (this._objLocker)
{
while (bContinue)
{
// Get the next msg
strMsgCode = srReader.ReadLine();
// Check code
switch (strMsgCode)
{
case (General.IMAGE):
{
// Read bytearray
int nSize = int.Parse(srReader.ReadLine().ToString());
byte[] btImageStream = new byte[nSize];
this._nsClient.Read(btImageStream, 0, nSize);
// Get the Stream
MemoryStream msImageStream = new MemoryStream(btImageStream, 0, btImageStream.Length);
// Set seek, so we read the image from the begining of the stream
msImageStream.Position = 0;
// Build the image from the stream
this._imgScreenImg = Image.FromStream(msImageStream); // Error Here
部分问题是您使用的WriteLine()在写入结束时添加了Environment.NewLine。当您在另一端调用Read()时,您没有正确处理换行符 您要做的是将()写入流,然后在另一端将其读回 转换为字符串很奇怪 传输图像时,您要做的是发送一个字节数组。您只需发送预期流的长度,然后发送图像本身,然后读取另一端的长度和字节数组 通过线路传输字节数组最基本、最简单的方法是首先发送一个表示数组长度的整数,然后在接收端读取该长度 一旦知道要发送/接收的数据量,就可以在线路上以原始字节数组的形式发送该数组,并读取之前在另一端确定的长度 现在您已经有了原始字节和大小,可以将数组从缓冲区重建为有效的图像对象(或刚刚发送的任何其他二进制格式) 另外,我不确定为什么会有DataContractSerializer。它是原始的二进制数据,而且您已经手动将其序列化为字节,所以这是没有用的 使用套接字和流进行网络编程的一个基本问题是定义协议,因为接收端无法以其他方式知道预期的内容或流何时结束。这就是为什么每个常见协议要么有一个非常严格定义的数据包大小和布局,要么做一些类似于发送长度/数据对的事情,以便接收端知道该做什么 如果您实现了一个非常简单的协议,例如发送一个表示数组长度的整数并在接收端读取一个整数,那么您已经完成了一半的目标。然后,发送方和接收方都同意接下来会发生什么。然后,发送方在线路上发送准确的字节数,接收方在线路上读取准确的字节数,并认为读取已完成。您现在拥有的是接收端原始字节数组的精确副本,然后您可以随意使用它,因为您首先知道该数据是什么 如果您需要一个代码示例,我可以提供一个简单的示例,或者网上有许多示例。尽量简短: Read函数(您使用的)返回一个int,该int表示读取了多少字节,这是返回给您的,以便您可以验证是否收到了所需的所有字节。 比如:
int byteCount=0;
while(byteCount < nSize)
{
int read = this._nsClient.Read(btImageStream, byteCount, nSize-byteCount);
byteCount += read;
}
int字节数=0;
while(字节数
这不是该作业的最佳代码我在写行中发送读行,这是因为我在发送消息“IMG”,因此ReceiveData函数现在将接收图像,我也在写行中发送数组大小,在读行中读取,我在写行中发送数组,在读行中读取数组,我不明白你的意思。谢谢。这看起来确实有帮助,但现在我有一个更严重的问题,write方法没有发送所有的数据,我如何解决这个问题?对不起,想不出write不能发送全部数据的原因。正如您所知,根据MSDN,在网络流上调用flush()是没有用的。