Sahil Verma
Sahil Verma

Reputation: 33

Image Save and Retrieve in SQL Server in asp.net using c#

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

Upvotes: 3

Views: 29986

Answers (5)

Md Shahriar
Md Shahriar

Reputation: 2786

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

Upvotes: 1

Sahil Verma
Sahil Verma

Reputation: 33

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

Upvotes: 0

Matt Tabor
Matt Tabor

Reputation: 1053

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

    }

Upvotes: 0

RononDex
RononDex

Reputation: 4183

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

Upvotes: 0

Wahtever
Wahtever

Reputation: 3678

Something like this will convert Byte[] to Image:

MemoryStream ms = new MemoryStream(byte);
Image i = Image.FromStream(ms);

Upvotes: 5

Related Questions