I am developing a website using ASP.NETand C#as code.
I have a database MSSQLcontaining images saved as varbinary(max). I extract them and convert the array byteto Base64Stringto show it on my site using CSS as follows:
if (cid[Database.MSImageType] != DBNull.Value && ((string)cid[Database.MSImageType]).StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
DataSet _imagedata = Database.GetDataOffline("SELECT " + Database.MSImage + " FROM " + table + " WHERE " + Database.Id + "=" + QueryStr["ID"]);
if (_imagedata.Tables.Count > 0 && _imagedata.Tables[0].Rows.Count == 1)
{
DataRow _image = _imagedata.Tables[0].Rows[0];
string imagedata = null;
if (_image[Database.MSImage] != DBNull.Value && (imagedata = MSImage.ConvertToImage((byte[])_image[Database.MSImage])) != null)
{
takzir2.InnerHtml = "<div id='msbgimage' class='msbgimage' style=\"background-image:url(data:" + (string)cid[Database.MSImageType] + ";base64," + imagedata + ")\"></div>";
msbgimagefullp.Visible = true;
msbgimagefullp.InnerHtml = "<div id='msbgimagefull' class='msbgimagefull' style=\"background-image:url(data:" + (string)cid[Database.MSImageType] + ";base64," + imagedata + ")\"></div>";
}
}
}
cidis DataRow.
The problem is that it takes a long time to download the image, and I would like to know how to make it faster, if possible. This is the function I use to convert it to Base64:
public static string ConvertToImage(byte[] imagedata)
{
try
{
if (imagedata != null && imagedata.Length > 0)
return Convert.ToBase64String(imagedata);
}
catch { }
return null;
}
source
share