Click here to Skip to main content
13,355,538 members (72,641 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 31 Mar 2009

Control to Display Binary Images in ASP.NET

, 31 Mar 2009
Rate this:
Please Sign up or sign in to vote.
The RbmBinaryImage control will help you display images directly from your database. You could bind the Image field directly to the ImageContent property, and also specify whether you want the display to be as a thumbnail and provide the thumbnail size.


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 and provide the thumbnail size.

Using the RbmBinaryImage Control

First, you need to add reference RbmControls.dll, then place the code below in the system.web section in your web.config.

   <add verb="GET" path="__RbmImageHandler.rbm"

   type="RbmControls.RbmImageHandler" />

Then, in the page you want to use the control, register it by using:

<%@ Register Assembly="RbmControls" Namespace="RbmControls" TagPrefix="Rbm" %>

You could bind the control directly using the ImageContent property. Also, you can specify whether to display the image as a thumbnail, and specify an image to display when 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 add the functionality of storing and rendering a binary image. It also adds the ability to generate a thumbnail based on a specified size and also caches your image.

The Imagecontent property retrieves and stores 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
        byte[] imageBytes = ViewState["ImageContent"] as byte[];
        if (!DisplayThumbnail)
            return (imageBytes == null) ? null : imageBytes;
        else if (imageBytes != null)
            byte[] bytes = CreateThumb();
            return bytes;
            return null;
        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)
    if (DesignMode)
    if (ImageContent != null)
            CacheKey = "Rbm" + System.DateTime.Now.Ticks;
        ImageUrl = String.Format("~/__RbmImageHandler.rbm?MimeType={0}" + 
                   MimeType, EnableCachinge ? "1" : "0", CacheKey);
        if(this.Context.Cache[CacheKey] == null)
            this.Context.Cache[CacheKey] = ImageContent;
    else if (ImageUrl == "" || ImageUrl == null)
        ImageUrl = EmptyImageUrl;

The RbmImageHandler class implements the IHttpHandler interface and overrides the ProcessRequest method to display the image:

public void ProcessRequest(HttpContext context)

    #region Caching Properties
    string cacheKey = context.Request["ImageContent"];
    if (String.IsNullOrEmpty(cacheKey))
    bool enableCaching = false;
    if (!String.IsNullOrEmpty(context.Request["EnableCaching"]))
        Boolean.TryParse(context.Request["EnableCaching"], out enableCaching);
    #region Image Properties
    string mimeType = context.Request["MimeType"];
    if (string.IsNullOrEmpty(mimeType))
        mimeType = "image/jpeg";
    byte []imageData = context.Cache[cacheKey] as byte[];

    if (!enableCaching)
    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 add to it, kindly share your thoughts here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Egypt Egypt
Fun Coder Smile | :) My Job is my Hobby Smile | :)

You may also be interested in...


Comments and Discussions

QuestionMemory Leaks Detected Pin
janologic8-Oct-13 6:44
memberjanologic8-Oct-13 6:44 
Questionon win server it is not show picture in firefox (a project published in IIs) Pin
faezeh6624-Oct-11 20:40
memberfaezeh6624-Oct-11 20:40 
QuestionVery useful and Great Program.Thanks a lot. Pin
cho zin oo27-Jul-11 1:39
membercho zin oo27-Jul-11 1:39 
Very useful and great Program. Thanks a lot.
GeneralMy vote of 5 Pin
cho zin oo27-Jul-11 1:36
membercho zin oo27-Jul-11 1:36 
GeneralWhy it only works using Visual Studio but not on IIS Pin
cyen200130-Oct-09 13:24
membercyen200130-Oct-09 13:24 
GeneralThank you Pin
sam4u2u10-Jul-09 4:57
membersam4u2u10-Jul-09 4:57 
GeneralHi!!!! Pin
Wilson Perez19-May-09 7:53
memberWilson Perez19-May-09 7:53 
GeneralRe: Hi!!!! Pin
Ahmed IG5-Aug-09 1:34
memberAhmed IG5-Aug-09 1:34 
GeneralRe: Hi!!!! Pin
RMostafa16-Sep-09 23:18
memberRMostafa16-Sep-09 23:18 
SuggestionASP.NET applications running on IIS7 Implementation Pin
chivaz11-Mar-13 4:43
memberchivaz11-Mar-13 4:43 
GeneralRe: Hi!!!! Pin
Member 795392726-May-11 7:44
memberMember 795392726-May-11 7:44 
GeneralThanks for the control It works Great!! Pin
miggitty9-May-09 6:46
membermiggitty9-May-09 6:46 
Generala similar control Pin
conannb28-Apr-09 21:42
memberconannb28-Apr-09 21:42 
GeneralDatabase missing Pin
~~Atul~~7-Apr-09 0:34
member~~Atul~~7-Apr-09 0:34 
GeneralRe: Database missing Pin
RMostafa11-Apr-09 22:15
memberRMostafa11-Apr-09 22:15 
GeneralHm-m... Pin
Danila Korablin31-Mar-09 9:37
memberDanila Korablin31-Mar-09 9:37 
AnswerRe: Hm-m... [modified] Pin
Danila Korablin31-Mar-09 21:32
memberDanila Korablin31-Mar-09 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by rmostafa
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid