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

user3698913
  • 33
  • 1
  • 7

1 Answers1

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
Viktor Bahtev
  • 4,858
  • 2
  • 31
  • 40
  • I tried it but it doesn't work correctly, actually it shows nothing !! – user3698913 Sep 21 '14 at 07:51
  • 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](https://dotnetfiddle.net/hksAY2) for ASP.NET MVC. – Viktor Bahtev Sep 21 '14 at 08:30