I have created a custom server control and trying to add an image using System.Web.UI.WebControls.Image. I would like to use an image from memory stream or bitmap. How can I do this, it seems the WebControls.Image requires ImageURL.
I don’t have a static image, the image is created on the fly. Thanks.
There’s two options here:
The first option is the most common method and highly recommended if your image is more than a few kilobytes in size. You’d implement a HttpHandler that streams back the generated image. In your page set the
ImageUrlin the page to a url pointing to your handler. You could include data in the image url to instruct the handler on how to render the image or what data to use.You should realize though that anyone can call this handler so if your images contain sensitive or personal information, make sure you check if the request is authorized.
The second option is to encode the image as base64 data directly into your page. Since base64 encoding blows up the data size by 137% it’s suitable for small images only. You’ll also get no support at all from the standard server controls so I you’ll have to implement this from scratch.