我在 winform 中有一个 dgv,我想在组合框下拉列表中显示 dgv 图像列中的图像。
我可以将 dgv 中的其他值添加到组合框:
var cbString = dgvLineSymbols.Rows[i].Cells["LINE_NAME"].Value.ToString();
if (cbString != "")
{
cbAllSymbols.Items.Add(dgvLineSymbols.Rows[i].Cells["LINE_NAME"].Value.ToString());
}
如果我尝试添加图像列的值,我会得到将图像描述为位图的文本。
我尝试了很多东西,但没有很多关于这个的信息。
这是我最有希望的尝试,但仍然没有奏效,这也需要加载额外的 dll 并导致我应用程序的其他地方出错。
System.Windows.Controls.Image theImage = new System.Windows.Controls.Image();
BitmapImage bitmap = new BitmapImage();
MemoryStream strm = new MemoryStream();
strm.Write(bytearr, 0, bytearr.Length);
bitmap.BeginInit();
bitmap.StreamSource = strm;
bitmap.EndInit();
theImage.Source = bitmap;
考虑到将图像添加到 dgv 是多么简单,必须有一种更简单的方法来实现这一点:
if (File.Exists(file))
{
Bitmap img;
img = new Bitmap(@"" + file);
dgvLineSymbols.Columns[1].Width = 100;
dgvLineSymbols.Rows[i].Cells[1].Value = img;
}