Control to Display Binary Images in ASP.NET
RBMBinaryImage Source Code
Introduction
The RbmBinaryImage control will help you display images directly from your database. You could bind the Image field directly to the ImageContent property, also you could specify whether you want the display to be as a thumbnail or not and provide the thumbnail size.
Using the RbmBinaryImage Control
First you need to add reference to the RbmControls.dll, then place the code below in the system.web section in your web.config
<httpHandlers> <add verb="GET" path="__RbmImageHandler.rbm" type="RbmControls.RbmImageHandler" /> </httpHandlers>
Then in the page you want to use the Control Register it by using
<%@ Register Assembly="RbmControls" Namespace="RbmControls" TagPrefix="Rbm" %>
you could either bind the control directly in the ImageContent Property, Also specify whether to display the image as a thumbnail. and specify an image to display when the ImageContent is empty
an alternative is to do that by code
RbmBinaryImage1.ImageContent = FileUpload1.FileBytes;
Using the Code
The RbmBinaryImage Class Inherits from System.Web.UI.WebControls.Image and the functionality of storing and rendering a binary image to it also it adds the ability to generate a thumbnail based on a specified size and caching your image.
The Imagecontent Property Retrieves and store the image bytes in the ViewState also if the DisplayThumbnail property is set to true then it retrieves the thumbnail of the image
The Imagecontent Property Retrieves and store the image bytes in the ViewState also if the DisplayThumbnail property is set to true then it retrieves the thumbnail of the image
public byte[] ImageContent
{
get
{
byte[] imageBytes = ViewState["ImageContent"] as byte[];
if (!DisplayThumbnail)
return (imageBytes == null) ? null : imageBytes;
else if (imageBytes != null)
{
byte[] bytes = CreateThumb();
return bytes;
}
else
return null;
}
set
{
ViewState["ImageContent"] = value;
}
}
The OnPreRender Method is used to set the ImageUrl Based on the Property Settings of the Image Control
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (DesignMode)
return;
if (ImageContent != null)
{
if(string.IsNullOrEmpty(CacheKey))
CacheKey = "Rbm" + System.DateTime.Now.Ticks;
ImageUrl = String.Format("~/__RbmImageHandler.rbm?MimeType={0}&EnableCaching={1}&ImageContent={2}",
MimeType, EnableCachinge ? "1" : "0", CacheKey);
if(this.Context.Cache[CacheKey] == null)
this.Context.Cache[CacheKey] = ImageContent;
}
else if (ImageUrl == "" || ImageUrl == null)
{
ImageUrl = EmptyImageUrl;
}
}
In the RbmImageHandler Class in implements the IHttpHandler Interface and override the ProcessRequest method to display the image
public void ProcessRequest(HttpContext context)
{
#region Caching Properties
string cacheKey = context.Request["ImageContent"];
if (String.IsNullOrEmpty(cacheKey))
return;
bool enableCaching = false;
if (!String.IsNullOrEmpty(context.Request["EnableCaching"]))
Boolean.TryParse(context.Request["EnableCaching"], out enableCaching);
#endregion
#region Image Properties
string mimeType = context.Request["MimeType"];
if (string.IsNullOrEmpty(mimeType))
mimeType = "image/jpeg";
byte []imageData = context.Cache[cacheKey] as byte[];
#endregion
if (!enableCaching)
context.Cache.Remove(cacheKey);
context.Response.ContentType = mimeType;
context.Response.OutputStream.Write(imageData, 0, imageData.Length);
}
Share your Thoughts
If you liked the control or have any comments on it or features you want to place in it kindly share your thoughts here


