3

I am trying to save images in a SQL Server database.

I convert the image to bytes and store in SQL Server, but now I want to convert the saved bytes to an image and show it on a label in asp.net using c#.

I tried a lot but didn't find a solution.

Here is the code (convert bytes to Image)

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");
    }
}

5 Answers 5

5

Something like this will convert Byte[] to Image:

MemoryStream ms = new MemoryStream(byte);
Image i = Image.FromStream(ms);
Sign up to request clarification or add additional context in comments.

Comments

1
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();

1 Comment

Can you add some explanation, so it's more than just a wall of code?
0

All you need to do is to store the byte content in a variable and then write it to the File system:

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

Comments

0

you can create a controller action to handle retrieving the image

    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);

    }

Comments

0

Thanks for your valuable reply.

i solve this problem with this one. give a look.

int id = Convert.ToInt32(drpImage.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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.