Monday, May 6, 2013

ASP.NET dynamic images without a separate aspx file

In the past, when I wanted to display a dynamically generated image in ASP.NET, I would have a standalone aspx page that would stream back the image bytes using a image ContentType.

I recently found out that you can also stream images by setting the src attribute of the image with encoded bytes. This can be easily done using the ASP.NET Image control. Assume you have "myobject" that contains an image byte[] property called "imagebytes":

image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(myobject.imagebytes);

This will increase the size of your HTML since the image bytes are encoded directly into the HTML source. But hey, you don't need a separate aspx file.

Yippie.

No comments:

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...