如何在C#中从数据库检索图像 [英] How to retrieve image from database in c#

查看:67
本文介绍了如何在C#中从数据库检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个错误表明我有此代码的无效参数... 谁能告诉我怎么了?我应该将图像分配给clientID.

an error shows saying that I have invalid parameters for this code... can anyone tell me whats wrong? Im supposed to get the image assigned to the clientID.

private void button1_Click(object sender, EventArgs e)
{
        MySqlConnection conn = new MySqlConnection(mycon);
        MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);

        conn.Open();
        MySqlDataReader myReader = null;
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            byte[] imgg = (byte[])(myReader["clientImage"]);
            if (imgg == null)
            {
                pictureBox1.Image = null;
            }
            else
            {
                MemoryStream mstream = new MemoryStream(imgg);
                pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
            }
        }
        conn.Close();
}

推荐答案

这段代码可能派上用场.我已经尝试过了.

This piece of code might come in handy. I have tried it.

byte[] imagedata = (byte [])dataGridView1[4, dataGridView1.SelectedRows[0].Index].Value;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imagedata, 0, imagedata.Length))
            {
                ms.Write(imagedata, 0, imagedata.Length);
                //Set image variable value using memory stream.
                image = Image.FromStream(ms, true );
            }

这篇关于如何在C#中从数据库检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆