我有windows application form
,我需要显示图片thumbnails
在datagridview
。
图像在SQL Server 2005中存储为二进制数据。
是否有任何方法可以显示图像thumbnails
而无需将图像存储在光盘上。
请尽快回复。
答案 0 :(得分:1)
是的。我假设您将图像存储在字节数组中:
byte[] bArray = GetRawImageBytes();
// Get an image object from the byte-array
Image ddbImage;
using (var ms = new MemoryStream(bArray, 0, bArray.Length))
{
ms.Write(bArray, 0, bArray.Length);
ddbImage = Image.FromStream(ms, true);
}
// Create a 100x100 thumbnail from the image
var ThumbnailSize = new Size(100, 100);
var thumb = ddbImage.GetThumbnailImage(
ThumbnailSize.Height,
ThumbnailSize.Width,
null, IntPtr.Zero);
// Create an image column for the datagridview
var imgColumn = new DataGridViewImageColumn
{
HeaderText = "Image",
Name = "img"
};
dataGridView1.Columns.Add(imgColumn);
// Add the first row, using this image
dataGridView1.Rows.Add();
dataGridView1.Rows[0].Cells[0].Value = thumb;
// lets set the gridview-height to the thumbnail height
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Height = ThumbnailSize.Height;
}
编辑:方便切割&粘贴代码进行测试,这是一个虚函数,它将返回存储在磁盘上的图像的字节数组:
private byte[] GetRawImageBytes()
{
var img = Image.FromFile(PATH_TO_TEST_IMAGE);
byte[] bArray;
using (var ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bArray = ms.GetBuffer();
}
return bArray;
}
答案 1 :(得分:1)
只需添加一个datagridviewimage列的列宽集,并在该循环之后设置data-source到每一行并设置行高。
dgvEmpInfo.DataSource = ds.Tables[0];
foreach (DataGridViewRow dgvRow in dgvEmpInfo.Rows)
{
dgvRow.Height = 100;
}