【发布时间】:2014-03-17 18:47:08
【问题描述】:
我有以下代码每秒被调用 4 次并更新网格的背景。当我不处理内存流时,内存输出使用量会缓慢增长和下降。 MemoryStream 有一个 Dispose 函数,但是如果我在 Dispose 源位图后调用它,背景只是白色。
我需要处理流吗?如果我这样做了,我做错了什么?
private void Viewer_OnUpdate(object self, Bitmap sourceBitmap, Int32Rect cropArea)
{
if (sourceBitmap == null)
{
return;
}
this.Dispatcher.BeginInvoke(DispatcherPriority.Render,
new Action(
() =>
{
MemoryStream stream = new MemoryStream();
sourceBitmap.Save(stream, ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
stream.Seek(0, SeekOrigin.Begin);
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
this.GridContainer.Background =
new ImageBrush(new CroppedBitmap(bitmapImage,cropArea));
sourceBitmap.Dispose();
}));
}
注意:我正在调度,因为调用事件总是来自非 UI 线程
【问题讨论】:
标签: c# garbage-collection managed