[英]Parameter is not valid - error when sending a bitmap via TCP with C#
I have a file transfer program.我有一个文件传输程序。 The program (Client) does following operations to send a bitmap via TCP socket: get screenshot -> grab Bitmap from memory -> convert to stream -> send
程序(客户端)执行以下操作以通过 TCP 套接字发送 bitmap:获取屏幕截图 ->从 memory 抓取 Bitmap -> 转换为 stream -> 发送
MemoryStream Fs = new MemoryStream();
//////////////11111
Bitmap bmp = TakeScreen();
///////////////2222
//Bitmap bmp = new Bitmap(@"C:\temp\001.bmp");
bmp.Save(Fs, ImageFormat.Bmp);
Byte[] buffer = Fs.ToArray();
Fs.Read(buffer, 0, buffer.Length);
TcpClient socket = new TcpClient("192.168.0.131", 1095);
NetworkStream nw = socket.GetStream();
nw.Write(buffer, 0, buffer.Length);
nw.Close();
Fs.Dispose();
socket.Close();
bmp.Dispose();
If I choose to transfer image directly from memory - no errors.如果我选择直接从 memory 传输图像 - 没有错误。
If I try to load Bitmap from file first - getting "Parameter is not valid" error on a server's side.如果我首先尝试从文件加载 Bitmap - 在服务器端出现“参数无效”错误。
here is the server side:这是服务器端:
NetworkStream Nw = new NetworkStream(handlerSocket.Client);
int thisRead = 0;
int Blocksize = 1024;
Byte[] dataByte = new Byte[Blocksize];
Bitmap screen = getScreen(Nw, dataByte, thisRead, Blocksize);
Nw.Close();
and和
private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
{
Bitmap bitmap;
using (var strm = new MemoryStream())
{
while (true)
{
thisRead = Nw.Read(dataByte, 0, Blocksize);
strm.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
bitmap = new Bitmap(strm); // Error here
}
Bitmap bm3 = new Bitmap(bitmap);
bitmap.Dispose();
return bm3;
}
What's causing this error?是什么导致了这个错误? I guess it has something to do with the MemoryStream.
我想这与 MemoryStream 有关。
edit: simplified the question编辑:简化问题
You nee to seek to the beginning of the stream before you can create the Bitmap
in getScreen
.在 getScreen 中创建
Bitmap
之前,您需要查找到 stream 的getScreen
。
private Bitmap getScreen(NetworkStream Nw, Byte[] dataByte, int thisRead, int Blocksize)
{
Bitmap bitmap;
using (var strm = new MemoryStream())
{
while (true)
{
thisRead = Nw.Read(dataByte, 0, Blocksize);
strm.Write(dataByte, 0, thisRead);
if (thisRead == 0)
break;
}
stream.Seek(0, SeekOrigin.Begin; // <-- Go Back to beginning of stream
bitmap = new Bitmap(strm); // Error here
}
Bitmap bm3 = new Bitmap(bitmap);
bitmap.Dispose();
return bm3;
}
EDIT编辑
Detailed explanation: After writing the last byte to the stream, the stream's current position is a the end of the stream. Creating a Bitmap
from the screen now tries to read the bitmap from the stream, which doesn't work, as there is no more data after the current position (= the end of the stream).详细解释:将最后一个字节写入stream后,流的当前position是stream的结尾。从屏幕创建
Bitmap
现在尝试从stream读取bitmap,因为没有'工作当前 position(= 流的结尾)之后的更多数据。
So what you need to do is tell the stream to set the current position back to the beginning of the stream. Then, the bitmap information can be read.所以你需要做的就是告诉stream把当前的position设置回stream的开头,这样bitmap的信息就可以读取了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.