Last active
December 19, 2015 14:59
-
-
Save huanlin/5973459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.Linq; | |
using System.Text; | |
namespace HuanlinLib.Drawing | |
{ | |
public enum ExifOrientations | |
{ | |
Unknown = 0, | |
TopLeft = 1, | |
TopRight = 2, | |
BottomRight = 3, | |
BottomLeft = 4, | |
LeftTop = 5, | |
RightTop = 6, | |
RightBottom = 7, | |
LeftBottom = 8 | |
} | |
/// <summary> | |
/// Reference: http://www.vb-helper.com/howto_net_read_exif_orientation.html | |
/// </summary> | |
public static class ExifHelper | |
{ | |
private const int OrientationID = 0x112; | |
/// <summary> | |
/// Return the image's orientation. | |
/// </summary> | |
/// <param name="img"></param> | |
/// <returns></returns> | |
public static ExifOrientations ImageOrientation(Image img) | |
{ | |
// Get the index of the orientation property. | |
int orientation_index = Array.IndexOf(img.PropertyIdList, OrientationID); | |
// If there is no such property, return Unknown. | |
if ((orientation_index < 0)) | |
return ExifOrientations.Unknown; | |
// Return the orientation value. | |
return (ExifOrientations)img.GetPropertyItem(OrientationID).Value[0]; | |
} | |
/// <summary> | |
/// Make an image to demonstrate orientations. | |
/// </summary> | |
/// <param name="orientation"></param> | |
/// <returns></returns> | |
public static Image OrientationImage(ExifOrientations orientation) | |
{ | |
const int size = 64; | |
Bitmap bm = new Bitmap(64, 64); | |
using (Graphics gr = Graphics.FromImage(bm)) | |
{ | |
gr.Clear(Color.White); | |
gr.TextRenderingHint = System.Drawing.Text.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 the_font = new Font("Times New Roman", 40, GraphicsUnit.Point)) | |
{ | |
if ((orientation == ExifOrientations.Unknown)) | |
{ | |
gr.DrawString("?", the_font, Brushes.Black, 0, 0, string_format); | |
} | |
else | |
{ | |
gr.DrawString("F", the_font, Brushes.Black, 0, 0, string_format); | |
} | |
} | |
} | |
} | |
return bm; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.Drawing.Imaging; | |
using System.Linq; | |
using System.Text; | |
namespace HuanlinLib.Drawing | |
{ | |
public static class ImageHelper | |
{ | |
/// <summary> | |
/// Creates a scaled thumbnail. | |
/// </summary> | |
/// <param name="width">The maximum width of the thumbnail to create.</param> | |
/// <param name="height">The maximum height of the thumbnail to create.</param> | |
/// <param name="interpolationMode">The Interpolation of the thumbnailing (HighQualityBicubic provides best quality)</param> | |
/// <returns>A bitmap thumbnail of the source image.</returns> | |
public static Bitmap CreateThumbnail(Bitmap aBitmap, int width, int height, InterpolationMode interpolationMode) | |
{ | |
//Calculate scales | |
float x = ((float)aBitmap.Width / (float)width); | |
float y = ((float)aBitmap.Height / (float)height); | |
float factor = Math.Max(x, y); | |
if (factor < 1) | |
factor = 1; | |
int thWidth = (int)Math.Round((aBitmap.Width / factor), 0); | |
int thHeight = (int)Math.Round((aBitmap.Height / factor), 0); | |
// Set the size of the target image | |
Bitmap bmpTarget = new Bitmap(thWidth, thHeight); | |
Graphics grfxThumb = Graphics.FromImage(bmpTarget); | |
grfxThumb.InterpolationMode = interpolationMode; | |
// Draw the original image to the target image | |
grfxThumb.DrawImage(aBitmap, new Rectangle(0, 0, thWidth, Convert.ToInt32(thWidth * aBitmap.Height / aBitmap.Width))); | |
grfxThumb.Dispose(); | |
return bmpTarget; | |
} | |
public static Bitmap CreateThumbnail(Bitmap aBitmap, int width, int height) | |
{ | |
return CreateThumbnail(aBitmap, width, height, InterpolationMode.Default); | |
} | |
public static void CreateThumbnail(string srcFileName, string dstFileName, int width, int height) | |
{ | |
Bitmap srcBitmap = new Bitmap(srcFileName); | |
Bitmap dstBitmap = CreateThumbnail(srcBitmap, width, height); | |
dstBitmap.Save(dstFileName); | |
} | |
public static Bitmap RotateImageBasedOnExif(Image img) | |
{ | |
Bitmap bmpTarget = new Bitmap(img); | |
ExifOrientations exifo = ExifHelper.ImageOrientation(img); | |
switch (exifo) | |
{ | |
case ExifOrientations.RightTop: | |
bmpTarget.RotateFlip(RotateFlipType.Rotate90FlipNone); | |
break; | |
case ExifOrientations.BottomRight: | |
bmpTarget.RotateFlip(RotateFlipType.Rotate180FlipNone); | |
break; | |
case ExifOrientations.LeftBottom: | |
bmpTarget.RotateFlip(RotateFlipType.Rotate270FlipNone); | |
break; | |
default: | |
break; | |
} | |
return bmpTarget; | |
} | |
public static Bitmap RotateImageBasedOnExif(string filename) | |
{ | |
Image img = Image.FromFile(filename); | |
return RotateImageBasedOnExif(img); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment