2

I have saved image in database and wanna show it in asp.net ,I have byte array and MIME and size...

    var da = new SqlDataAdapter("GetBlob", sqlConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.AddWithValue("@BlobId", blobDto.BlobId);
                var ds = new DataSet();
                da.Fill(ds, "GetBlob");
                var dr = ds.Tables[0].Rows[0];
                var byteArray = (byte[]) dr["BlobData"];
                string MIME=dr["MIME"]; e.g .jpg

Please help me how can I show this image

1 Answer 1

1

You can convert your byte array to base64 encoded string and pass it to some img tag as src attribute. The src attribute format for base64 encoded image is - data:conentType;base64,theBase64string.

To convert a byte array to base64 string use Convert.ToBase64String(byteArray);

Example C#:

byte[] bytes = new byte[]{ }; // .. Get it from database
string contentType = "image/jpeg"; // .. Get it from database

string imageSrc = string.Format("data:{0};base64,{1}", 
    contentType, Convert.ToBase64String(bytes));

In Html:

<img src="@imageSrc" />
// Or
<img src="<%= imageSrc =>" />
// Depending on your view engine
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it but it doesn't work correctly, actually it shows nothing !!
It works as I described. If it doesn't work for you the problem is somewhere in your code. Please share more of your code and tell what exact technology you use so I can make more complete example. For now here is dotnetfiidle demo for ASP.NET MVC.

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.