ImageList 抛出“内存不足"从 SQL 数据库添加图像时出现异常 [英] ImageList throws "Out of memory" exception when adding images from SQL database
问题描述
我有几个 SQL 数据库,其中有一个使用 varbinary
数据类型存储图像的表.
I have a few SQL databases which has a table that store images using varbinary
datatype.
单击按钮时,它将从 ImageList
控件中删除除前三个图像之外的所有现有图像,并将所有新图像添加到我表单上的 ImageList
.
On button click, it will remove all existing images except the first three images from the ImageList
control and add all new images to the ImageList
on my form.
有一个 TreeView
使用这个 ImageList
.
There is a TreeView
that uses this ImageList
.
我得到了:
内存不足异常
...如果我点击相同数据库上的按钮多次.
...if I click the button many times on the same database.
简化代码:
if(imageList1.Images.Count > 3)
{
for (int i = imageList1.Images.Count - 1; i > 2; i--)
{
imageList1.Images.RemoveAt(i);
}
}
int counter = 0;
foreach (DataRow dr in dataset.Tables[0].Rows)
{
if (dr["ImageField"] != DBNull.Value)
{
byte[] imageData = (byte[])dr["ImageField"];
MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length);
Bitmap img = new Bitmap(ms);
imageList1.Images.Add("Image" + counter, img);
img.Dispose();
ms.Dispose();
counter++;
}
}
堆栈跟踪:
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
at System.Drawing.Font.ToLogFont(Object logFont)
at System.Drawing.Font.ToHfont()
at System.Windows.Forms.Control.FontHandleWrapper..ctor(Font font)
at System.Windows.Forms.OwnerDrawPropertyBag.get_FontHandle()
at System.Windows.Forms.TreeView.CustomDraw(Message& m)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control.EndUpdateInternal(Boolean invalidate)
at System.Windows.Forms.TreeView.ImageListChangedHandle(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.ImageList.OnChangeHandle(EventArgs eventargs)
at System.Windows.Forms.ImageList.ImageCollection.Add(Original original, ImageInfo imageInfo)
at System.Windows.Forms.ImageList.ImageCollection.Add(String key, Image image)
at Test.MyClass.LoadLibraryImageList()
为什么在内存使用率很低的时候会抛出那个异常?
Why does it throw that exception when the memory usage is very low?
推荐答案
看起来 ImageList
只是冰山一角,真正的问题在于我扩展的 MultiSelectTreeView
控制.阅读完这个问题后,我添加了GC.每当我更改
TreeNode
字体和 voila,问题就解决了.
Looks like ImageList
is just the tip of the iceberg, the real problem lays in my extended MultiSelectTreeView
control. After reading this question, I added GC.Collect()
to my code whenever I change the TreeNode
font and voila, problem solved.
根据建议,我还在加载新批次之前处理了 ImageList
中不需要的图像,这似乎也有助于降低我的 GDI 计数.谢谢大家.
From the suggestions, I also dispose unwanted images from my ImageList
before loading a new batch which seems to help lower my GDI counts too. Thanks everyone.
这篇关于ImageList 抛出“内存不足"从 SQL 数据库添加图像时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!