将图像加载到DataTable Out of Memory Exception

时间:2013-08-19 09:39:15

标签: c# datatable devexpress

我想在我的XtraGrid(devexpress控件)中显示图像。在显示网格之前,我想将数据加载到数据表中。不幸的是,在加载图像时我总是遇到“内存不足”。我只使用小图像(300 kb)。我发现,当我处于发布模式时,只会发生异常。

这里是我的代码:

private void LoadData(List<string> categories)
    {
        SqlCeConnection cn;

        cn = new SqlCeConnection(ConnectionString);

        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }

        for (int j = 0; j < categories.Count; j++)
        {
            string value = categories[j];

            // only if the connection string is not empty and a connection to the database was possible
            if (ConnectionString != string.Empty)
            {
                if (value == "Alle")
                {
                    DataTable dtAlle = new DataTable("Alle");

                    for (int i = 0; i < data.Count; i++)
                    {
                        dtAlle.Merge(data[i], false, MissingSchemaAction.Add);
                    }

                    this.data.Add(dtAlle);
                }
                else
                {
                    string sql = "select * from " + value;

                    SqlCeCommand cmd = new SqlCeCommand(sql, cn);
                    SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
                    DataTable dt = new DataTable(value);
                    da.Fill(dt);

                    List<string> imagePaths = new List<string>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        imagePaths.Add(Convert.ToString(dt.Rows[i][0]));
                    }

                    dt.Columns.RemoveAt(0);
                    DataColumn col = new DataColumn("Vorschaubild", typeof(Image));
                    dt.Columns.Add(col);
                    dt.Columns["Vorschaubild"].SetOrdinal(0); // einfuegen an erster Stelle

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string imagePath = imagePaths[i];
                        if (imagePath != string.Empty)
                        {
                            FileInfo info = new FileInfo(imagePath);
                            string generatedPath = CheckIfPictureIsInClipboarPathAvailable(clipboardPath, info.Name);

                            if (generatedPath != string.Empty)
                            {
                                using (Image image = Image.FromFile(generatedPath))
                                {
                                    dt.Rows[i].SetField("Vorschaubild", image);
                                    image.Dispose();
                                }
                            }
                            else if (imagePath != string.Empty)
                            {
                                using (Image image = Image.FromFile(imagePath))
                                {
                                    dt.Rows[i].SetField("Vorschaubild", image);
                                    image.Dispose();
                                }
                            }
                        }
                    }

                    this.data.Add(dt);
                }
            }
        }
    }

有什么建议吗? 谢谢, TRO

0 个答案:

没有答案