我想在我的 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);
}
}
}
}
有什么建议么?谢谢,特罗