forked from JimBobSquarePants/ImageProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolution.cs
More file actions
104 lines (92 loc) · 3.78 KB
/
Copy pathResolution.cs
File metadata and controls
104 lines (92 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Resolution.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to change the resolution of the image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging.MetaData;
/// <summary>
/// Encapsulates methods to change the resolution of the image.
/// </summary>
public class Resolution : IGraphicsProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="Resolution"/> class.
/// </summary>
public Resolution() => this.Settings = new Dictionary<string, string>();
/// <summary>
/// Gets or sets the dynamic parameter.
/// </summary>
public dynamic DynamicParameter
{
get;
set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings
{
get;
set;
}
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
/// the image to process.
/// </param>
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory)
{
const float InchInCm = 0.3937007874015748f;
Image image = factory.Image;
try
{
Tuple<int, int, PropertyTagResolutionUnit> resolution = this.DynamicParameter;
// Set the bitmap resolution data.
// Ensure that the resolution is recalculated for bitmap since it only
// supports inches.
if (resolution.Item3 == PropertyTagResolutionUnit.Cm)
{
float horizontal = resolution.Item1 / InchInCm;
float vertical = resolution.Item2 / InchInCm;
((Bitmap)image).SetResolution(horizontal, vertical);
}
else
{
((Bitmap)image).SetResolution(resolution.Item1, resolution.Item2);
}
if (factory.PreserveExifData && factory.ExifPropertyItems.Count > 0)
{
// Set the horizontal EXIF data.
var horizontalRational = new Rational<uint>((uint)resolution.Item1, 1);
factory.SetPropertyItem(ExifPropertyTag.XResolution, horizontalRational);
// Set the vertical EXIF data.
var verticalRational = new Rational<uint>((uint)resolution.Item2, 1);
factory.SetPropertyItem(ExifPropertyTag.YResolution, verticalRational);
// Set the unit EXIF data
ushort units = (ushort)resolution.Item3;
factory.SetPropertyItem(ExifPropertyTag.ResolutionUnit, units);
}
}
catch (Exception ex)
{
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return image;
}
}
}