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. How to pass binary image to image handler to display it in DataList?

How to pass binary image to image handler to display it in DataList?

Scheduled Pinned Locked Moved ASP.NET
databasetutorialquestion
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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    I have a web page with a method called to display some images in the DataList control

    MyImage.cs
    class MyImage
    {
    string Name { get; set; }
    byte[] Jpeg { get; set; }
    }

    MyImages.aspx.cs
    pubic void DisplayMyImages(IEnumerable myImages)
    {
    this.myImagesDataList.DataSource = myImages;
    this.myImagesDataList.DataBind();
    }
    ...
    protected void myImagesDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
    {
    MyImage myImage = (MyImage)e.Item.DataItem;
    Image myImageImage = (Image)e.Item.FindControl("myImageImage");

            // How to pass myImage.Jpeg to ImageHandler? form here
    
            myImageImage.ImageUrl = "~/Handlers/ImageHandler.ashx";
        }
    

    }

    But how to pass jpeg image to ImageHandler if it is already extracted from the database and passed to DisplayMyImages() function? Remarks: I do not want to save them back to files and pass the paths in a query string to ImageHandler Having a standard query string approach is not possible as I do not want to violate model view presenter approach

    Чесноков

    Richard DeemingR 1 Reply Last reply
    0
    • C Chesnokov Yuriy

      I have a web page with a method called to display some images in the DataList control

      MyImage.cs
      class MyImage
      {
      string Name { get; set; }
      byte[] Jpeg { get; set; }
      }

      MyImages.aspx.cs
      pubic void DisplayMyImages(IEnumerable myImages)
      {
      this.myImagesDataList.DataSource = myImages;
      this.myImagesDataList.DataBind();
      }
      ...
      protected void myImagesDataList_ItemDataBound(object sender, DataListItemEventArgs e)
      {
      if (e.Item.ItemType == ListItemType.Item ||
      e.Item.ItemType == ListItemType.AlternatingItem)
      {
      MyImage myImage = (MyImage)e.Item.DataItem;
      Image myImageImage = (Image)e.Item.FindControl("myImageImage");

              // How to pass myImage.Jpeg to ImageHandler? form here
      
              myImageImage.ImageUrl = "~/Handlers/ImageHandler.ashx";
          }
      

      }

      But how to pass jpeg image to ImageHandler if it is already extracted from the database and passed to DisplayMyImages() function? Remarks: I do not want to save them back to files and pass the paths in a query string to ImageHandler Having a standard query string approach is not possible as I do not want to violate model view presenter approach

      Чесноков

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Depending on how big your images are, and which browsers you need to support, you might be able to use a data URI[^]. (If you're using IE8, you're limited to 32Kb; earlier versions of IE aren't supported at all.)

      myImageImage.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(myImage.Jpeg);

      If you need to support older versions of IE, you'll need to pass a query-string to your handler. If the image is more than 32Kb, it'll be too large for a query-string, so you'll need to pass some kind of ID to allow the handler to retrieve the image.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      C 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Depending on how big your images are, and which browsers you need to support, you might be able to use a data URI[^]. (If you're using IE8, you're limited to 32Kb; earlier versions of IE aren't supported at all.)

        myImageImage.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(myImage.Jpeg);

        If you need to support older versions of IE, you'll need to pass a query-string to your handler. If the image is more than 32Kb, it'll be too large for a query-string, so you'll need to pass some kind of ID to allow the handler to retrieve the image.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        C Offline
        C Offline
        Chesnokov Yuriy
        wrote on last edited by
        #3

        that is interesting approach! never thought of it before

        Чесноков

        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