📄 exifpropertyformatter.cs
字号:
if (format == FormatInstruction.ALLCHAR)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
strRet = encoding.GetString(propItem.Value, 0, propItem.Len);
}
else if (format == FormatInstruction.UNICODE)
{
System.Text.UnicodeEncoding unicode = new System.Text.UnicodeEncoding();
strRet = unicode.GetString(propItem.Value, 0, propItem.Len);
}
else if (format == FormatInstruction.USERCOMMENT)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string encodingTag = encoding.GetString(propItem.Value, 0, 8).ToUpper();
if (encodingTag.StartsWith("ASCII"))
{
strRet = encoding.GetString(propItem.Value, 8, propItem.Len-8);
}
else if (encodingTag.StartsWith("JIS"))
{
System.Text.Encoding shiftJis = System.Text.Encoding.GetEncoding(932);
strRet = shiftJis.GetString(propItem.Value, 8, propItem.Len-8);
}
else if (encodingTag.StartsWith("UNICODE"))
{
System.Text.UnicodeEncoding unicode = new System.Text.UnicodeEncoding();
strRet = unicode.GetString(propItem.Value, 8, propItem.Len-8);
}
else if (encodingTag.StartsWith("\0\0\0\0\0\0\0\0"))
{
strRet = "";
}
else
{
strRet = encoding.GetString(propItem.Value, 0, propItem.Len);
}
}
else
{
strRet = BitConverter.ToString(propItem.Value, 0, propItem.Len);
}
return strRet;
}
/// <summary>
/// Format a SLong tag (signed).
/// </summary>
/// <param name="propItem">The <see cref="PropertyItem"/> to format.</param>
/// <returns>A formatted signed Long (<see cref="Int32"/> string.</returns>
private static string FormatTagSLong(PropertyItem propItem)
{
string strRet = "";
for (int i = 0; i < propItem.Len; i = i + BYTEJUMP_SLONG)
{
System.Int32 val = BitConverter.ToInt32(propItem.Value, i);
strRet += val.ToString();
if (i + BYTEJUMP_SLONG < propItem.Len) strRet += " ";
}
return strRet;
}
/// <summary>
/// Format a SRational tag (signed).
/// </summary>
/// <param name="propItem">The <see cref="PropertyItem"/> to format.</param>
/// <param name="FormatInstruction">The <see cref="FormatInstruction"/> to use for formatting.</param>
/// <returns>A formatted signed Rational string.</returns>
/// <remarks>By defailt a string will be formatted as decimal unless the <see cref="FormatInstruction"/> is <see cref="FormatInstruction.FRACTION"/></remarks>
private static string FormatTagSRational(PropertyItem propItem, FormatInstruction format)
{
string strRet = "";
for (int i = 0; i < propItem.Len; i = i + BYTEJUMP_SRATIONAL)
{
System.Int32 numer = BitConverter.ToInt32(propItem.Value, i);
System.Int32 denom = BitConverter.ToInt32(propItem.Value, i + BYTEJUMP_SLONG);
if (format == FormatInstruction.FRACTION)
{
strRet += ToRationalString(numer, denom);
}
else
{
double dbl;
if (denom == 0) dbl = 0.0;
else dbl = (double)numer / (double)denom;
strRet += dbl.ToString(DOUBLETYPE_FORMAT);
}
if (i + BYTEJUMP_SRATIONAL < propItem.Len) strRet += " ";
}
return strRet;
}
#region Formatting a Rational
// //////////////////////////////////////////////////////////
// Formatting a Rational
// ---------------------
// Modified from:
// Java Software Solutions: Foundations of Program Design e/3
// by John Lewis and William Loftus
// Published by Addison Wesley
// ISBN: 0-201-78129-8
// Copyright 2003
// //////////////////////////////////////////////////////////
// Format a signed rational
// //////////////////////////////////////////////////////////
/// <summary>
/// Converts the signed numerator and denominator values
/// to its equivalent string representation.
/// A fraction (x/y) is returned when applicable.</summary>
/// <param name="numer">The numeratior.</param>
/// <param name="denom">The denominator.</param>
/// <returns>A formatted Rational string.</returns>
private static string ToRationalString(Int32 numer, Int32 denom)
{
if (denom == 0) denom = 1;
// Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
ReduceRational(ref numer, ref denom);
string result;
if (numer == 0) result = "0";
else if (denom == 1) result = numer + "";
else result = numer + "/" + denom;
return result;
}
/// <summary>
/// Reduces the rational number by dividing both the numerator
/// and the denominator by their greatest common divisor.
/// </summary>
/// <param name="numer">The numeratior.</param>
/// <param name="denom">The denominator.</param>
private static void ReduceRational(ref Int32 numer, ref Int32 denom)
{
if (numer != 0)
{
Int32 common = GCD(Math.Abs(numer), denom);
numer = numer / common;
denom = denom / common;
}
}
/// <summary>
/// Computes and returns the greatest common divisor of the two
/// positive parameters. Uses Euclid's algorithm.
/// </summary>
/// <param name="num1">First number.</param>
/// <param name="num2">Second number.</param>
/// <returns>The greated common denominator as a string.</returns>
private static Int32 GCD(Int32 num1, Int32 num2)
{
while (num1 != num2)
{
if (num1 > num2) num1 = num1 - num2;
else num2 = num2 - num1;
}
return num1;
}
// //////////////////////////////////////////////////////////
// Format an unsigned rational
// //////////////////////////////////////////////////////////
/// <summary>
/// Converts the unsigned numerator and denominator values
/// to its equivalent string representation.
/// A fraction is used when applicable.
/// </summary>
/// <param name="numer">The numeratior.</param>
/// <param name="denom">The denominator.</param>
/// <returns>A formatted Rational string.</returns>
private static string ToRationalString(UInt32 numer, UInt32 denom)
{
if (denom == 0) denom = 1;
ReduceRational(ref numer, ref denom);
string result;
if (numer == 0) result = "0";
else if (denom == 1) result = numer + "";
else result = numer + "/" + denom;
return result;
}
/// <summary>
/// Reduces the rational number by dividing both the numerator
/// and the denominator by their greatest common divisor.
/// </summary>
/// <param name="numer">The numeratior.</param>
/// <param name="denom">The denominator.</param>
private static void ReduceRational(ref UInt32 numer, ref UInt32 denom)
{
if (numer != 0)
{
UInt32 common = GCD(numer, denom);
numer = numer / common;
denom = denom / common;
}
}
/// <summary>
/// Computes and returns the greatest common divisor of the two
/// positive parameters. Uses Euclid's algorithm.
/// </summary>
/// <param name="num1">First number.</param>
/// <param name="num2">Second number.</param>
/// <returns>The greated common denominator as a string.</returns>
private static UInt32 GCD(UInt32 num1, UInt32 num2)
{
while (num1 != num2)
{
if (num1 > num2) num1 = num1 - num2;
else num2 = num2 - num1;
}
return num1;
}
#endregion
#region Special Formatting Operations
/// <summary>The possible specialized formatting instruction
/// when reading an image's tag property value.</summary>
public enum FormatInstruction
{
/// <summary>No formatting instruction</summary>
NO_OP,
/// <summary>Instruction to change the value to a fraction.</summary>
/// <remarks>This is only applicable to RATIONAL and SRATIONAL tags.</remarks>
FRACTION,
/// <summary>Instruction to change the value to a decimal.</summary>
/// <remarks>This is only applicable to RATIONAL and SRATIONAL tags.</remarks>
DECIMAL,
/// <summary>Instruction to change the value to a date/time.</summary>
/// <remarks>This is only applicable to ASCII tags.</remarks>
DATETIME,
/// <summary>Instruction to format the bytes as a non-null terminated string.</summary>
/// <remarks>This is only applicable to UNDEFINED tags.</remarks>
ALLCHAR,
/// <summary>Instruction to format the bytes as a non-null terminated string.
/// That can be encoded using either ASCII, JIS, Unicode or Undefined</summary>
/// <remarks>This is only applicable to UNDEFINED tags.</remarks>
USERCOMMENT,
/// <summary>Instruction to format the bytes as a non-null terminated unicode string.
/// </summary>
UNICODE,
/// <summary>Instruction to format the bytes as a Base-64 string.</summary>
/// <remarks>This is only applicable to BYTE tags.</remarks>
BASE64
}
public static FormatInstruction FindSpecialFormat(ExifProperty property)
{
switch(property)
{
case ExifProperty.ExposureTime:
return FormatInstruction.FRACTION;
case ExifProperty.ExifVersion:
case ExifProperty.UserComment:
case ExifProperty.FlashPixVersion:
return FormatInstruction.ALLCHAR;
case ExifProperty.FNumber:
case ExifProperty.FocalLength:
return FormatInstruction.DECIMAL;
case ExifProperty.DateTime:
case ExifProperty.DateTimeDigitized:
case ExifProperty.DateTimeOriginal:
return FormatInstruction.DATETIME;
default:
return FormatInstruction.NO_OP;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -