Problem
A binary picture that has been saved in a database or an Access File (i.e. of the Sparx Systems Enterprise Architect) shall be displayed on a web page.
Approach
- Create a new ASP.NET Webform and name it GetImage.aspx
- Go to the Page_Load function in it
- Paste the code under solution in the area in customize according your data structure (here it is Sparx EA).
- Create a img-Tag in HTML, that has a src-Attribute pointing to that webpage with a get parameter img={your image id}
- Use Response.BinaryWrite in the way shown below
Solution
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GetImage : System.Web.UI.Page
{
// Mit folgender URL Kann ein Bild nun rausgeladen werden
// http://localhost:51241/GetImage.aspx?img=343868582
// und entsprechend in HTML über den Image-Tag geladen werden:
// <img src="GetImage.aspx?img=343868582" />
protected void Page_Load(object sender, EventArgs e)
{
string sqlStatement = @"
SELECT Image
FROM t_image
WHERE ImageID={ImageID}
";
sqlStatement = sqlStatement.Replace("{ImageID}", Request.QueryString["img"].Trim());
OleDbConnection conn = new OleDbConnection(MyConfigurationManager.eapFilePath);
try
{
conn.Open();
OleDbCommand comm = new OleDbCommand();
comm.Connection = conn;
comm.CommandText = sqlStatement;
OleDbDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "image/jpeg"; // if your image is a jpeg of course
Response.BinaryWrite((byte[])reader.GetValue(0));
}
}
catch (Exception ex)
{
//return e.Message;
}
finally
{
conn.Close();
}
}
}