C# 网络编程:在TCP客户端服务器中接收动态图像对象中的图像文件
um使用此代码将接收到的图像存储在硬盘内存中C# 网络编程:在TCP客户端服务器中接收动态图像对象中的图像文件,c#,networking,tcp,network-programming,C#,Networking,Tcp,Network Programming,um使用此代码将接收到的图像存储在硬盘内存中 我试图编辑此代码,将接收到的图像文件存储在动态图像对象中,而不在硬盘内存中存储 private void StartReceiving() { try { string hstServer = Dns.GetHostName(); IPAddress ipaLocal ="127.0.0.1"; if (tlsServer
我试图编辑此代码,将接收到的图像文件存储在动态图像对象中,而不在硬盘内存中存储
private void StartReceiving()
{
try
{
string hstServer = Dns.GetHostName();
IPAddress ipaLocal ="127.0.0.1";
if (tlsServer == null)
{
tlsServer = new TcpListener(ipaLocal, Convert.ToInt32(txtPort.Text));
}
tlsServer.Start();
TcpClient tclServer = tlsServer.AcceptTcpClient();
strRemote = tclServer.GetStream();
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
FileName = System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize);
try
{
strLocal = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}catch(Exception exp)
{
strLocal = new FileStream(FileName.Substring(0,5), FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
downBuffer = new byte[2048];
while ((bytesSize = strRemote.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
}
}
finally
{
strLocal.Close();
strRemote.Close();
}
}
在.net 4中使用CopyTo进行编辑
tlsServer.Start();
TcpClient tclServer = tlsServer.AcceptTcpClient();
tclServer.GetStream().CopyTo(ms);
//Get the image out of the stream
Image img = Image.FromStream(ms);
}
finally
{
m = Image.FromStream(strLocal);
strLocal.Close();
strRemote.Close();
}
根据细节:
所以您想将其存储在
图像
对象的内存中?异常:参数无效。
MemoryStream ms = new MemoryStream();
//This call will block until the client disconnects though
tclServer.GetStream().CopyTo(ms);
//Get the image out of the stream
Image img = Image.FromStream(ms);