4

I'm developing a website using ASP.NET and C# as the code-behind.

I have a MSSQL database that contains images saved as varbinary(max). I'm retrieving them and converting the byte array into Base64String in order to show it in my website using CSS like this:

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

cid is a DataRow.

The problem is that it takes a lot of time to load the image, and I'd 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;
}
Omer Aviv
  • 286
  • 3
  • 20

1 Answers1

0

If speed when showing the image is important are you able to store a pre-calculated base64 string of the image in the database itself?

Russ Penska
  • 126
  • 1
  • 6