It is posible to retrieve image from database like in asp.net handler or better approach?
I want to display images stored in database/binary form from html/js, like previously with an asp.net handler
==============================
Something like in client html/js :
img class=”avatar” src=”~/ShowImage.ashx?id=” + data.id + ‘.png”
==============================
In server handler demo code:
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 empno;
if (context.Request.QueryString[“id”] != null)
empno = Convert.ToInt32(context.Request.QueryString[“id”]);
else
throw new ArgumentException(“No parameter specified”);
context.Response.ContentType = “image/png”; //”image/jpeg”;
Stream strm = ShowEmpImage(empno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}
public Stream ShowEmpImage(int empno)
{
string conn = ConfigurationManager.ConnectionStrings[“EmployeeConnString”].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = “SELECT empimg FROM EmpDetails WHERE empid = @ID”;
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue(“@ID”, empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}
}
Of course 🙂
You can
1- Use a PictureBox and assign  the Image property to an Image created from the database.
2- Use a PictureBox and assign the ImageSource to an image file name saved from  the database.
3- Use a class derived from PictureBox, add “: Wisej.Core.IWisejHandler” and assign the ImageSource to this.GetPostbackUrl(), then implement IWisejHandler.ProcessRequest() as in any handler, the parameter is HttpContext.
4-Â Create a component on a page (or form), or add “: IWisejHandler” to the page (or form) and then you can pass this.GetPostbackUrl() to any <img src> tag you can place in any control (Label, Button, DataGridViewCell) than has AllowHtml. You can also add parameters to this.GetPostbackUrl() + “&img-name=test”, for example, and have your IWisejHandler.ProcessRequest() return an image depending on the parameters.
And infinite combinations of the above.
Please login first to submit.