嗯使用此代码将接收到的图像存储在硬盘内存中
我尝试编辑此代码以将接收到的图像文件存储在动态图像对象中,而不存储在硬盘内存中
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();
}