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. Displaying the image from the Sql

Displaying the image from the Sql

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestion
9 Posts 6 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.
  • N Offline
    N Offline
    NaveenHS
    wrote on last edited by
    #1

    Displaying the image from the Sql Hi, I am trying to display image on the page stored in the SQL DB. Image is store in the <BinaryData> format in the DB. I am display in the DataGrid its working fine, problem with page.

    SqlCommand Cmd = new SqlCommand(Query, myconnection);
    SqlDataReader dr = Cmd.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    lblProdName.Text = Product;
    lblProdDesc.Text = dr[0].ToString();
    lblPrice.Text = dr[3].ToString();

          //dr\[5\] image
           byte\[\] Photo = (byte\[\])dr\[5\];
           Response.ContentType = "image/jpeg";
           Response.OutputStream.Write(Photo, 0, Photo.Length); 
          
        }
    
        dr.Close();
    

    only image is displayed on the page without any other content , if I comment lines

    // Response.ContentType = "image/jpeg";
    // Response.OutputStream.Write(Photo, 0, Photo.Length);

    All the contents are displayed except the image. what is the proper way to display the image on the page using SQL ?? Please help me with this. Thanking you, Naveen.

    B G R M 4 Replies Last reply
    0
    • N NaveenHS

      Displaying the image from the Sql Hi, I am trying to display image on the page stored in the SQL DB. Image is store in the <BinaryData> format in the DB. I am display in the DataGrid its working fine, problem with page.

      SqlCommand Cmd = new SqlCommand(Query, myconnection);
      SqlDataReader dr = Cmd.ExecuteReader();
      if (dr.HasRows)
      {
      dr.Read();
      lblProdName.Text = Product;
      lblProdDesc.Text = dr[0].ToString();
      lblPrice.Text = dr[3].ToString();

            //dr\[5\] image
             byte\[\] Photo = (byte\[\])dr\[5\];
             Response.ContentType = "image/jpeg";
             Response.OutputStream.Write(Photo, 0, Photo.Length); 
            
          }
      
          dr.Close();
      

      only image is displayed on the page without any other content , if I comment lines

      // Response.ContentType = "image/jpeg";
      // Response.OutputStream.Write(Photo, 0, Photo.Length);

      All the contents are displayed except the image. what is the proper way to display the image on the page using SQL ?? Please help me with this. Thanking you, Naveen.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      try this Response.ContentType = "image/jpeg" ; Response.BinaryWrite(dr["ImageField"])) ;


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

      1 Reply Last reply
      0
      • N NaveenHS

        Displaying the image from the Sql Hi, I am trying to display image on the page stored in the SQL DB. Image is store in the <BinaryData> format in the DB. I am display in the DataGrid its working fine, problem with page.

        SqlCommand Cmd = new SqlCommand(Query, myconnection);
        SqlDataReader dr = Cmd.ExecuteReader();
        if (dr.HasRows)
        {
        dr.Read();
        lblProdName.Text = Product;
        lblProdDesc.Text = dr[0].ToString();
        lblPrice.Text = dr[3].ToString();

              //dr\[5\] image
               byte\[\] Photo = (byte\[\])dr\[5\];
               Response.ContentType = "image/jpeg";
               Response.OutputStream.Write(Photo, 0, Photo.Length); 
              
            }
        
            dr.Close();
        

        only image is displayed on the page without any other content , if I comment lines

        // Response.ContentType = "image/jpeg";
        // Response.OutputStream.Write(Photo, 0, Photo.Length);

        All the contents are displayed except the image. what is the proper way to display the image on the page using SQL ?? Please help me with this. Thanking you, Naveen.

        G Offline
        G Offline
        GenJerDan
        wrote on last edited by
        #3

        Basically, you want to push a complete web page to a browser, but browsers are designed to pull pages. I don't think you can send two streams like that for one display page (text + image). However, if you programmatically create a ".mht" page, you cvould probably do what you're tryign to do. Maybe something in this article will give you an idea: Convert HTML to MHTML using ASP.NET[^]

        Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. My Mu[sic] My Films My Windows Programs, etc.

        N 1 Reply Last reply
        0
        • G GenJerDan

          Basically, you want to push a complete web page to a browser, but browsers are designed to pull pages. I don't think you can send two streams like that for one display page (text + image). However, if you programmatically create a ".mht" page, you cvould probably do what you're tryign to do. Maybe something in this article will give you an idea: Convert HTML to MHTML using ASP.NET[^]

          Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. My Mu[sic] My Films My Windows Programs, etc.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          GenJerDan wrote:

          you want to push a complete web page to a browser

          What? No, the page is being rendered in response to a request. It's not like the web server is randomly connecting to some machine and trying to send content.

          GenJerDan wrote:

          I don't think you can send two streams like that

          There is only one stream. The images is being placed within that stream.


          I know the language. I've read a book. - _Madmatt

          G 1 Reply Last reply
          0
          • N Not Active

            GenJerDan wrote:

            you want to push a complete web page to a browser

            What? No, the page is being rendered in response to a request. It's not like the web server is randomly connecting to some machine and trying to send content.

            GenJerDan wrote:

            I don't think you can send two streams like that

            There is only one stream. The images is being placed within that stream.


            I know the language. I've read a book. - _Madmatt

            G Offline
            G Offline
            GenJerDan
            wrote on last edited by
            #5

            Mark Nischalke wrote:

            What? No, the page is being rendered in response to a request. It's not like the web server is randomly connecting to some machine and trying to send content.

            Yes, but, basically, a browser requests something and the server delivers it in the stream, i.e. a pull, not a push. Then the browser "looks" at what it got and says to itself Ah-hah! I need to get the image for this. I better request another stream for it. It doesn't all come in one clump. Trying to send two streams when the broweser is only capable of requesting one at a time is not going to work.

            Mark Nischalke wrote:

            There is only one stream. The images is being placed within that stream.

            Nope. There needs to be multiple responses, one for each discrete item. Or there needs to be something on the browser itself that can take a multiplexed stream and demux it. An ActiveX control could do that....but there are too many problems there, the least of which is convincing the user to load yet another plugin. But the MHTML process is already built-in, which is why I suggested it. The only difficulty is in creating the MHTML object in your server code. You can downvote me all you want, but it won't change the way browsers work.

            Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. My Mu[sic] My Films My Windows Programs, etc.

            N 1 Reply Last reply
            0
            • G GenJerDan

              Mark Nischalke wrote:

              What? No, the page is being rendered in response to a request. It's not like the web server is randomly connecting to some machine and trying to send content.

              Yes, but, basically, a browser requests something and the server delivers it in the stream, i.e. a pull, not a push. Then the browser "looks" at what it got and says to itself Ah-hah! I need to get the image for this. I better request another stream for it. It doesn't all come in one clump. Trying to send two streams when the broweser is only capable of requesting one at a time is not going to work.

              Mark Nischalke wrote:

              There is only one stream. The images is being placed within that stream.

              Nope. There needs to be multiple responses, one for each discrete item. Or there needs to be something on the browser itself that can take a multiplexed stream and demux it. An ActiveX control could do that....but there are too many problems there, the least of which is convincing the user to load yet another plugin. But the MHTML process is already built-in, which is why I suggested it. The only difficulty is in creating the MHTML object in your server code. You can downvote me all you want, but it won't change the way browsers work.

              Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. My Mu[sic] My Films My Windows Programs, etc.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              GenJerDan wrote:

              There needs to be multiple responses, one for each discrete item.

              Correct, one at a time. What the OP was asking to is insert the image into the ONE stream along with the text, all of which which would be streamed to the requesting browser as one chunk of binary data. It is possible and is done quite frequently.

              GenJerDan wrote:

              it won't change the way browsers work.

              No but I think you need to change your understanding how things work. The down vote was justified because you are incorrect in your views and what you are suggesting is just plain wrong.


              I know the language. I've read a book. - _Madmatt

              G 1 Reply Last reply
              0
              • N Not Active

                GenJerDan wrote:

                There needs to be multiple responses, one for each discrete item.

                Correct, one at a time. What the OP was asking to is insert the image into the ONE stream along with the text, all of which which would be streamed to the requesting browser as one chunk of binary data. It is possible and is done quite frequently.

                GenJerDan wrote:

                it won't change the way browsers work.

                No but I think you need to change your understanding how things work. The down vote was justified because you are incorrect in your views and what you are suggesting is just plain wrong.


                I know the language. I've read a book. - _Madmatt

                G Offline
                G Offline
                GenJerDan
                wrote on last edited by
                #7

                Oh, really? So what Content-Type type is text with embedded image that a browser can handle, other than as an email client? Other than the MHTML, of course. Or should we all start worrying every web page out there being able to piggyback whatever they want in a content stream?

                Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life. My Mu[sic] My Films My Windows Programs, etc.

                1 Reply Last reply
                0
                • N NaveenHS

                  Displaying the image from the Sql Hi, I am trying to display image on the page stored in the SQL DB. Image is store in the <BinaryData> format in the DB. I am display in the DataGrid its working fine, problem with page.

                  SqlCommand Cmd = new SqlCommand(Query, myconnection);
                  SqlDataReader dr = Cmd.ExecuteReader();
                  if (dr.HasRows)
                  {
                  dr.Read();
                  lblProdName.Text = Product;
                  lblProdDesc.Text = dr[0].ToString();
                  lblPrice.Text = dr[3].ToString();

                        //dr\[5\] image
                         byte\[\] Photo = (byte\[\])dr\[5\];
                         Response.ContentType = "image/jpeg";
                         Response.OutputStream.Write(Photo, 0, Photo.Length); 
                        
                      }
                  
                      dr.Close();
                  

                  only image is displayed on the page without any other content , if I comment lines

                  // Response.ContentType = "image/jpeg";
                  // Response.OutputStream.Write(Photo, 0, Photo.Length);

                  All the contents are displayed except the image. what is the proper way to display the image on the page using SQL ?? Please help me with this. Thanking you, Naveen.

                  R Offline
                  R Offline
                  RichardGrimmer
                  wrote on last edited by
                  #8

                  OK - what you're doing here is explicitly telling the browser that what's coming is an image in jpeg format (Response.ContentType), and that the content itself is the bytestream (Response.OutputStream.Write). With those lines in place, you're NOT rendering anything else to the browser - just the image. The default Response.ContentType is (IIRC) text/html - which is effectively a webpage, so with the lines commented, you're basically saying "do the normal thing", and not writing anything explicitly. To render the image with the text, you need to set the Source of an Image control that's part of the page. Something similar to the following...

                  byte[] imageData = (byte[])dr[5];
                  MemoryStream memStream = new MemoryStream(imageData);
                  image = Image.FromStream(memStream);

                  Which incidentally is pretty much ripped off wholesale from the top link in Google[^]

                  C# has already designed away most of the tedium of C++.

                  1 Reply Last reply
                  0
                  • N NaveenHS

                    Displaying the image from the Sql Hi, I am trying to display image on the page stored in the SQL DB. Image is store in the <BinaryData> format in the DB. I am display in the DataGrid its working fine, problem with page.

                    SqlCommand Cmd = new SqlCommand(Query, myconnection);
                    SqlDataReader dr = Cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                    dr.Read();
                    lblProdName.Text = Product;
                    lblProdDesc.Text = dr[0].ToString();
                    lblPrice.Text = dr[3].ToString();

                          //dr\[5\] image
                           byte\[\] Photo = (byte\[\])dr\[5\];
                           Response.ContentType = "image/jpeg";
                           Response.OutputStream.Write(Photo, 0, Photo.Length); 
                          
                        }
                    
                        dr.Close();
                    

                    only image is displayed on the page without any other content , if I comment lines

                    // Response.ContentType = "image/jpeg";
                    // Response.OutputStream.Write(Photo, 0, Photo.Length);

                    All the contents are displayed except the image. what is the proper way to display the image on the page using SQL ?? Please help me with this. Thanking you, Naveen.

                    M Offline
                    M Offline
                    Morgs Morgan
                    wrote on last edited by
                    #9

                    Hi, I don't know if you have sorted this out but the last person who commented got it right though unclear. What he/she meant is that have an already existing img/image on you page that it's src comes from another page like so:

                    ![theImageFromSQL](ImagesPage.aspx?id=someId)
                    //Now create a separate empty page ImagesPage.aspx
                    //in the Page Load check the query string (id) and use it to query your database for the right image to display
                    //then do exactly what you did above
                    

                    //when a browser loads the image it will call ImagesPage.aspx and this page will write back JUST an image which is what you want (i guess) You can leave the other controls on the 'main' page which could be Home.aspx/Default.aspx and ofcourse your img/image will be in this same main page. shout if u need more help Morgs

                    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