对于其他的jpg我用bitmap可以正常打开
对于我用filestream新建后又写入了数据的jpg(之后用可以在电脑文件管理器中看到并打开查看,是正确的jpg文件),然而随后我调用了dispose,因为我想再用bitmap直接打开这个文件,然而失败了。并没有报错,只是异步调用的函数一致卡在new Bitmap(filepath)这一句上。。。
求解。
在做一个C#的网络文件传输,客户端发给服务器再由服务器发送给所有在线的客户端。
贴代码
/// <summary>
/// 收到图片信息
/// </summary>
/// <param name="imageMsg">图片消息</param>
public void ReceiveImageMessage( ImageMessage imageMsg )
{
MessageDelegate imageDel = new MessageDelegate(DealImageMessage);
imageDel.BeginInvoke( imageMsg , null , null );
}
private void DealImageMessage( Message msg )
{//中间一大段可以不用看。文件传输没问题。
ImageMessage imageMsg = msg as ImageMessage;
string ip = imageMsg.Ip;
int port = imageMsg.Port; //获取客户端ip和port
string filename = string.Format("{0}{1}{2}{3}{4}{5}{6}",DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second,
Path.GetExtension(imageMsg.ImageName));
string username = imageMsg.UserName;
IPEndPoint end = new IPEndPoint(IPAddress.Parse(ip),port);
TcpClient imageClient = new TcpClient();
imageClient.Connect( end ); //connect向客户端
NetworkStream streamToClient = imageClient.GetStream();
int BufferSize = 1024;
byte[] buffer = new byte[BufferSize];
FileStream outputToImage = new FileStream("../BQTemp/"+filename,FileMode.CreateNew,FileAccess.Write);
int bytesread = 0;
int totalbytes = 0;
do
{
bytesread = streamToClient.Read( buffer , 0 , BufferSize );
totalbytes += bytesread;
outputToImage.Write( buffer , 0 , bytesread );
} while(bytesread > 0); //读取文件
streamToClient.Dispose();
outputToImage.Dispose();
imageClient.Close(); //关闭
BroadImage( username , filename ); //广播图片
}
然后在BroadImage( username , filename );调用了下面这个函数
/// <summary>
/// 打印图片
/// </summary>
/// <param name="imageMsg"></param>
public void ShowImage( ImageMessage imageMsg )
{
string imageName = imageMsg.ImageName;
chatBox.AppendText( imageMsg.UserName + ":\n" );
Clipboard.Clear();
Bitmap bmp = new Bitmap("../Temp/"+imageName);//嗯没错是这一句卡住了
Clipboard.SetImage( bmp );
chatBox.Paste();
Clipboard.Clear();
chatBox.AppendText( "\n" );
}