New to Telerik Document Processing? Download free 30-day trial

Handle the import of JPEG images with orientation set in their metadata different than 0 (normal) | Telerik Document Processing

Product Version Product Author
2022.2.620 RadPdfProcessing Martin Velikov

Description

This article shows how to rotate a JPEG image with orientation set in its metadata (EXIF data) and then insert it into a RadFixedPage.

EXIF data is useful information about a JPEG image, hidden inside the file`s metadata. When images are photographed, digital cameras use orientation sensors to store an EXIF orientation value for how the camera is held. The same behavior can be observed when using specific image processing software as well.

Solution

In the following example, we are using the System.Drawing.Bitmap class in order to load a rotated JPEG image and then pass it to a helper method to rotate the image according to the appropriate rotation angle. After the image is successfully rotated we are inserting it into the RadFixedPage.

Insert a JPEG image with orientation set in its metadata into a RadFixedDocument using a helper method

string imagePath = "Progress_DevCraft_rotated.jpg"; 
Image bitmap = new Bitmap(imagePath); 
Image rotatedBitmap = ExifRotate(bitmap); 
 
MemoryStream ms = new MemoryStream(); 
rotatedBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
 
ImageSource imageSource = new ImageSource(ms); 
 
var image = new Telerik.Windows.Documents.Fixed.Model.Objects.Image 
{ 
    ImageSource = imageSource 
}; 
 
RadFixedDocument document = new RadFixedDocument(); 
RadFixedPage page = document.Pages.AddPage(); 
page.Content.Add(image); 
Here is an example of rotating the image according to its EXIF orientation.

The helper method

public static Image ExifRotate(Image img) 
{ 
    int exifOrientationID = 0x0112; 
 
    if (!img.PropertyIdList.Contains(exifOrientationID)) 
    { 
        return img; 
    } 
 
    var prop = img.GetPropertyItem(exifOrientationID); 
    int val = BitConverter.ToUInt16(prop.Value, 0); 
    RotateFlipType rot = RotateFlipType.RotateNoneFlipNone; 
 
    if (val == 3 || val == 4) 
    { 
        rot = RotateFlipType.Rotate180FlipNone; 
    } 
    else if (val == 5 || val == 6) 
    { 
        rot = RotateFlipType.Rotate90FlipNone; 
    } 
    else if (val == 7 || val == 8) 
    { 
        rot = RotateFlipType.Rotate270FlipNone; 
    } 
 
    if (val == 2 || val == 4 || val == 5 || val == 7) 
    { 
        rot |= RotateFlipType.RotateNoneFlipX; 
    } 
 
    if (rot != RotateFlipType.RotateNoneFlipNone) 
    { 
        img.RotateFlip(rot); 
    } 
 
    return img; 
} 
In this article