C# 位图图形CreateBitmapSourceFromHBitmap内存泄漏
我想在带有WPF的C# 位图图形CreateBitmapSourceFromHBitmap内存泄漏,c#,.net,wpf,memory-management,memory-leaks,C#,.net,Wpf,Memory Management,Memory Leaks,我想在带有WPF的图像容器中显示位图 private void updateImagePreview() { Bitmap bmp = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); Graphics gr = Graphics.FromImage(bmp); Image Imag
图像
容器中显示位图
private void updateImagePreview()
{
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width,
Screen.PrimaryScreen.WorkingArea.Height);
Graphics gr = Graphics.FromImage(bmp);
Image Image_Preview;
while (true)
{
gr.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(bmp.Width, bmp.Height));
Image_Preview.Source = loadBitmap(bmp);
}
}
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o);
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
IntPtr ip = source.GetHbitmap();
BitmapSource bs = null;
try
{
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(ip);
}
return bs;
}
问题是,这会造成巨大的内存泄漏。泄漏发生在CreateBitmapSourceFromHBitmap
调用上,并在循环时填充内存,直到超出限制。如果我不打那个电话,漏洞就会消失。知道为什么会发生这种情况吗?在返回它之前,请尝试在BitmapSource
上调用Freeze()
。它似乎有助于释放对图像字节的一些引用
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o);
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
IntPtr ip = source.GetHbitmap();
BitmapSource bs = null;
try
{
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(ip);
}
bs.Freeze();
return bs;
}
在返回BitmapSource
之前,请尝试调用Freeze()
。它似乎有助于释放对图像字节的一些引用
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o);
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
IntPtr ip = source.GetHbitmap();
BitmapSource bs = null;
try
{
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(ip);
}
bs.Freeze();
return bs;
}
如果发生内存泄漏,首先要检查是否存在未处理的一次性对象 我可以看出,您没有同时处理和实例,这可能会导致内存泄漏 确保在使用结束时将其处理掉 例如:在
updateImagePreview
方法中
using (Graphics gr = Graphics.FromImage(bmp))
{
....
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
....
finally
{
DeleteObject(ip);
source.Dispose();
}
return bs;
}
例如:在loadBitmap
方法中
using (Graphics gr = Graphics.FromImage(bmp))
{
....
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
....
finally
{
DeleteObject(ip);
source.Dispose();
}
return bs;
}
如果发生内存泄漏,首先要检查是否存在未处理的一次性对象 我可以看出,您没有同时处理和实例,这可能会导致内存泄漏 确保在使用结束时将其处理掉 例如:在
updateImagePreview
方法中
using (Graphics gr = Graphics.FromImage(bmp))
{
....
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
....
finally
{
DeleteObject(ip);
source.Dispose();
}
return bs;
}
例如:在loadBitmap
方法中
using (Graphics gr = Graphics.FromImage(bmp))
{
....
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
....
finally
{
DeleteObject(ip);
source.Dispose();
}
return bs;
}
我不知怎么解决了,但我不知道我是怎么解决这个漏洞的。我假设调用导致线程内存泄漏的代码可以解决这个问题。我强烈建议使用注释中提到的,因为仅使用MemoryStream也会导致内存泄漏。不管怎样,下面的代码不会导致上述内存泄漏,对我来说很有吸引力
Timer takeScreen;
// This is a button to start the screen capturing
private void Button_Play_Click(object sender, RoutedEventArgs e)
{
int fps = 30;
takeScreen = new Timer(o => addNewImage(), null, 0, 1000 / fps);
}
private void addNewImage()
{
using (Bitmap bmp = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(bmp.Width, bmp.Height));
Image_Preview.Dispatcher.Invoke(new Action(() => Image_Preview.Source = loadBitmap(bmp)));
}
}
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
BitmapSource bmpf = null;
using (MemoryStream ms = new MemoryStream())
{
using (WrappingStream ws = new WrappingStream(ms))
{
source.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
bmpf = BitmapFrame.Create(ws, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
return bmpf;
}
我不知怎么解决了,但我不知道我是怎么解决这个漏洞的。我假设调用导致线程内存泄漏的代码可以解决这个问题。我强烈建议使用注释中提到的,因为仅使用MemoryStream也会导致内存泄漏。不管怎样,下面的代码不会导致上述内存泄漏,对我来说很有吸引力
Timer takeScreen;
// This is a button to start the screen capturing
private void Button_Play_Click(object sender, RoutedEventArgs e)
{
int fps = 30;
takeScreen = new Timer(o => addNewImage(), null, 0, 1000 / fps);
}
private void addNewImage()
{
using (Bitmap bmp = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(bmp.Width, bmp.Height));
Image_Preview.Dispatcher.Invoke(new Action(() => Image_Preview.Source = loadBitmap(bmp)));
}
}
}
public BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
BitmapSource bmpf = null;
using (MemoryStream ms = new MemoryStream())
{
using (WrappingStream ws = new WrappingStream(ms))
{
source.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
bmpf = BitmapFrame.Create(ws, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
return bmpf;
}
“内存泄漏”是什么意思?它在应该被销毁后在内存中保留引用?还是一个电话就能填满你的记忆?它会保留参考资料。一个呼叫就可以了,但是内存泄漏仍然存在。你看到了吗?它有很多链接,但没有答案。请尝试另一种方法:将位图保存到MemoryStream,并从该流中解码BitmapImage或BitmapFrame(如StackOverflow上的多篇文章所示)。我尝试了这种方法,包括包装器类,它也在泄漏内存。你说的“内存泄漏”是什么意思?它在应该被销毁后在内存中保留引用?还是一个电话就能填满你的记忆?它会保留参考资料。一个呼叫就可以了,但是内存泄漏仍然存在。你看到了吗?它有很多链接,但没有答案。请尝试另一种方法:将位图保存到MemoryStream,并从该流中解码BitmapImage或BitmapFrame(如StackOverflow上的多篇文章所示)。我尝试了这种方法,包括包装器类,而且内存也在泄漏。