This is a migrated thread and some comments may be shown as answers.

Convert a RadBinaryImage into System.Drawing.Image

2 Answers 179 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 26 Apr 2012, 08:01 PM
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 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 01 May 2012, 03:00 PM
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.
0
Richard
Top achievements
Rank 1
answered on 01 May 2012, 04:51 PM
Thanks Tsvetina - I'll give this a go and let you know how I get on.

Kind Regards, Richard
Tags
BinaryImage
Asked by
Richard
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Richard
Top achievements
Rank 1
Share this question
or