日期:2014-05-18 浏览次数:20772 次
// 全局变量,储存图片 private SortedDictionary<string, Image> slist = new SortedDictionary<string, Image>(); private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 显示图片列的索引) { if (e.Value is string) { // 图片路径 var key = (string)e.Value; Image img; // 保存在全局变量中,避免重复加载 if (!slist.TryGetValue(key, out img)) { img = Image.FromFile(key); slist.Add(key, img); } e.Value = img; e.FormattingApplied = true; } } }
------解决方案--------------------
private void demoGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (demoGrid.Columns[e.ColumnIndex].Name.Equals("Image")) { string path = System.Windows.Forms.Application.StartupPath + @"/1.gif"; e.Value = GetImage(path); } } public System.Drawing.Image GetImage(string path) { System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open); System.Drawing.Image result = System.Drawing.Image.FromStream(fs); fs.Close(); return result; }