Unplanned
Last Updated: 20 Nov 2013 15:58 by Keith
I'd like to be able to instantiate and databind a RadBinaryImage, then call a "GetUri" method on the instance to get the WebResource URI that would be generated in markup if the control were added to the page.

In a particular use case, this would allow putting the URI in a meta tag for OpenGraph calls.

An additional API method that would be handy would be a "handler" mode that would allow instantiation of a RadBinaryImage in a custom IHttpHandler implementation, and then serving that request from the final image (after scaling and cropping) generated by the RadBinaryImage control. I could see this taking two forms:

* GetMimeType and GetBytes methods, which could be used to manually populate the Response.
* a Respond method which would take an HttpContext argument, and would set appropriate headers, stream the image, and end the request itself.
Unplanned
Last Updated: 07 Apr 2020 16:26 by ADMIN

The same is observed with the Thumbnail images in the RadImageGallery, which actually use such BinaryImages.

Reproduction

Note: The attached image should be the only file in a "Images" in the root of the application

protected void Page_Load(object sender, EventArgs e)
{
    var binary = new RadBinaryImage();
    binary.AutoAdjustImageControlSize = false;
    binary.ResizeMode = BinaryImageResizeMode.Crop;

    binary.ID = "BinaryImage";
    binary.Width = Unit.Pixel(100);
    binary.Height = Unit.Pixel(100);

    var path = Directory.EnumerateFiles(Server.MapPath("~/Images")).First();
        
    binary.DataValue = File.ReadAllBytes(path);
    Form.Controls.Add(binary);
}

 

Why this is happening

This seems to be a known issue with some images and their EXIF information regarding the rotation. Detailed information on this matter and a solution could be found in the following forum thread:

Workaround

protected void Page_Load(object sender, EventArgs e)
{
    var binary = new RadBinaryImage();
    binary.ID = "BinaryImage1";
    binary.ResizeMode = BinaryImageResizeMode.Crop;

    binary.Width = Unit.Pixel(100);
    binary.Height = Unit.Pixel(100);

    var path = Directory.EnumerateFiles(Server.MapPath("~/Images")).First();
        
    binary.DataValue = ImagePathToByteArray(path);
    //binary.DataValue = File.ReadAllBytes(path);
    Form.Controls.Add(binary);
}
public byte[] ImagePathToByteArray(string path)
{
    System.Drawing.Image img = Bitmap.FromFile(path);
    if (Array.IndexOf(img.PropertyIdList, 274) > -1)
    {
        var orientation = (int)img.GetPropertyItem(274).Value[0];
        switch (orientation)
        {
            case 1:
                // No rotation required.
                break;
            case 2:
                img.RotateFlip(RotateFlipType.RotateNoneFlipX);
                break;
            case 3:
                img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;
            case 4:
                img.RotateFlip(RotateFlipType.Rotate180FlipX);
                break;
            case 5:
                img.RotateFlip(RotateFlipType.Rotate90FlipX);
                break;
            case 6:
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            case 7:
                img.RotateFlip(RotateFlipType.Rotate270FlipX);
                break;
            case 8:
                img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
        }
        // This EXIF data is now invalid and should be removed.
        img.RemovePropertyItem(274);
    }

    return ImageToByte(img);
}

public static byte[] ImageToByte(System.Drawing.Image img)
{
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}