0

This is my database:

create table images 
(
     ID int primary key identity, 
     Name nvarchar(255),
     Size int, 
     ImgData varbinary(max) 
)

CREATE PROCEDURE UploadImages
     @Name nvarchar(255),
     @Size int,
     @ImgData varbinary(max),
     @NewId int output
AS
BEGIN
    INSERT INTO images
    VALUES (@Name, @Size, @ImgData)

    SELECT @NewId = SCOPE_IDENTITY()    
END

I want to display image from database into label - how can I do that in asp.net?

This is my code:

 HttpPostedFile PostedFile = FileUpload1.PostedFile;

 string fileName = Path.GetFileName(PostedFile.FileName);
 string fileExtension = Path.GetExtension(fileName);
 int fileSize = PostedFile.ContentLength;

 if(fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".bmp"|| fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
 {
     Stream stream = PostedFile.InputStream;
     BinaryReader binaryReader = new BinaryReader(stream);

     byte[] bytes = binaryReader.ReadBytes((int)stream.Length);

     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

     using (SqlConnection con = new SqlConnection(cs))
     {
         SqlCommand cmd = new SqlCommand("UploadImages", con);
         cmd.CommandType = CommandType.StoredProcedure;

         con.Open();

         SqlParameter paramName = new SqlParameter()
         {
             ParameterName = "@Name",
             Value = fileName
         };
         cmd.Parameters.Add(paramName);

         SqlParameter paramSize = new SqlParameter()
         {
             ParameterName = "@Size",
             Value = fileSize
         };
         cmd.Parameters.Add(paramSize);

         SqlParameter paramImgData = new SqlParameter()
         {
             ParameterName = "@ImgData",
             Value = bytes
         };
         cmd.Parameters.Add(paramImgData);

         SqlParameter paramNewId = new SqlParameter()
         {
             ParameterName = "@NewId",
             Value =-1,
             Direction = ParameterDirection.Output
         };
         cmd.Parameters.Add(paramNewId);

         cmd.ExecuteNonQuery();
         con.Close();

         Lmas.Visible = true;
         Lmas.Text = "done";
         Lmas.ForeColor = System.Drawing.Color.Green;
         HyperLink1.Visible = true;
         HyperLink1.NavigateUrl = "~/ShowImage.aspx?Id=" + cmd.Parameters["@NewId"].Value.ToString();

         //LoadImage();
     } 
 } 
 else 
 {
     Lmas.Visible = true;
     Lmas.Text = "only images (.jpg .png .gif .bmp) can be uploaded";
     Lmas.ForeColor = System.Drawing.Color.Red;
     HyperLink1.Visible = false;
 }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Naeem
  • 11
  • 8

1 Answers1

0

An easy fix is:

 <img id="img" runat="server" alt=""/> (instead of an label)

and in your function

img.Src = "data:image/jpg;base64," + Convert.ToBase64String((byte[])datarow[0]);
Roelant M
  • 1,581
  • 1
  • 13
  • 28
  • I try it but doesnt reconize .src – Naeem Sep 19 '16 at 09:46
  • you do get the img-tag? – Roelant M Sep 19 '16 at 09:47
  • opps, i was made it by asp.net but now i changed it , but i have red line under datarow says: it used like a variable – Naeem Sep 19 '16 at 09:52
  • you need to replace datarow[0] for your variable that contains the byte-array. – Roelant M Sep 19 '16 at 09:56
  • How can i do this? – Naeem Sep 19 '16 at 09:58
  • byte[] sevenThousandItems = CreateSpecialByteArray(7000); public static byte[] CreateSpecialByteArray(int length) { var arr = new byte[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = 0x20; } return arr; } Is that right? – Naeem Sep 19 '16 at 10:10
  • Why not just reading it out of the DB? In the page where you want to put your image you need to read out the column/record from the db. in your code that you posted the following would be good: img.Src = "data:image/jpg;base64," + Convert.ToBase64String(bytes); – Roelant M Sep 19 '16 at 10:57
  • string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetImageById", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter paramID = new SqlParameter() { ParameterName = "@ID", Value = Request.QueryString["ID"] }; cmd.Parameters.Add(paramID); – Naeem Sep 19 '16 at 11:03
  • con.Open(); cmd.ExecuteScalar(); byte[] bytes = (byte[])cmd.ExecuteScalar(); string strBase64 = Convert.ToBase64String(bytes); img.Src = "data:image/jpg;base64," + Convert.ToBase64String((byte[])sevenThousandItems); } } public static byte[] CreateSpecialByteArray(int length) { var arr = new byte[length]; for (int i = 0; i < arr.Length; i++) { arr[i] = 0x20; } return arr; } – Naeem Sep 19 '16 at 11:03
  • why arr[i] = 0x20 ? – Roelant M Sep 19 '16 at 11:08
  • If i want to display images in anther page i should copy all of the code? – Naeem Sep 19 '16 at 11:31
  • I hope for you that you have a repository where you can call your logic every time. But yeah, in essential yes. So in your new page something like: var imgByteArray = ImagesRepository.GetImageByteArray(id); and then you can img.Src = "data:image/jpg;base64," + Convert.ToBase64String(imgByteArray); – Roelant M Sep 19 '16 at 11:33
  • img.Src = "data:image/jpg;base64," + Convert.ToBase64String(bytes); It work but i should clink on HyperLink1, i want when i click on upload the image display in Default.aspx . that is my code – Naeem Sep 19 '16 at 16:35
  • string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetImageById", con); cmd.CommandType = CommandType.StoredProcedure; – Naeem Sep 19 '16 at 16:36
  • SqlParameter paramID = new SqlParameter() { ParameterName = "@ID", Value = Request.QueryString["ID"] }; cmd.Parameters.Add(paramID); con.Open(); cmd.ExecuteScalar(); byte[] bytes = (byte[])cmd.ExecuteScalar(); string strBase64 = Convert.ToBase64String(bytes); Image1.ImageUrl = "data:Image/png;base64," + strBase64; – Naeem Sep 19 '16 at 16:36