C#如何在没有OutOfMemoryException的情况下批量加载listview上的图像? [英] C# How to bulk load images on a listview without getting OutOfMemoryException?
问题描述
Hello Winforms社区,
我正在尝试在listview中加载超过一千个图像,但我得到了OutOfMemoryException。作为解决方案,我阅读了有关按需加载或启用VirtualMode及其处理程序的信息。我不知道如何将我的代码转换到那个级别...但是我怎么能成功实现
?b
private void btnAddImg_Click(object sender,EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
DialogResult dr = ofd.ShowDialog();
if(dr == DialogResult.OK)
{
foreach(ofd.FileNames中的字符串文件名)
{
Image img = Image.FromFile(filename);
string a = c.ToString();
imageList1.Images.Add(a,img);
var listViewItem = listView1.Items.Add("");
listViewItem.ImageKey = a;
c ++;
}
}
}
$
嗨尼克,
>>我不知道如何改造我的代码虽然...
请参阅以下有关使用带有ListView的VirtualMode的演示:
1,定义一个列表以保存需要加载的数据:
protected List< ListViewItem> CurrentCacheItemsSource
{
get;
私人套装;
}2,通过VirtualMode加载数据:
private void LoadListViewItems(List< ListViewItem> item)
{
listView.Items.Clear();
if(items == null)
{
stripStatusInfo.Text =" Current Record:0" ;;
返回;
}
listView.GridLines = true;
listView.FullRowSelect = true;
listView.View = View.Details;
listView.Scrollable = true;
listView.MultiSelect = false;
listView.HeaderStyle = ColumnHeaderStyle.Clickable;
listView.Visible = true;
listView.VirtualListSize = items.Count; //设置虚拟列表容量大小
listView.VirtualMode = true; //设置虚拟模式
listView.RetrieveVirtualItem + = new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem); //绑定虚拟操作
stripStatusInfo.Text =" Current Record:" + items.Count;
}3, 在RetrieveVirtualItem方法中加载相关数据:
void listView_RetrieveVirtualItem (对象发送者,RetrieveVirtualItemEventArgs e)
{
if(this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count == 0)
{
return;
}
e.Item = this.CurrentCacheItemsSource [e.ItemIndex];
if(e.ItemIndex == this.CurrentCacheItemsSource.Count)
{
this.CurrentCacheItemsSource = null;
}
}有关VirtualMode的更多详情,请参阅以下文件:
ListView.VirtualMode Property。
希望这会有所帮助!
最好的问候,
Stanly
Hello Winforms Community,
I am trying to load more than a thousand images in my listview but Im getting the OutOfMemoryException. As a solution I read about load on demand or enabling VirtualMode and its handlers. I dont know how to transform my code to that level though... how can
I achieve that successfully?
private void btnAddImg_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; DialogResult dr = ofd.ShowDialog(); if(dr == DialogResult.OK) { foreach (string filename in ofd.FileNames) { Image img = Image.FromFile(filename); string a = c.ToString(); imageList1.Images.Add(a, img); var listViewItem = listView1.Items.Add(""); listViewItem.ImageKey = a; c++; } } }
Hi Nick,
>>I dont know how to transform my code to that level though...
Please refer to the following demo about use VirtualMode with ListView:
1, Define a list to save data what need to loaded:
protected List<ListViewItem> CurrentCacheItemsSource { get; private set; }2, Load data through VirtualMode:
private void LoadListViewItems(List<ListViewItem> items) { listView.Items.Clear(); if (items == null) { stripStatusInfo.Text = "Current Record: 0"; return; } listView.GridLines = true; listView.FullRowSelect = true; listView.View = View.Details; listView.Scrollable = true; listView.MultiSelect = false; listView.HeaderStyle = ColumnHeaderStyle.Clickable; listView.Visible = true; listView.VirtualListSize = items.Count; //Set the virtual list capacity size listView.VirtualMode = true; //set virtual mode listView.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem); //Bind the virtual operation stripStatusInfo.Text = "Current Record:" + items.Count; }3, Load the relevant data in the RetrieveVirtualItem method:
void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { if (this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count == 0) { return; } e.Item = this.CurrentCacheItemsSource[e.ItemIndex]; if (e.ItemIndex == this.CurrentCacheItemsSource.Count) { this.CurrentCacheItemsSource = null ; } }More details about VirtualMode, please refer to the following documents:
ListView.VirtualMode Property.
Hope this helps!
Best Regards,
Stanly
这篇关于C#如何在没有OutOfMemoryException的情况下批量加载listview上的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!