Conversion de données binarys en contrôle d’image dans ASP.NET

J’ai des données binarys d’une image dans ma firebase database et je souhaite les afficher dans un contrôle d’image dans ASP.NET. Comment? Si c’est impossible, veuillez trouver un autre moyen de l’enregistrer dans la firebase database et de l’afficher dans un contrôle d’image.

Créez un élément HTML standard img comme ceci:

  

Et dans le code derrière faire ceci:

 image.src = "data:image/png;base64," + Convert.ToBase64Ssortingng(imageBytes); 

Où imageBytes est un byte[] .

Vous avez terminé. L’image sera affichée.

Très probablement, l’image est stockée sous forme de tableau d’octets dans la firebase database. Si oui, alors vous pouvez utiliser ceci:

 public static System.Drawing.Image ByteArrayToImage(byte[] bArray) { if (bArray == null) return null; System.Drawing.Image newImage; try { using (MemoryStream ms = new MemoryStream(bArray, 0, bArray.Length)) { ms.Write(bArray, 0, bArray.Length); newImage = System.Drawing.Image.FromStream(ms, true); } } catch (Exception ex) { newImage = null; //Log an error here } return newImage; } 

Dans un gestionnaire générique (.ashx):

  public class ImageHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { if(!ssortingng.IsNullOrEmpty(context.Request.QuerySsortingng["ImageId"])){ try { ssortingng ImageId = context.Request.QuerySsortingng["ImageId"].ToSsortingng(); ImageDataModel idm = new ImageDataModel(); byte[] ImageData = idm.getImageData(ImageId); context.Response.ContentType = "image/JPEG"; context.Response.OutputStream.Write(ImageData, 0, ImageData.Length); } 
 public Byte[] Ret_image(Int32 id) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from tbimage where imageid=@id"; cmd.Connection = con; cmd.Parameters.Add("@id", SqlDbType.Int).Value = id; SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); Byte[] ar = (Byte[])(dr[1]); dr.Close(); cmd.Dispose(); return ar; } 
 protected void Button2_Click(object sender, EventArgs e) { Byte[] ar = Ret_image(Convert.ToInt32(TextBox2.Text)); Ssortingng st = Server.MapPath("abc.jpg"); FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write); fs.Write(ar, 0, ar.Length); fs.Close(); Image1.ImageUrl = "abc.jpg"; } 

Utilisez cet événement pour le clic du bouton pour récupérer l’image et appelez la méthode Ret_Image ici.

 SqlConnection con = new SqlConnection(); ssortingng _path; Using SYstem.IO; Using System.Data.SQLClient; //convert Image to binary and save in DB private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { _path = openFileDialog1.FileName; InsertInSQL(_path); } } private void InsertInSQL(ssortingng _path) { con.ConnectionSsortingng = Pic.Properties.Settings.Default.ConnectionS; ssortingng strQ = "insert into dbo.PicTBL(Pic)values(@p)"; SqlCommand command = new SqlCommand(strQ,con); command.Parameters.AddWithValue("@p",ImageToBinary(_path)); con.Open(); command.ExecuteNonQuery(); con.Close(); } public static byte[] ImageToBinary(ssortingng _path) { FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read); byte[] b = new byte[fS.Length]; fS.Read(b, 0, (int)fS.Length); fS.Close(); return b; } //Convert Binary to imge and save in a folder private void button1_Click_1(object sender, EventArgs e) { DataTable dt = Rimage(); foreach (DataRow row in dt.Rows) { byte[] b = (byte[])row["Pic"]; Image img = BinaryToImage(b); img.Save("D:\\NewFolder\\" + row["ID"].ToSsortingng() + ".jpg"); } } private Image BinaryToImage(byte[] b) { if (b == null) return null; MemoryStream memStream = new MemoryStream(); memStream.Write(b, 0, b.Length); return Image.FromStream(memStream); } private DataTable Rimage() { con.ConnectionSsortingng = Pic.Properties.Settings.Default.ConnectionS; SqlCommand cmd = new SqlCommand(); cmd.CommandText = "select * from dbo.PicTBL"; cmd.Connection = con; SqlDataAdapter adp = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); con.Open(); adp.Fill(dt); return dt; }