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

Image Size/Width Detection

1 Answer 295 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
WebGeek
Top achievements
Rank 1
WebGeek asked on 22 Oct 2010, 03:26 PM
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.
http://www.telerik.com/community/forums/aspnet-ajax/binary-image/change-image-size-different-for-landscape-portrait.aspx

Thanks

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 26 Oct 2010, 12:22 PM
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)
{
    base.OnPreRenderComplete(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;
...
public
static Image CreateImgFromBytes(byte[] image)
{
    Image img;
    using (MemoryStream stream = new MemoryStream(image))
    {
        try
        {
            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.

Regards,
Veli
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
Tags
BinaryImage
Asked by
WebGeek
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or