Reputation: 33
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
Upvotes: 2
Views: 2884
Reputation: 4908
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
Upvotes: 1