C# 使用c在asp.net的SQL Server中保存和检索图像

C# 使用c在asp.net的SQL Server中保存和检索图像,c#,asp.net,sql,sql-server,image,C#,Asp.net,Sql,Sql Server,Image,我正在尝试将图像保存到SQL Server数据库中 我将图像转换为字节并存储在SQL Server中,但现在我想将保存的字节转换为图像,并使用c将其显示在asp.net中的标签上 我试了很多,但没有找到解决办法 下面是将字节转换为图像的代码 if (fImage.HasFile) { if (fImage.PostedFile.ContentType == "image/jpg" || fImage.PostedFile.ContentType == "image/jpeg" || fI

我正在尝试将图像保存到SQL Server数据库中

我将图像转换为字节并存储在SQL Server中,但现在我想将保存的字节转换为图像,并使用c将其显示在asp.net中的标签上

我试了很多,但没有找到解决办法

下面是将字节转换为图像的代码

if (fImage.HasFile)
{
    if (fImage.PostedFile.ContentType == "image/jpg" || fImage.PostedFile.ContentType == "image/jpeg" || fImage.PostedFile.ContentType == "image/png")
    {
       int filelenght = fImage.PostedFile.ContentLength;
       byte[] imagebytes = new  byte[filelenght];
       fImage.PostedFile.InputStream.Read(imagebytes, 0, filelenght);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandText = "Insert into tbImage(Img) values(@img)";
       cmd.Connection = con;
       cmd.Parameters.AddWithValue("@img", imagebytes);
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
       Response.Write("Image saved to database");
    }
}

类似的内容将字节[]转换为图像:


您只需将字节内容存储在变量中,然后将其写入文件系统:

var imgBlob = ... // Load the image binary array from the database
File.WriteAllBytes("path/to/file.jpg", imgBlob);

您可以创建一个控制器操作来处理检索图像的操作

    public FileResult DownloadImage(int Photo_Id)
    {

        byte[] fileBytes = "get bytes from db here";
        string fileName = "file name here"

        return File(fileBytes, "contentType of the image" , fileName);

    }

谢谢你宝贵的回复

我用这个来解决这个问题。 看一看

int id=Convert.ToInt32drpImage.SelectedValue

    SqlCommand cmd = new SqlCommand("Select img from tbImage where id = " + id+"", con);
    con.Open();
    SqlDataReader dr = null;
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            byte[] img = (byte[])dr["img"];
            string base64string = Convert.ToBase64String(img, 0, img.Length);

            lblImage.Text += "<img src='data:image/png;base64," + base64string + "' alt='No Image' width='200px' vspace='5px' hspace='5px' />";
        }
    }
    con.Close();

你能解释一下吗,这不仅仅是一堵代码墙?
    SqlCommand cmd = new SqlCommand("Select img from tbImage where id = " + id+"", con);
    con.Open();
    SqlDataReader dr = null;
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            byte[] img = (byte[])dr["img"];
            string base64string = Convert.ToBase64String(img, 0, img.Length);

            lblImage.Text += "<img src='data:image/png;base64," + base64string + "' alt='No Image' width='200px' vspace='5px' hspace='5px' />";
        }
    }
    con.Close();
SqlCommand cmd = new SqlCommand("Select img from tbImage where id = " + id, con);

con.Open();

SqlDataReader reader = cmd.ExecuteReader();

if (reader.HasRows)
{
    while (reader.Read())
    {
        byte[] img = (byte[])reader["img"];

        MemoryStream ms = new MemoryStream(img);

        PictureBox1.Image = Image.FromStream(ms);
    }
}

con.Close();