Image Size/Width Detection

2 posts, 0 answers
  1. WebGeek
    WebGeek avatar
    40 posts
    Member since:
    Dec 2006

    Posted 22 Oct 2010 Link to this post

    I am trying to figure out what to do when an image I upload to my database is smaller than my max height/width that I have setup for resize on upload. 

    For example, I have my resize utlity resizing all images to be no larger than 1000 X 1200 pixels and this works great with RadBinary because it "fits" them into my various objects (thumbnail, full size, screen size, etc) at my specificied sizes (for example full size = 500 pixels width).  But my problem is sometimes I am supplied with photos that have already been resized to dimensions which are smaller than the ones I have setup for my project (for example I might be supplied with a 300 x 400 image).  This causes the image to be streched when I am using radbinary on my page because I have my sizes preset to 500 width.

    I would like to know if there is a way to detect the image size from the database, then see if it is smaller than my presets (500 px), if so, then it would need to show the image in its largest format size and if not, show it in my preset size format. 

    I found this article, but it is referring to using it in the grid, but I am not, I just have the files on my page.

  2. Veli
    Veli avatar
    2002 posts

    Posted 26 Oct 2010 Link to this post

    Hi medicalwebgeek,

    RadBinaryImage automatically calculates the image size based on the image data and sets its Width and Height properties if you do not explicitly specify them. However, this happens as late as the PreRender phase of the control. This means that you can get the calculated Width and Height of the RadBinaryImage at the earliest in the OnPreRenderComplete method of the page:

    protected override void OnPreRenderComplete(EventArgs e)
        Response.Write(RadBinaryImage1.Width + "<br />" + RadBinaryImage1.Height);

    Unfortunately, this point in the page life cycle may be too late for your you. Alternatively, you can create method that will take the image data in the form of a byte[] and convert it to a System.Drawing.Image instance:

    using System.Drawing;
    static Image CreateImgFromBytes(byte[] image)
        Image img;
        using (MemoryStream stream = new MemoryStream(image))
                img = Image.FromStream(stream);
            catch (ArgumentException ex)
                throw new ArgumentException("The provided binary data may not be valid image or may contains unknown header", ex);
        return img;

    The above static method will create an Image object out of  your image data. You can then use the image Width and Height properties to get the original image size. This is the most straightforward way of getting the size of your image from its binary data.

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top