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. set binary image in Image web control.

set binary image in Image web control.

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestion
3 Posts 2 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.
  • A Offline
    A Offline
    andiyuniar
    wrote on last edited by
    #1

    In my web application, I need to display binary image from database in image web control. I can convert binary data to image, but how to bind it to Image web control? Please help me.

    R 1 Reply Last reply
    0
    • A andiyuniar

      In my web application, I need to display binary image from database in image web control. I can convert binary data to image, but how to bind it to Image web control? Please help me.

      R Offline
      R Offline
      Ryomin
      wrote on last edited by
      #2

      Hi andiyuniar I've had this issue before. The way I handled it was to "spoof" a content page to change it's ContentType to display images. I would pass it an id via query string and then set the imageurl to the spoofed page. Here's an example that should help you. Please keep in mind, I don't know if it's the best solution, but it works for me. Also, there could be some performance hits if you have too many images being loaded on your page via this method. Create a WebForm... i called mine LoadImage.aspx Place this code in the Load event

      if (Request.QueryString["Id"] != null)
      {
      int Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
      byte[] image = null;
      //You will have to add your connection information and grab the image from the database
      // ToDo: Load binary image into image variable;

                  if (image!=null)
                  {
                      MemoryStream ms = new MemoryStream(image);
                      Bitmap b = new Bitmap(Image.FromStream(ms));
      
                      Response.ContentType = "image/jpeg";
                      b.Save(Response.OutputStream, ImageFormat.Jpeg);
                  }
                  else
                  {
                      Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));
      
                      Response.ContentType = "image/jpeg";
                      b.Save(Response.OutputStream, ImageFormat.Jpeg);
                  }
          }
          else
          {
              Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));
      
              Response.ContentType = "image/jpeg";
              b.Save(Response.OutputStream, ImageFormat.Jpeg);
          }
      

      Then on the page you want to bind your image to just set the ImageUrl property to the webform plus query string.

      Image1.ImageUrl = "LoadImage.aspx?Id=1";

      Happy Coding

      Cheers Disgyza Programmer Analyst

      A 1 Reply Last reply
      0
      • R Ryomin

        Hi andiyuniar I've had this issue before. The way I handled it was to "spoof" a content page to change it's ContentType to display images. I would pass it an id via query string and then set the imageurl to the spoofed page. Here's an example that should help you. Please keep in mind, I don't know if it's the best solution, but it works for me. Also, there could be some performance hits if you have too many images being loaded on your page via this method. Create a WebForm... i called mine LoadImage.aspx Place this code in the Load event

        if (Request.QueryString["Id"] != null)
        {
        int Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
        byte[] image = null;
        //You will have to add your connection information and grab the image from the database
        // ToDo: Load binary image into image variable;

                    if (image!=null)
                    {
                        MemoryStream ms = new MemoryStream(image);
                        Bitmap b = new Bitmap(Image.FromStream(ms));
        
                        Response.ContentType = "image/jpeg";
                        b.Save(Response.OutputStream, ImageFormat.Jpeg);
                    }
                    else
                    {
                        Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));
        
                        Response.ContentType = "image/jpeg";
                        b.Save(Response.OutputStream, ImageFormat.Jpeg);
                    }
            }
            else
            {
                Bitmap b = new Bitmap(Image.FromFile(Server.MapPath("images/noimage.png")));
        
                Response.ContentType = "image/jpeg";
                b.Save(Response.OutputStream, ImageFormat.Jpeg);
            }
        

        Then on the page you want to bind your image to just set the ImageUrl property to the webform plus query string.

        Image1.ImageUrl = "LoadImage.aspx?Id=1";

        Happy Coding

        Cheers Disgyza Programmer Analyst

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

        Thanks. it help me.

        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