[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Show sample EXIF orientations in C#

[Show sample EXIF orientations in C#]

This example creates an image that draws the letter F in various ways to demonstrate EXIF orientations. Note that this example doesn't actually use EXIF data. It just draws some Fs to use as a reference for the orientations.

The program's ExifStuff class includes several methods for working with EXIF data. It also dfeines the following method to generate the reference image.

// Make an image to demonstrate EXIF orientations. public static Image OrientationImage(ExifOrientations orientation) { const int size = 64; Bitmap bm = new Bitmap(size, size); using (Graphics gr = Graphics.FromImage(bm)) { gr.Clear(Color.White); gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Orient the result. switch (orientation) { case ExifOrientations.TopLeft: break; case ExifOrientations.TopRight: gr.ScaleTransform(-1, 1); break; case ExifOrientations.BottomRight: gr.RotateTransform(180); break; case ExifOrientations.BottomLeft: gr.ScaleTransform(1, -1); break; case ExifOrientations.LeftTop: gr.RotateTransform(90); gr.ScaleTransform(-1, 1, MatrixOrder.Append); break; case ExifOrientations.RightTop: gr.RotateTransform(-90); break; case ExifOrientations.RightBottom: gr.RotateTransform(90); gr.ScaleTransform(1, -1, MatrixOrder.Append); break; case ExifOrientations.LeftBottom: gr.RotateTransform(90); break; } // Translate the result to the center of the bitmap. gr.TranslateTransform( size / 2, size / 2, MatrixOrder.Append); using (StringFormat string_format = new StringFormat()) { string_format.LineAlignment = StringAlignment.Center; string_format.Alignment = StringAlignment.Center; using (Font font = new Font("Times New Roman", 40, GraphicsUnit.Point)) { if (orientation == ExifOrientations.Unknown) { gr.DrawString("?", font, Brushes.Black, 0, 0, string_format); } else { gr.DrawString("F", font, Brushes.Black, 0, 0, string_format); } } } } return bm; }

This method demonstrates some interesting graphics transformations. First it creates a Bitmap and an associated Graphics object. Then depending on the desired orientation, it applies a transformation to the Graphics object to appropriately rotate and flip the drawing that follows.

Next the code applies a translation to center the resulting image in the Bitmap. Finally the method draws the letter F centered at the origin. The transformations rotate and flip the result and then translate it to center it in the Bitmap.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.