Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. retreiving images from database and display as thumbnail images

retreiving images from database and display as thumbnail images

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabase
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    developerit
    wrote on last edited by
    #1

    hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me saving code

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (FileUpload1.HasFile && FileUpload2.HasFile)
        {
    
            img1.ImageUrl = imgc.ImageUrl;
            img2.ImageUrl = imgo.ImageUrl;
            FileUpload1.SaveAs(MapPath("~/images/" + FileUpload1.FileName));
            imgc.ImageUrl = "~/images/" + FileUpload1.FileName;
            FileUpload2.SaveAs(MapPath("~/images/" + FileUpload2.FileName));
            imgo.ImageUrl = "~/images/" + FileUpload2.FileName;
    
            con.Open();
    
            SqlCommand cmd = new SqlCommand("Addimgproc",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Shop","Alsafa");
            FileInfo imageinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
            byte\[\] content = new byte\[imageinfo.Length\];
            FileStream imagestream = imageinfo.OpenRead();
            imagestream.Read(content,0,content.Length);
            imagestream.Close();
            //2nd upload
            FileInfo imageinfo1 = new FileInfo(FileUpload2.PostedFile.FileName.Trim());
            byte\[\] content1 = new byte\[imageinfo1.Length\];
            FileStream imagestream1 = imageinfo1.OpenRead();
            imagestream1.Read(content1, 0, content1.Length);
            imagestream1.Close();
    
            cmd.Parameters.AddWithValue("@imgc", content);
            cmd.Parameters.AddWithValue("@imgo",content1);
            cmd.ExecuteNonQuery();
            con.Close();
    
        }
    
    
        }
    

    retreiving code ================ protected void Page_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand("select imgc,imgo from images where Shop='Alsafa'",con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }

    A A 2 Replies Last reply
    0
    • D developerit

      hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me saving code

      protected void Button1_Click(object sender, EventArgs e)
      {

          if (FileUpload1.HasFile && FileUpload2.HasFile)
          {
      
              img1.ImageUrl = imgc.ImageUrl;
              img2.ImageUrl = imgo.ImageUrl;
              FileUpload1.SaveAs(MapPath("~/images/" + FileUpload1.FileName));
              imgc.ImageUrl = "~/images/" + FileUpload1.FileName;
              FileUpload2.SaveAs(MapPath("~/images/" + FileUpload2.FileName));
              imgo.ImageUrl = "~/images/" + FileUpload2.FileName;
      
              con.Open();
      
              SqlCommand cmd = new SqlCommand("Addimgproc",con);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@Shop","Alsafa");
              FileInfo imageinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
              byte\[\] content = new byte\[imageinfo.Length\];
              FileStream imagestream = imageinfo.OpenRead();
              imagestream.Read(content,0,content.Length);
              imagestream.Close();
              //2nd upload
              FileInfo imageinfo1 = new FileInfo(FileUpload2.PostedFile.FileName.Trim());
              byte\[\] content1 = new byte\[imageinfo1.Length\];
              FileStream imagestream1 = imageinfo1.OpenRead();
              imagestream1.Read(content1, 0, content1.Length);
              imagestream1.Close();
      
              cmd.Parameters.AddWithValue("@imgc", content);
              cmd.Parameters.AddWithValue("@imgo",content1);
              cmd.ExecuteNonQuery();
              con.Close();
      
          }
      
      
          }
      

      retreiving code ================ protected void Page_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand("select imgc,imgo from images where Shop='Alsafa'",con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }

      A Offline
      A Offline
      Arindam Tewary
      wrote on last edited by
      #2

      Hi, Can you please mention what problem you have with your code. It would be good if you can say whether you get any error/exception or what is not happening (that was expected !!!). Please let us know which line you might be suspecting something problem!!! Unless you mention those ponts, it will not be possible for people to go through your code or compile your code and to see what is happening!!.

      Thanks, Arindam D Tewary

      1 Reply Last reply
      0
      • D developerit

        hi, iam using asp.net with c# i have table images shop nvarchar(50), imgc image imgo image iam saving images in a database but while retreving it is showing blank images. i want to display as thumb nail images , can you correct my code which helps me saving code

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile && FileUpload2.HasFile)
            {
        
                img1.ImageUrl = imgc.ImageUrl;
                img2.ImageUrl = imgo.ImageUrl;
                FileUpload1.SaveAs(MapPath("~/images/" + FileUpload1.FileName));
                imgc.ImageUrl = "~/images/" + FileUpload1.FileName;
                FileUpload2.SaveAs(MapPath("~/images/" + FileUpload2.FileName));
                imgo.ImageUrl = "~/images/" + FileUpload2.FileName;
        
                con.Open();
        
                SqlCommand cmd = new SqlCommand("Addimgproc",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Shop","Alsafa");
                FileInfo imageinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
                byte\[\] content = new byte\[imageinfo.Length\];
                FileStream imagestream = imageinfo.OpenRead();
                imagestream.Read(content,0,content.Length);
                imagestream.Close();
                //2nd upload
                FileInfo imageinfo1 = new FileInfo(FileUpload2.PostedFile.FileName.Trim());
                byte\[\] content1 = new byte\[imageinfo1.Length\];
                FileStream imagestream1 = imageinfo1.OpenRead();
                imagestream1.Read(content1, 0, content1.Length);
                imagestream1.Close();
        
                cmd.Parameters.AddWithValue("@imgc", content);
                cmd.Parameters.AddWithValue("@imgo",content1);
                cmd.ExecuteNonQuery();
                con.Close();
        
            }
        
        
            }
        

        retreiving code ================ protected void Page_Load(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand("select imgc,imgo from images where Shop='Alsafa'",con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Image1.ImageUrl = dr["imgc"].ToString(); Image2.ImageUrl = dr["imgo"].ToString(); } dr.Close(); } con.Close(); }

        A Offline
        A Offline
        adkalavadia
        wrote on last edited by
        #3

        you are storing images in binary in sql server...n you r directly use tostring() function on it..!!!

          bool flgImage = false;
          byte\[\] buffer = new byte\[0\];
          buffer = (byte\[\])SQLHelper.ExecuteScalar("SELECT imgImage FROM dbo.MeasurementImages WHERE intImageID=id");
        
            context.Response.ClearHeaders();
            context.Response.Expires = 0;
            context.Response.Cache.SetNoServerCaching();
            context.Response.Cache.SetMaxAge(TimeSpan.Zero);
            context.Response.ContentType = "image/gif";
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.BufferOutput = false;
            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            context.Response.Flush();
            context.Response.End();
        

        Make image handler file for retriving images from database. this is a better way.

        !- F - R - I - E - N - D - S -!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups