【发布时间】:2011-12-11 16:52:04
【问题描述】:
我想编写一个例程,从服务器(远程桌面 - 类似)接收一些 jpeg 帧,将它们转换为位图图像,然后在 Windows 窗体上显示它们。我试图使例程尽可能轻松,但也许我做错了,因为我总是收到 System.OutOfMemoryException。我的代码如下:
编辑:添加了与此异常相关的部分
private void WatcherRoutine()
{
Boolean lLoopEnd = false;
Bitmap lCurrent = null;
//Graphics lGraphics = null;
Image lImg = null;
BinaryReader lBRVideo = new BinaryReader(this._state.Video.GetStream());
while (lLoopEnd == false)
{
try
{
// Reads frame type
switch (lBRVideo.ReadByte())
{
// Frame received is a big frame (ie a desktop screenshot)
case Constants.BIGFRAME:
{
// Reads frame size in bytes
Int32 lVideoLength = lBRVideo.ReadInt32();
if (lVideoLength > 0)
{
// Stores frame in a stream
MemoryStream ms = new MemoryStream(lBRVideo.ReadBytes(lVideoLength));
// Creates image from stream
lImg = Image.FromStream(ms);
ms.Dispose();
// Creates bitmap from image
lCurrent = new Bitmap(lImg);
lImg.Dispose();
// Passes image to windows form to display it
this.Invoke(this._state.dUpdateVideo, lCurrent);
////lGraphics = Graphics.FromImage(lImg);
//lGraphics.Dispose();
}
}
break;
// Diff frame (ie a small part of desktop that has changed)
// Commenting this part makes the exception disappear :|
case Constants.DIFFFRAME:
{
Int16 lX = lBRVideo.ReadInt16(),
lY = lBRVideo.ReadInt16();
Int32 lVideoLength = lBRVideo.ReadInt32();
if (lVideoLength > 0)
{
//Byte[] lVideoImg = lBRVideo.ReadBytes(lVideoLength);
//Image lImgDiff = Image.FromStream(new MemoryStream(lVideoImg));
////if(lGraphics != null)
//{
// lGraphics.DrawImage(lImgDiff, lX, lY);
// this.Invoke(this._state.dUpdateVideo, new Bitmap(lImg));
//}
}
}
break;
case Constants.CURSOR:
{
Int16 lX = lBRVideo.ReadInt16(),
lY = lBRVideo.ReadInt16();
// TODO
}
break;
default:
break;
}
}
catch (Exception e)
{
if (this._state.WorkEnd == false)
{
this._state.WorkEnd = true;
this.BeginInvoke(this._state.dDisconnect);
}
lLoopEnd = true;
SmartDebug.DWL(e.Message);
}
}
}
dUpdateVideo 是一个包含这个小例程的委托。也许我要释放 pBmp?
private void UpdateVideo(Bitmap pBmp)
{
this.VideoPictureBox.Image = pBmp;
}
【问题讨论】:
-
不查看是否释放 lCurrent 使用的内存
-
请提供dUpdateVideo的来源,也可能是内存泄漏的来源。检查您是否在 lVideoLength 中收到正确的数据(由于某些错误,它可能是非常大的数字)
-
我认为这是您的二进制协议的问题,并且视频长度有些混乱(请参阅 lBRVideo.ReadInt16 和 ReadInt32 调用您注释掉)
-
现在我会检查一下 :) 编辑:我发现了一个错误..现在我会尝试看看它是否相关..
-
它是相关的..如果你添加一个答案我会给你另一点,因为它是问题的一部分:) 谢谢你
标签: c# windows image bitmap out-of-memory