当前位置:编程学习 > C#/ASP.NET >>

C#从数据库中提取图片并显示在PictureBox中

Byte[] imgbt = (Byte[])dt.Rows[0]["photo"];
            if (imgbt.Length != 0)
            {
                MemoryStream stream = new MemoryStream();
                stream.Write(imgbt, 0, imgbt.Length);
                Bitmap bitmap = new Bitmap(stream);
            }
Byte[] imgbt = (Byte[])dt.Rows[0]["photo"];已经查出来了,是正确的
跟踪代码到   Bitmap bmpt = new Bitmap(ms);这一步时出错,说参数无效
我在数据库中查看了,photo下的值显示为<二进制数据>,应该没错吧 --------------------编程问答-------------------- 你数据库中应该存图片路径! --------------------编程问答--------------------    Byte[] imgbt = (Byte[])dt.Rows[0]["photo"];
  if (imgbt.Length != 0)
  {
    MemoryStream stream = new MemoryStream(imgbt);
    Image img = Image.FromStream(stream );
    pictureBox.Image = img;
    stream.close();
  }
 
--------------------编程问答-------------------- 楼上正解! --------------------编程问答-------------------- --------------------编程问答--------------------


datagridview cellpainting事件代码:
if (e.RowIndex != -1)
            {
                using
                    (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor)
                    )
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 清除单元格
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // 画 Grid 边线(仅画单元格的底边线和右边线)
                        //   如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
                        if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
                        dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
                        e.Value.ToString())
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);
                        // 画右边线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom);

                        // 画(填写)单元格内容,相同的内容的单元格只填写第一个
                        if (e.Value != null)
                        {
                            if (e.RowIndex > 0 &&
                            dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
                            e.Value.ToString())
                            { }
                            else
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                    Brushes.Black, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 5, StringFormat.GenericDefault);
                            }
                        }
                        e.Handled = true;
                    }
                }
            }


效果图如上~~ 
   不是我想要的,这种方法是对所有相同单元格进行合并,很不规范,我想实现同代码下,相同单元格才合并,比如1013,1027,它们记分都为0,但是不能合并它们     
    不知道我说的清楚不,  麻烦指点下~怎么实现?


--------------------编程问答-------------------- 发错咯~ --------------------编程问答--------------------
引用 2 楼  的回复:
   Byte[] imgbt = (Byte[])dt.Rows[0]["photo"];
  if (imgbt.Length != 0)
  {
    MemoryStream stream = new MemoryStream(imgbt);
    Image img = Image.FromStream(stream );
    pictureBox.Image = i……
还是出现同样的问题,还是参数无效 --------------------编程问答-------------------- 我一般都是把byte[]写到临时文件里,
再Image.From(string path)
来调用的 --------------------编程问答-------------------- Image img = Image.FromStream(new MemoryStream(imgbt));

pictureBox.Image = img; --------------------编程问答-------------------- /// <summary>
        /// 将图片从数据库中取出
        /// </summary>
        /// <param name="strEmployeeID"></param>
        /// <param name="pb"></param>
        public static void GetImage(string strEmployeeID, PictureBox pb)
        {
            byte[] imageBytes = null;
            SqlConnection conn = DBClass.GetConnection();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select photo from tb_employee where employeeID=" + strEmployeeID, conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                imageBytes = (byte[])sdr.GetValue(0);
            }
            cmd.Dispose();
            sdr.Close();
            conn.Close();
            if (imageBytes.Length != 0)
            {
                MemoryStream ms = new MemoryStream(imageBytes);
                Image img = Image.FromStream(ms);
                //Bitmap bmp = new Bitmap(ms);
                pb.Image = img;
                ms.Close();
            }
        } --------------------编程问答-------------------- 楼主有时间,还是把数据库中的图片提取出来只存路径吧,这样搞没意思 --------------------编程问答-------------------- 数据库里面是怎么建的。 --------------------编程问答-------------------- byte太长了,数据库读取换个长点的变量,不要byte[]。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,