How to retrieve image from database in c#

前端 未结 2 965
滥情空心
滥情空心 2020-12-22 01:02

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.

         


        
相关标签:
2条回答
  • 2020-12-22 01:40

    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 );
                }
    
    0 讨论(0)
  • 2020-12-22 01:48

    this code works. MySql Wamp.

    using System.IO;
    
    string appPath = Path.GetDirectoryName(Application.Executable) + @"/student Images/";
    string imagename;
    
    //put this code below to load form.
    getimage();
    if(File.Exist(appPath + imagename)
    {
    PictureBox1.Image = Image.FromFile(appPath + imagename);
    }
    else
    {
    PictureBox1.Image = Properties.Resources.Image_notfound;
    }
    
    private void getimage()
    {
    MySqlConnection connect = new MySqlConnection(con);
    MySqlCommand cmd = new MySqlCommand("SELECT PictureName as 'pic' FROM userprofile where ID='" + datagrid.CurrentRow.Cells["ID"].ToString() + "'");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connect;
    connect.Open();
    Try
    {
    MySqlDataReader dr = cmd.ExecuteReader();
    while(dr.Read())
    {
    imagename = dr.GetString("pic");
    }
    dr.Close();
    }
    catch(Exception ee)
    {
    Console.WriteLine(ee.ToString());
    }
    finally
    {
    connect.Close();
    }
    
    0 讨论(0)
提交回复
热议问题