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. read file base64 and show on the image

read file base64 and show on the image

Scheduled Pinned Locked Moved ASP.NET
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
    apadana_1989
    wrote on last edited by
    #1

    hi I want read the file base64 and show on the image control thanks

    J 2 Replies Last reply
    0
    • A apadana_1989

      hi I want read the file base64 and show on the image control thanks

      J Offline
      J Offline
      Joris Janssens
      wrote on last edited by
      #2

      Use a generic handler to read the base64 encoded image , decode it and output it to the browser. Feel free to use Session or Request to pass parameters to the handler to determine which file to read. If you use ASP.NET 4 you can use the very convenient MemoryStream.CopyTo()-method, in versions below you'll have to do the copying yourself. In your webform you can use it like this: <asp:Image ImageUrl="Base64ImageHandler.ashx" runat="server" /> (using the HTML img-element works too ofcourse)

      <%@ WebHandler Language="C#" Class="Base64ImageHandler" %>

      using System;
      using System.Web;
      using System.IO;

      public class Base64ImageHandler : IHttpHandler {

      public void ProcessRequest (HttpContext context) {
          context.Response.ContentType = "image/jpeg";
          string data64 = File.ReadAllText(@"e:\\flotspe\\www\\images\\5base64.txt");
          byte\[\] data = Convert.FromBase64String(data64);
          
          using (MemoryStream ms=new MemoryStream(data)) {
              ms.CopyTo(context.Response.OutputStream);
              //CopyStream(ms, context.Response.OutputStream); // for ASP.NET 3.5 or below
          }
      }
      
      public bool IsReusable {
          get {
              return false;
          }
      }
      private static void CopyStream(Stream input, Stream output) {
          byte\[\] buffer = new byte\[32768\];
          while (true) {
              int read = input.Read(buffer, 0, buffer.Length);
              if (read <= 0)
                  return;
              output.Write(buffer, 0, read);
          }
      } 
      

      }

      1 Reply Last reply
      0
      • A apadana_1989

        hi I want read the file base64 and show on the image control thanks

        J Offline
        J Offline
        Joris Janssens
        wrote on last edited by
        #3

        As an alternative to using a generic handler you could also embed the base64-code directly into the src-attribute of the img element. trivial example:

        using System;
        using System.IO;
        using System.Web.UI;
        using System.Web.UI.WebControls;

        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e) {
        string data64 = File.ReadAllText(@"e:\flotspe\www\images\5base64.txt");
        this.form1.Controls.Add(new Image() { ImageUrl = string.Format("data:image/jpg;base64,{0}",data64) });
        }
        }

        for more information about data URI scheme's see http://en.wikipedia.org/wiki/Data:_URI_scheme[^]

        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