Convert a RadBinaryImage into System.Drawing.Image

3 posts, 0 answers
  1. Richard
    Richard avatar
    14 posts
    Member since:
    Apr 2011

    Posted 26 Apr 2012 Link to this post

    Hi

    I would like to write an aspx page that uses a RadBinaryImage to load an image from a database with a table structured as follows.

    ImageID, ImageData
    10, 12345678901234567890

    With ImageID a key and ImageData the binary image data for a RadBinaryImage control.

    This page (imageresize.aspx) should load the imge with an ImageID specified on a QueryString from the database, then resize the image and finally set the response contentType to "image/jpeg" and write out the image.

    I would like to be able to include it on another page in another site, e.g. main.html.  The HTML for main.html might include <img src=imageresize.aspx?ID=10>. 

    Is there any way I can convert a RadBinaryImage into System.Drawing.Image to be able to write it out, or is there any other way of doing this?

    Kind Regards, Richard Jonas
  2. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 01 May 2012 Link to this post

    Hi Richard,

    This could not be done with RadBinaryImage but you could try using a custom HTTP handler in order to populate an <img> with the binary data on your page. You can see in the source code of our ImageGallery example an example of such approach:
    http://demos.telerik.com/aspnet-ajax/imagegallery/default.aspx

    The RadListView control's client item template contains an img element pouplate through a custom handler:
    <img src="CustomHandlers/Images.ashx?photoId=#=PhotoID#"></img>

    where #=PhotoID# is evaluated into a photo id from the DB. The code of the called handler follows bellow:
    public class Images : IHttpHandler
    {
        public const string PhotoID = "photoId";
     
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request[PhotoID]))
            {
                int photoId = Convert.ToInt32(context.Request[PhotoID]);
                Photo currentPhoto = PhotoDAO.GetPhotoById(photoId);
                byte[] binaryData = currentPhoto.Data;
                if (binaryData != null)
                {
                    context.Response.ContentType = "image";
                    context.Response.AddHeader("content-disposition", "inline; filename=" + currentPhoto.Title.Replace(' ', '_'));
                    context.Response.BinaryWrite(binaryData);
                }
            }
        }
     
        public bool IsReusable
        {
            get { return true; }
        }
    }

    I hope this helps.

    All the best,
    Tsvetina
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard
    Richard avatar
    14 posts
    Member since:
    Apr 2011

    Posted 01 May 2012 Link to this post

    Thanks Tsvetina - I'll give this a go and let you know how I get on.

    Kind Regards, Richard
Back to Top