Afficher Image binaire depuis BDD C# asp.net
Bonjour,
Voila plusieurs heures que j'essaye d'afficher une image depuis une BDD sans l'enregistrer sur le client ou le serveur.
J'ai donc créer un Handler.ashx:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
public ConnexionSQL connec = new ConnexionSQL();
private string CONNEXION_SQL_Reference;
public void ProcessRequest (HttpContext context) {
HttpRequest request = context.Request;
HttpServerUtility server = context.Server;
context.Response.ContentType = "image/jpeg";
CONNEXION_SQL_Reference = connec.ConnexionEcommerce();
if (context.Request.QueryString["imgId"] != null)
{
string imgId = "";
imgId = context.Request.QueryString["imgId"];
byte[] tab = GetImageFromDB(imgId);
MemoryStream memoryStream = new MemoryStream(tab, false);
System.Drawing.Image imgFromGB = System.Drawing.Image.FromStream(memoryStream);
imgFromGB.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private byte[] GetImageFromDB(string serie)
{
byte[] imageBytes = null;
try
{
System.Data.SqlClient.SqlConnection myConnection2 = new System.Data.SqlClient.SqlConnection();
myConnection2.ConnectionString = CONNEXION_SQL_Reference;
myConnection2.Open();
System.Data.SqlClient.SqlCommand myCommand2 = new System.Data.SqlClient.SqlCommand("SELECT [NumSerie],[Image],[NomImage] FROM [BDD].[dbo].[Img] where [NumSerie]='" + serie + "'", myConnection2);
System.Data.SqlClient.SqlDataReader myReader3 = null;
myReader3 = myCommand2.ExecuteReader();
myReader3.Read();
imageBytes = (byte[])myReader3["Image"];
myConnection2.Close();
return imageBytes;
}
catch (Exception ex)
{
return null;
}
finally
{
}
}
public bool IsReusable {
get {
return true;
}
} |
Puis depuis ma page aspx :
Code:
1 2 3
|
<asp:Image ID="imgProfile" Visible="true" runat="server" />
imgProfile.ImageUrl = "~/Account/Handler.ashx?imgid=" + serie; |
Mon problème est que j'ai l'erreur invalid Parameter sur cette ligne :
Code:
1 2
|
System.Drawing.Image imgFromGB = System.Drawing.Image.FromStream(memoryStream); |
Merci pour votre aide