public void startSendViedo(string IP, int port)
{
try
{
ms = new MemoryStream();
IDataObject data;
Image bmap;
//复制图片到剪贴板
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);
//从剪贴板获取图片并将其转化成bitmap
data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
bmap.Save(ms, ImageFormat.Bmp);
}
picture_out.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arrImage = ms.GetBuffer();
tc = new TcpClient(IP, port);//连接到对方计算机
myns = tc.GetStream();
bw = new BinaryWriter(myns);
bw.Write(arrImage);//发送图片到对方计算机
ms.Flush();
bw.Flush();
myns.Flush();
ms.Close();
bw.Close();
myns.Close();
tc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
timer.Enabled = false;
stopvideo.Enabled = false;
startvideo.Enabled = true;
}
}
断点跟踪,catch与finally块呈现灰白,只执行catch块,finally直接跳过,只好把timer.enable=false放在catch块中处理;
tc = new TcpClient(IP, port);//连接到对方计算机执行到这条语句异味常,跳到catch块中执行,catch运行完没执行finally块,而是运行timer1_Click()这个函数。
在执行关闭前如果发生异常,那么程序将进入catch块而跳过关闭.本回答被提问者采纳