read file base64 and show on the image
-
hi I want read the file base64 and show on the image control thanks
-
hi I want read the file base64 and show on the image control thanks
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); } }
}
-
hi I want read the file base64 and show on the image control thanks
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[^]