2014.2.23 datagridview显示图片的方法

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 { }

    }

posted on 2016-10-15 18:28  mol1995  阅读(235)  评论(0编辑  收藏  举报

导航