从MySQL数据库中检索图像并使用c#在网页上显示

时间:2013-11-26 04:19:26

标签: c# mysql asp.net

我正在使用带有c#的asp.net来构建网站,但现在我遇到了一个问题。我将带有blob类型的图像插入到mysql数据库中但我无法检索它。 Web控件没有picturebox布局。我想使用image.imageURL来显示此图片。我搜索了很多,有些人建议使用另一个aspx页面,有些人建议使用ashx,但我找不到详细的解决方案。这就是我现在所拥有的:

   protected void Button1_Click(object sender, EventArgs e)
    {
        String myname = Request.QueryString["Name"];
        string myConnection = "server=127.0.0.1;uid=root;" + "pwd=81210ZLK;database=database;" + "Allow User Variables=True";

    try
    {

        MySqlConnection myConn = new MySqlConnection(myConnection);
        myConn.ConnectionString = myConnection;
        MySqlCommand SelectCommand = new MySqlCommand();
        string mySQL = "SELECT iddb1,fullname,age,gender,healthrecord,headpicture FROM database.db1 where fullname = @myname  ";
        SelectCommand.CommandText = mySQL;
        SelectCommand.Parameters.AddWithValue("@myname", myname);
        SelectCommand.Connection = myConn;
        MySqlDataReader myReader;
        myConn.Open();
        myReader = SelectCommand.ExecuteReader();
        while (myReader.Read())
        {

            Int16 ID = myReader.GetInt16(0);
            string FName = myReader.GetString(1);
            Int16 FAge = myReader.GetInt16(2);
            string FGender = myReader.GetString(3);
            string FRecord = myReader.GetString(4);
            ShowID.Text = ID.ToString();
            ShowName.Text = FName.ToString();
            ShowAge.Text = FAge.ToString();
            ShowGender.Text = FGender.ToString();
            ShowRecord.Text = FRecord.ToString();

            byte[] imgg = (byte[])(myReader["headpicture"]);
            if (imgg == null)
                Image1.ImageUrl = null;
            else {
                MemoryStream mstream = new MemoryStream(imgg);
           //   Image1.ImageURL = System.Drawing.Image.FromStream(mstream);

            }
        }
        myConn.Close();
    }
    catch (Exception ex)
    {
        MessageBoxShow(this, ex.Message);
    }

}

这里有问题,我用\\

标记了它

1 个答案:

答案 0 :(得分:1)

试试这个,

Image1.ImageURL = "data:image/jpeg;base64,"+Convert.ToBase64String(imgg);