2014.2.23 datagridview显示图片的方法

时间:2022-06-28 01:22:20

DataTable dt = new DataTable();

dt.Columns.Add("", typeof(byte[]));      

dt.Rows.Add(File.ReadAllBytes("F:\\宝宝照片\\2009-04\\P1010591.jpg"));           

du.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

du.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

du.AllowUserToAddRows = false; du.DataSource = dt;

private void dv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.ColumnIndex == 0)

{

try

{

using (MemoryStream oldms = new MemoryStream((byte[])e.Value))

{

Image img = Image.FromStream(oldms);

Bitmap bt = new Bitmap(img, new Size(400, 300));

using (MemoryStream newms = new MemoryStream())

{

bt.Save(newms, System.Drawing.Imaging.ImageFormat.Jpeg);

e.Value = newms.ToArray();

}

}

}

catch { }

}

2014.2.23 datagridview显示图片的方法