![](/img/trans.png)
[英]Why isn't the action in BeginInvoke executed when a control is disposed before the event loop starts?
[英]Why isn't image disposed by GC
我正在用C#从OpenCV Emgu获取帧,过了一会儿,内存用完了。 当我手动放置旧框架时,问题消失了。 这种缝隙只有在我从Emgu获得帧时才发生。 例如,如果我从AForge获得帧,则将按预期进行清理。
由于我在CopyToNewBPP中复制了数据并从捕获中处置了帧,因此应释放非托管内存。 当我在图片框中设置新的位图时,不应引用旧框架。
问题是我在理解上缺少什么。
private void button1_Click(object sender, EventArgs e)
{
Capture capture = new Capture("rtsp://192.168.0.89:554/live2.sdp");
Task t = new Task(() =>
{
DateTime last = DateTime.Now;
while (true)
{
var frame = capture.QueryFrame();
if (frame == null)
{
continue;
}
Bitmap bitmap = CopyToNewBPP((Bitmap)frame.Bitmap);
frame.Dispose();
pictureBox1.Invoke(new MethodInvoker(() =>
{
var old = pictureBox1.Image;
pictureBox1.Image = bitmap;
if (old != null)
{
old.Dispose(); // this should be done automatically by GC in my mind
}
}));
frame.Dispose();
}
});
t.Start();
}
public Bitmap CopyToNewBPP(Bitmap orig, PixelFormat format = PixelFormat.Format32bppRgb)
{
Bitmap clone = new Bitmap(orig.Width, orig.Height, format);
using (Graphics gr = Graphics.FromImage(clone))
{
gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
}
return clone;
}
我不知道我的答案是否有帮助
Image Gui程序对记忆舔很重要。
例如代码。
if(pictureBox.Image != null)
{
pictureBox.Image.Dispose();
pictureBox.Image = null;
pictureBox.Image = [New Image];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.