to display image in image control from data base

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faizalahmd
    New Member
    • Mar 2008
    • 7

    to display image in image control from data base

    How to display image in image control , which is retrieved from the data base . using c# in asp.net

    regards,
    FAIZAL AHMED.H
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What part are you having trouble with?
    Create a Bitmap (or Image) object from the data you retreive and set it as the image for the image control.

    Comment

    • faizalahmd
      New Member
      • Mar 2008
      • 7

      #3
      Code:
      MyCommand = new SqlCommand("SELECT IMAGE_DATA FROM MYTABLE WHERE ID=1", MyConnection);
      MyConnection.Open();
      MyReader = MyCommand.ExecuteReader();
      
      if (MyReader.Read())
      {
         byte[] m_MyImage = (byte[])MyReader["IMAGE_DATA"];
         Response.BinaryWrite(m_MyImage);
      }
      This is how , i retrieved image from the data base , now i would like to display that image in my image control ..can you help me in converting this byte array into Image object or bit map object and bind that object into the image control .. -

      Comment

      • ShahbazAshraf
        New Member
        • Mar 2008
        • 36

        #4
        use this

        This will create an image object then this object can be reffered into Image Control.
        Code:
        using (MemoryStream ms = new MemoryStream(my_MyImage,0,my_MyImage.Length)) 
        { 
        
        ms.Write(my_MyImage,0,my_MyImage.Length); 
        
        System.Drawing.Image newImage = Image.FromStream(ms,true); 
        
        }

        Comment

        • faizalahmd
          New Member
          • Mar 2008
          • 7

          #5
          Thank you very much , now the code is working fine as per my expectation .

          regards,
          FAIZAL AHMED.H

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I figured it was probably how to get a bitmap object from a byte[]. But someone beat me to posting about MemoryStream, hehe.

            Comment

            Working...