Is there anyway to display dynamically generated Bitmap on a asp image control?

23,460

Using a ashx handler is better cause it works on all browsers and you can cache the output images on the client.

However if you must do it, images can be displayed inline directly using the <img>tag as follows:

<img src="data:image/gif;base64,<YOUR BASE64 DATA>" width="100" height="100"/>

ASPX:

<img runat="server" id="imgCtrl" />

CS:

MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
imgCtrl.Src = "data:image/gif;base64," + base64Data;

Yes, you could write the bitmap directly, but compressed formats(JPEG, GIF) are better for the web.

Note: Inline images don't work on older browsers. Some versions of IE had limitations of max 32KB size.

Share:
23,460
Admin
Author by

Admin

Updated on February 06, 2020

Comments

  • Admin
    Admin over 4 years

    In my code I create a bitmap dynamically, using c# and ASP.NET. Than I need to display it on the asp image control. There are anyway to do it whithout using handlers?

  • Admin
    Admin over 11 years
    Thank u! How i get my Base64 Data,, sorry =/ If it is hard to explain, just tell me and I will find a way.
  • aiden87
    aiden87 over 7 years
    could u explain how to do it, using ashx handler?
  • nunespascal
    nunespascal over 7 years
    The code would be quite the same. You should just output the headers and byte data directly from the handler. Comments are great to share code. If you have a question on this, point me to it and I can paste the code there.