If I understand, you are asking how to render an image stored in a database table. The standard approach is taking advantage of the ASP.NET Generic handler (ashx). Add -> New Item -> Generic Handler. The generic handler is the URL you'll use in the Image control.
Write a query to return the file contents in the ashx.
public class GetFileById : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//Assumes the file path is passed in the query string
int Id = 0;
if (int.TryParse(context.Request.QueryString["id"], out Id))
{
string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);
string sqlStatement = @"SELECT [FileContents]
,[FileName]
,[ContentType]
FROM [dbo].[Images]
WHERE ImageId = @ImageId ";
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@ImageId", Id);
cmd.CommandType = CommandType.Text;
byte[] fileContents = null;
string fileName = string.Empty;
string contentType = string.Empty;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
fileContents = (byte[])reader[0];
fileName = (string)reader[1];
contentType = (string)reader[2];
}
}
HttpResponse response = context.Response;
response.ContentType = "contentType";
response.AppendHeader("Content-Disposition", "filename=" + fileName);
response.OutputStream.Write(fileContents, 0, fileContents.Length);
response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
The handler fetches the file contents by the primary key. Use whatever filter makes sense for your application. Also, the table holds content-type which is go to have so you know what file type the user uploaded.
Finally, just use the ashx file to render the image. I place the ashx file in a FileHandler folder.
<asp:Image ID="Image1" runat="server" ImageUrl="/FileHandler/GetFileById.ashx?id=1" />