This is a C# version of the same code:
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace ExifWorks<br />
{<br />
public class ExifManager : IDisposable<br />
{<br />
<br />
private System.Drawing.Bitmap _Image;<br />
private System.Text.Encoding _Encoding = System.Text.Encoding.UTF8;<br />
<br />
#region Type declarations<br />
<br />
<br />
public enum TagNames : int<br />
{<br />
ExifIFD = 0x8769,<br />
GpsIFD = 0x8825,<br />
NewSubfileType = 0xFE,<br />
SubfileType = 0xFF,<br />
ImageWidth = 0x100,<br />
ImageHeight = 0x101,<br />
BitsPerSample = 0x102,<br />
Compression = 0x103,<br />
PhotometricInterp = 0x106,<br />
ThreshHolding = 0x107,<br />
CellWidth = 0x108,<br />
CellHeight = 0x109,<br />
FillOrder = 0x10A,<br />
DocumentName = 0x10D,<br />
ImageDescription = 0x10E,<br />
EquipMake = 0x10F,<br />
EquipModel = 0x110,<br />
StripOffsets = 0x111,<br />
Orientation = 0x112,<br />
SamplesPerPixel = 0x115,<br />
RowsPerStrip = 0x116,<br />
StripBytesCount = 0x117,<br />
MinSampleValue = 0x118,<br />
MaxSampleValue = 0x119,<br />
XResolution = 0x11A,<br />
YResolution = 0x11B,<br />
PlanarConfig = 0x11C,<br />
PageName = 0x11D,<br />
XPosition = 0x11E,<br />
YPosition = 0x11F,<br />
FreeOffset = 0x120,<br />
FreeByteCounts = 0x121,<br />
GrayResponseUnit = 0x122,<br />
GrayResponseCurve = 0x123,<br />
T4Option = 0x124,<br />
T6Option = 0x125,<br />
ResolutionUnit = 0x128,<br />
PageNumber = 0x129,<br />
TransferFuncition = 0x12D,<br />
SoftwareUsed = 0x131,<br />
DateTime = 0x132,<br />
Artist = 0x13B,<br />
HostComputer = 0x13C,<br />
Predictor = 0x13D,<br />
WhitePoint = 0x13E,<br />
PrimaryChromaticities = 0x13F,<br />
ColorMap = 0x140,<br />
HalftoneHints = 0x141,<br />
TileWidth = 0x142,<br />
TileLength = 0x143,<br />
TileOffset = 0x144,<br />
TileByteCounts = 0x145,<br />
InkSet = 0x14C,<br />
InkNames = 0x14D,<br />
NumberOfInks = 0x14E,<br />
DotRange = 0x150,<br />
TargetPrinter = 0x151,<br />
ExtraSamples = 0x152,<br />
SampleFormat = 0x153,<br />
SMinSampleValue = 0x154,<br />
SMaxSampleValue = 0x155,<br />
TransferRange = 0x156,<br />
JPEGProc = 0x200,<br />
JPEGInterFormat = 0x201,<br />
JPEGInterLength = 0x202,<br />
JPEGRestartInterval = 0x203,<br />
JPEGLosslessPredictors = 0x205,<br />
JPEGPointTransforms = 0x206,<br />
JPEGQTables = 0x207,<br />
JPEGDCTables = 0x208,<br />
JPEGACTables = 0x209,<br />
YCbCrCoefficients = 0x211,<br />
YCbCrSubsampling = 0x212,<br />
YCbCrPositioning = 0x213,<br />
REFBlackWhite = 0x214,<br />
ICCProfile = 0x8773,<br />
Gamma = 0x301,<br />
ICCProfileDescriptor = 0x302,<br />
SRGBRenderingIntent = 0x303,<br />
ImageTitle = 0x320,<br />
Copyright = 0x8298,<br />
ResolutionXUnit = 0x5001,<br />
ResolutionYUnit = 0x5002,<br />
ResolutionXLengthUnit = 0x5003,<br />
ResolutionYLengthUnit = 0x5004,<br />
PrintFlags = 0x5005,<br />
PrintFlagsVersion = 0x5006,<br />
PrintFlagsCrop = 0x5007,<br />
PrintFlagsBleedWidth = 0x5008,<br />
PrintFlagsBleedWidthScale = 0x5009,<br />
HalftoneLPI = 0x500A,<br />
HalftoneLPIUnit = 0x500B,<br />
HalftoneDegree = 0x500C,<br />
HalftoneShape = 0x500D,<br />
HalftoneMisc = 0x500E,<br />
HalftoneScreen = 0x500F,<br />
JPEGQuality = 0x5010,<br />
GridSize = 0x5011,<br />
ThumbnailFormat = 0x5012,<br />
ThumbnailWidth = 0x5013,<br />
ThumbnailHeight = 0x5014,<br />
ThumbnailColorDepth = 0x5015,<br />
ThumbnailPlanes = 0x5016,<br />
ThumbnailRawBytes = 0x5017,<br />
ThumbnailSize = 0x5018,<br />
ThumbnailCompressedSize = 0x5019,<br />
ColorTransferFunction = 0x501A,<br />
ThumbnailData = 0x501B,<br />
ThumbnailImageWidth = 0x5020,<br />
ThumbnailImageHeight = 0x502,<br />
ThumbnailBitsPerSample = 0x5022,<br />
ThumbnailCompression = 0x5023,<br />
ThumbnailPhotometricInterp = 0x5024,<br />
ThumbnailImageDescription = 0x5025,<br />
ThumbnailEquipMake = 0x5026,<br />
ThumbnailEquipModel = 0x5027,<br />
ThumbnailStripOffsets = 0x5028,<br />
ThumbnailOrientation = 0x5029,<br />
ThumbnailSamplesPerPixel = 0x502A,<br />
ThumbnailRowsPerStrip = 0x502B,<br />
ThumbnailStripBytesCount = 0x502C,<br />
ThumbnailResolutionX = 0x502D,<br />
ThumbnailResolutionY = 0x502E,<br />
ThumbnailPlanarConfig = 0x502F,<br />
ThumbnailResolutionUnit = 0x5030,<br />
ThumbnailTransferFunction = 0x5031,<br />
ThumbnailSoftwareUsed = 0x5032,<br />
ThumbnailDateTime = 0x5033,<br />
ThumbnailArtist = 0x5034,<br />
ThumbnailWhitePoint = 0x5035,<br />
ThumbnailPrimaryChromaticities = 0x5036,<br />
ThumbnailYCbCrCoefficients = 0x5037,<br />
ThumbnailYCbCrSubsampling = 0x5038,<br />
ThumbnailYCbCrPositioning = 0x5039,<br />
ThumbnailRefBlackWhite = 0x503A,<br />
ThumbnailCopyRight = 0x503B,<br />
LuminanceTable = 0x5090,<br />
ChrominanceTable = 0x5091,<br />
FrameDelay = 0x5100,<br />
LoopCount = 0x5101,<br />
PixelUnit = 0x5110,<br />
PixelPerUnitX = 0x5111,<br />
PixelPerUnitY = 0x5112,<br />
PaletteHistogram = 0x5113,<br />
ExifExposureTime = 0x829A,<br />
ExifFNumber = 0x829D,<br />
ExifExposureProg = 0x8822,<br />
ExifSpectralSense = 0x8824,<br />
ExifISOSpeed = 0x8827,<br />
ExifOECF = 0x8828,<br />
ExifVer = 0x9000,<br />
ExifDTOrig = 0x9003,<br />
ExifDTDigitized = 0x9004,<br />
ExifCompConfig = 0x9101,<br />
ExifCompBPP = 0x9102,<br />
ExifShutterSpeed = 0x9201,<br />
ExifAperture = 0x9202,<br />
ExifBrightness = 0x9203,<br />
ExifExposureBias = 0x9204,<br />
ExifMaxAperture = 0x9205,<br />
ExifSubjectDist = 0x9206,<br />
ExifMeteringMode = 0x9207,<br />
ExifLightSource = 0x9208,<br />
ExifFlash = 0x9209,<br />
ExifFocalLength = 0x920A,<br />
ExifMakerNote = 0x927C,<br />
ExifUserComment = 0x9286,<br />
ExifDTSubsec = 0x9290,<br />
ExifDTOrigSS = 0x9291,<br />
ExifDTDigSS = 0x9292,<br />
ExifFPXVer = 0xA000,<br />
ExifColorSpace = 0xA001,<br />
ExifPixXDim = 0xA002,<br />
ExifPixYDim = 0xA003,<br />
ExifRelatedWav = 0xA004,<br />
ExifInterop = 0xA005,<br />
ExifFlashEnergy = 0xA20B,<br />
ExifSpatialFR = 0xA20C,<br />
ExifFocalXRes = 0xA20E,<br />
ExifFocalYRes = 0xA20F,<br />
ExifFocalResUnit = 0xA210,<br />
ExifSubjectLoc = 0xA214,<br />
ExifExposureIndex = 0xA215,<br />
ExifSensingMethod = 0xA217,<br />
ExifFileSource = 0xA300,<br />
ExifSceneType = 0xA301,<br />
ExifCfaPattern = 0xA302,<br />
GpsVer = 0x0,<br />
GpsLatitudeRef = 0x1,<br />
GpsLatitude = 0x2,<br />
GpsLongitudeRef = 0x3,<br />
GpsLongitude = 0x4,<br />
GpsAltitudeRef = 0x5,<br />
GpsAltitude = 0x6,<br />
GpsGpsTime = 0x7,<br />
GpsGpsSatellites = 0x8,<br />
GpsGpsStatus = 0x9,<br />
GpsGpsMeasureMode = 0xA,<br />
GpsGpsDop = 0xB,<br />
GpsSpeedRef = 0xC,<br />
GpsSpeed = 0xD,<br />
GpsTrackRef = 0xE,<br />
GpsTrack = 0xF,<br />
GpsImgDirRef = 0x10,<br />
GpsImgDir = 0x11,<br />
GpsMapDatum = 0x12,<br />
GpsDestLatRef = 0x13,<br />
GpsDestLat = 0x14,<br />
GpsDestLongRef = 0x15,<br />
GpsDestLong = 0x16,<br />
GpsDestBearRef = 0x17,<br />
GpsDestBear = 0x18,<br />
GpsDestDistRef = 0x19,<br />
GpsDestDist = 0x1A<br />
}<br />
<br />
<br />
<br />
public enum Orientations<br />
{<br />
TopLeft = 1,<br />
TopRight = 2,<br />
BottomRight = 3,<br />
BottomLeft = 4,<br />
LeftTop = 5,<br />
RightTop = 6,<br />
RightBottom = 7,<br />
LftBottom = 8<br />
}<br />
<br />
<br />
<br />
public enum ExposurePrograms<br />
{<br />
Manual = 1,<br />
Normal = 2,<br />
AperturePriority = 3,<br />
ShutterPriority = 4,<br />
Creative = 5,<br />
Action = 6,<br />
Portrait = 7,<br />
Landscape = 8,<br />
}<br />
<br />
<br />
<br />
public enum ExposureMeteringModes<br />
{<br />
Unknown = 0,<br />
Average = 1,<br />
CenterWeightedAverage = 2,<br />
Spot = 3,<br />
MultiSpot = 4,<br />
MultiSegment = 5,<br />
Partial = 6,<br />
Other = 255<br />
}<br />
<br />
<br />
<br />
public enum FlashModes<br />
{<br />
NotFired = 0,<br />
Fired = 1,<br />
FiredButNoStrobeReturned = 5,<br />
FiredAndStrobeReturned = 7,<br />
}<br />
<br />
<br />
<br />
public enum LightSources<br />
{<br />
Unknown = 0,<br />
Daylight = 1,<br />
Fluorescent = 2,<br />
Tungsten = 3,<br />
Flash = 10,<br />
StandardLightA = 17,<br />
StandardLightB = 18,<br />
StandardLightC = 19,<br />
D55 = 20,<br />
D65 = 21,<br />
D75 = 22,<br />
Other = 255<br />
}<br />
<br />
<br />
public enum ExifDataTypes : short<br />
{<br />
UnsignedByte = 1,<br />
AsciiString = 2,<br />
UnsignedShort = 3,<br />
UnsignedLong = 4,<br />
UnsignedRational = 5,<br />
SignedByte = 6,<br />
Undefined = 7,<br />
SignedShort = 8,<br />
SignedLong = 9,<br />
SignedRational = 10,<br />
SingleFloat = 11,<br />
DoubleFloat = 12<br />
}<br />
<br />
<br />
public struct Rational<br />
{<br />
public Int32 Numerator;<br />
public Int32 Denominator;<br />
<br />
<br />
<br />
public override string ToString()<br />
{<br />
return ToString("/");<br />
}<br />
<br />
public string ToString(string Delimiter)<br />
{<br />
return Numerator + "/" + Denominator;<br />
}<br />
<br />
<br />
public double ToDouble()<br />
{<br />
return (double)Numerator / Denominator;<br />
}<br />
}<br />
<br />
#endregion<br />
<br />
public ExifManager(System.Drawing.Bitmap Bitmap)<br />
{<br />
if (Bitmap == null)<br />
throw new ArgumentNullException("Bitmap");<br />
this._Image = Bitmap;<br />
}<br />
<br />
public ExifManager(string FileName)<br />
{<br />
this._Image = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(FileName);<br />
}<br />
<br />
public System.Text.Encoding Encoding<br />
{<br />
get<br />
{<br />
return this._Encoding;<br />
}<br />
set<br />
{<br />
if (value == null)<br />
throw new ArgumentNullException();<br />
this._Encoding = value;<br />
}<br />
}<br />
<br />
public System.Drawing.Bitmap GetBitmap()<br />
{<br />
return (System.Drawing.Bitmap)this._Image.Clone();<br />
}<br />
<br />
public override string ToString()<br />
{<br />
System.Text.StringBuilder SB = new StringBuilder();<br />
<br />
SB.Append("Image:");<br />
SB.Append("\n\tDimensions: " + this.Width + " x " + this.Height + " px");<br />
SB.Append("\n\tResolution: " + this.ResolutionX + " x " + this.ResolutionY + " dpi");<br />
SB.Append("\n\tOrientation: " + Enum.GetName(typeof(Orientations), this.Orientation));<br />
SB.Append("\n\tTitle: " + this.Title);<br />
SB.Append("\n\tDescription: " + this.Description);<br />
SB.Append("\n\tCopyright: " + this.Copyright);<br />
SB.Append("\nEquipment:");<br />
SB.Append("\n\tMaker: " + this.EquipmentMaker);<br />
SB.Append("\n\tModel: " + this.EquipmentModel);<br />
SB.Append("\n\tSoftware: " + this.Software);<br />
SB.Append("\nDate and time:");<br />
SB.Append("\n\tGeneral: " + this.DateTimeLastModified.ToString());<br />
SB.Append("\n\tOriginal: " + this.DateTimeOriginal.ToString());<br />
SB.Append("\n\tDigitized: " + this.DateTimeDigitized.ToString());<br />
SB.Append("\nShooting conditions:");<br />
SB.Append("\n\tExposure time: " + this.ExposureTime.ToString("N4") + " s");<br />
SB.Append("\n\tExposure program: " + Enum.GetName(typeof(ExposurePrograms), this.ExposureProgram));<br />
SB.Append("\n\tExposure mode: " + Enum.GetName(typeof(ExposureMeteringModes), this.ExposureMeteringMode));<br />
SB.Append("\n\tAperture: F" + this.Aperture.ToString("N2"));<br />
SB.Append("\n\tISO sensitivity: " + this.ISO);<br />
SB.Append("\n\tSubject distance: " + this.SubjectDistance.ToString("N2") + " m");<br />
SB.Append("\n\tFocal length: " + this.FocalLength);<br />
SB.Append("\n\tFlash: " + Enum.GetName(typeof(FlashModes), this.FlashMode));<br />
SB.Append("\n\tLight source (WB): " + Enum.GetName(typeof(LightSources), this.LightSource));<br />
return SB.ToString();<br />
}<br />
<br />
#region Nicely formatted well-known properties<br />
<br />
public string EquipmentMaker<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.EquipMake);<br />
}<br />
}<br />
<br />
public string EquipmentModel<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.EquipModel);<br />
}<br />
}<br />
<br />
public string Software<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.SoftwareUsed);<br />
}<br />
}<br />
<br />
public Orientations Orientation<br />
{<br />
get<br />
{<br />
Int32 X = this.GetPropertyInt16((int)TagNames.Orientation);<br />
<br />
if (!Enum.IsDefined(typeof(Orientations), X))<br />
return Orientations.TopLeft;<br />
else<br />
return (Orientations)Enum.Parse(typeof(Orientations), Enum.GetName(typeof(Orientations), X));<br />
}<br />
}<br />
<br />
public DateTime DateTimeLastModified<br />
{<br />
get<br />
{<br />
try<br />
{<br />
return DateTime.ParseExact(this.GetPropertyString((int)TagNames.DateTime), @"yyyy\:MM\:dd HH\:mm\:ss", null);<br />
}<br />
catch<br />
{<br />
return DateTime.MinValue;<br />
}<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.DateTime, value.ToString(@"yyyy\:MM\:dd HH\:mm\:ss"));<br />
}<br />
catch<br />
{ }<br />
}<br />
}<br />
<br />
public DateTime DateTimeOriginal<br />
{<br />
get<br />
{<br />
try<br />
{<br />
return DateTime.ParseExact(this.GetPropertyString((int)TagNames.ExifDTOrig), @"yyyy\:MM\:dd HH\:mm\:ss", null);<br />
}<br />
catch<br />
{<br />
return DateTime.MinValue;<br />
}<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.ExifDTOrig, value.ToString(@"yyyy\:MM\:dd HH\:mm\:ss"));<br />
}<br />
catch<br />
{ }<br />
}<br />
}<br />
<br />
public DateTime DateTimeDigitized<br />
{<br />
get<br />
{<br />
try<br />
{<br />
return DateTime.ParseExact(this.GetPropertyString((int)TagNames.ExifDTDigitized), @"yyyy\:MM\:dd HH\:mm\:ss", null);<br />
}<br />
catch<br />
{<br />
return DateTime.MinValue;<br />
}<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.ExifDTDigitized, value.ToString(@"yyyy\:MM\:dd HH\:mm\:ss"));<br />
}<br />
catch<br />
{ }<br />
}<br />
}<br />
<br />
public Int32 Width<br />
{<br />
get { return this._Image.Width; }<br />
}<br />
<br />
public Int32 Height<br />
{<br />
get { return this._Image.Height; }<br />
}<br />
<br />
public double ResolutionX<br />
{<br />
get<br />
{<br />
double R = this.GetPropertyRational((int)TagNames.XResolution).ToDouble();<br />
<br />
if (this.GetPropertyInt16((int)TagNames.ResolutionUnit) == 3)<br />
{<br />
return R * 2.54;<br />
}<br />
else<br />
{<br />
return R;<br />
}<br />
}<br />
}<br />
<br />
public double ResolutionY<br />
{<br />
get<br />
{<br />
double R = this.GetPropertyRational((int)TagNames.YResolution).ToDouble();<br />
<br />
if (this.GetPropertyInt16((int)TagNames.ResolutionUnit) == 3)<br />
{<br />
return R * 2.54;<br />
}<br />
else<br />
{<br />
return R;<br />
}<br />
}<br />
}<br />
<br />
public string Title<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.ImageTitle);<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.ImageTitle, value);<br />
}<br />
catch { }<br />
}<br />
}<br />
<br />
public string UserComment<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.ExifUserComment);<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.ExifUserComment, value);<br />
}<br />
catch { }<br />
}<br />
}<br />
<br />
public string Artist<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.Artist);<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.Artist, value);<br />
}<br />
catch { }<br />
}<br />
}<br />
<br />
public string Description<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.ImageDescription);<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.ImageDescription, value);<br />
}<br />
catch { }<br />
}<br />
}<br />
<br />
public string Copyright<br />
{<br />
get<br />
{<br />
return this.GetPropertyString((int)TagNames.Copyright);<br />
}<br />
set<br />
{<br />
try<br />
{<br />
this.SetPropertyString((int)TagNames.Copyright, value);<br />
}<br />
catch { }<br />
}<br />
}<br />
<br />
<br />
public double ExposureTimeAbs<br />
{<br />
get<br />
{<br />
if (this.IsPropertyDefined((int)TagNames.ExifExposureTime))<br />
return this.GetPropertyRational((int)TagNames.ExifExposureTime).ToDouble();<br />
else<br />
if (this.IsPropertyDefined((int)TagNames.ExifShutterSpeed))<br />
return (1 / Math.Pow(2, this.GetPropertyRational((int)TagNames.ExifShutterSpeed).ToDouble()));<br />
else<br />
return 0;<br />
}<br />
}<br />
<br />
public Rational ExposureTime<br />
{<br />
get<br />
{<br />
if (this.IsPropertyDefined((int)TagNames.ExifExposureTime))<br />
return this.GetPropertyRational((int)TagNames.ExifExposureTime);<br />
else<br />
return new Rational();<br />
}<br />
}<br />
<br />
public double Aperture<br />
{<br />
get<br />
{<br />
if (this.IsPropertyDefined((int)TagNames.ExifFNumber))<br />
return this.GetPropertyRational((int)TagNames.ExifFNumber).ToDouble();<br />
else<br />
if (this.IsPropertyDefined((int)TagNames.ExifAperture))<br />
return Math.Pow(System.Math.Sqrt(2), this.GetPropertyRational((int)TagNames.ExifAperture).ToDouble());<br />
else<br />
return 0;<br />
}<br />
}<br />
<br />
public ExposurePrograms ExposureProgram<br />
{<br />
get<br />
{<br />
Int32 X = this.GetPropertyInt16((int)TagNames.ExifExposureProg);<br />
<br />
if (Enum.IsDefined(typeof(ExposurePrograms), X))<br />
return (ExposurePrograms)Enum.Parse(typeof(ExposurePrograms), Enum.GetName(typeof(ExposurePrograms), X));<br />
else<br />
return ExposurePrograms.Normal;<br />
}<br />
}<br />
<br />
public Int16 ISO<br />
{<br />
get { return this.GetPropertyInt16((int)TagNames.ExifISOSpeed); }<br />
}<br />
<br />
public double SubjectDistance<br />
{<br />
get { return this.GetPropertyRational((int)TagNames.ExifSubjectDist).ToDouble(); }<br />
}<br />
<br />
public ExposureMeteringModes ExposureMeteringMode<br />
{<br />
get<br />
{<br />
Int32 X = this.GetPropertyInt16((int)TagNames.ExifMeteringMode);<br />
<br />
if (Enum.IsDefined(typeof(ExposureMeteringModes), X))<br />
return (ExposureMeteringModes)Enum.Parse(typeof(ExposureMeteringModes), Enum.GetName(typeof(ExposureMeteringModes), X));<br />
else<br />
return ExposureMeteringModes.Unknown;<br />
}<br />
}<br />
<br />
public double FocalLength<br />
{<br />
get { return this.GetPropertyRational((int)TagNames.ExifFocalLength).ToDouble(); }<br />
}<br />
<br />
public FlashModes FlashMode<br />
{<br />
get<br />
{<br />
Int32 X = this.GetPropertyInt16((int)TagNames.ExifFlash);<br />
<br />
if (Enum.IsDefined(typeof(FlashModes), X))<br />
return (FlashModes)Enum.Parse(typeof(FlashModes), Enum.GetName(typeof(FlashModes), X));<br />
else<br />
return FlashModes.NotFired;<br />
}<br />
}<br />
<br />
public LightSources LightSource<br />
{<br />
get<br />
{<br />
Int32 X = this.GetPropertyInt16((int)TagNames.ExifLightSource);<br />
<br />
if (Enum.IsDefined(typeof(LightSources), X))<br />
return (LightSources)Enum.Parse(typeof(LightSources), Enum.GetName(typeof(LightSources), X));<br />
else<br />
return LightSources.Unknown;<br />
}<br />
}<br />
<br />
#endregion<br />
<br />
#region Support methods for working with EXIF properties<br />
<br />
public bool IsPropertyDefined(Int32 PID)<br />
{<br />
return (Array.IndexOf(this._Image.PropertyIdList, PID) > -1);<br />
}<br />
<br />
public Int32 GetPropertyInt32(Int32 PID)<br />
{<br />
return GetPropertyInt32(PID, 0);<br />
}<br />
<br />
public Int32 GetPropertyInt32(Int32 PID, Int32 DefaultValue)<br />
{<br />
if (IsPropertyDefined(PID))<br />
return GetInt32(this._Image.GetPropertyItem(PID).Value);<br />
else<br />
return DefaultValue;<br />
}<br />
<br />
public Int16 GetPropertyInt16(Int32 PID)<br />
{<br />
return GetPropertyInt16(PID, 0);<br />
}<br />
<br />
public Int16 GetPropertyInt16(Int32 PID, Int16 DefaultValue)<br />
{<br />
if (IsPropertyDefined(PID))<br />
return GetInt16(this._Image.GetPropertyItem(PID).Value);<br />
else<br />
return DefaultValue;<br />
}<br />
<br />
public string GetPropertyString(Int32 PID)<br />
{<br />
return GetPropertyString(PID, "");<br />
}<br />
<br />
public string GetPropertyString(Int32 PID, string DefaultValue)<br />
{<br />
if (IsPropertyDefined(PID))<br />
return GetString(this._Image.GetPropertyItem(PID).Value);<br />
else<br />
return DefaultValue;<br />
}<br />
<br />
public byte[] GetProperty(Int32 PID, byte[] DefaultValue)<br />
{<br />
if (IsPropertyDefined(PID))<br />
return this._Image.GetPropertyItem(PID).Value;<br />
else<br />
return DefaultValue;<br />
}<br />
<br />
public byte[] GetProperty(Int32 PID)<br />
{<br />
return GetProperty(PID, null);<br />
}<br />
<br />
public Rational GetPropertyRational(Int32 PID)<br />
{<br />
if (IsPropertyDefined(PID))<br />
return GetRational(this._Image.GetPropertyItem(PID).Value);<br />
else<br />
{<br />
Rational R;<br />
R.Numerator = 0;<br />
R.Denominator = 1;<br />
return R;<br />
}<br />
}<br />
<br />
public void SetPropertyString(Int32 PID, string Value)<br />
{<br />
byte[] Data = this._Encoding.GetBytes(Value + '\0');<br />
SetProperty(PID, Data, ExifDataTypes.AsciiString);<br />
}<br />
<br />
public void SetPropertyInt16(Int32 PID, Int16 Value)<br />
{<br />
byte[] Data = new byte[2];<br />
Data[0] = (byte)(Value & 0xFF);<br />
Data[1] = (byte)((Value & 0xFF00) >> 8);<br />
SetProperty(PID, Data, ExifDataTypes.SignedShort);<br />
}<br />
<br />
public void SetPropertyInt32(Int32 PID, Int32 Value)<br />
{<br />
byte[] Data = new byte[4];<br />
for (int I = 0; I < 4; I++)<br />
{<br />
Data[I] = (byte)(Value & 0xFF);<br />
Value >>= 8;<br />
}<br />
SetProperty(PID, Data, ExifDataTypes.SignedLong);<br />
}<br />
<br />
public void SetProperty(Int32 PID, byte[] Data, ExifDataTypes Type)<br />
{<br />
System.Drawing.Imaging.PropertyItem P = this._Image.PropertyItems[0];<br />
P.Id = PID;<br />
P.Value = Data;<br />
P.Type = (Int16)Type;<br />
P.Len = Data.Length;<br />
this._Image.SetPropertyItem(P);<br />
}<br />
<br />
private Int32 GetInt32(byte[] B)<br />
{<br />
if (B.Length < 4)<br />
throw new ArgumentException("Data too short (4 bytes expected)", "B");<br />
<br />
return B[3] << 24 | B[2] << 16 | B[1] << 8 | B[0];<br />
}<br />
<br />
private Int16 GetInt16(byte[] B)<br />
{<br />
if (B.Length < 2)<br />
throw new ArgumentException("Data too short (2 bytes expected)", "B");<br />
<br />
return (short)(B[1] << 8 | B[0]);<br />
}<br />
<br />
private string GetString(byte[] B)<br />
{<br />
string R = this._Encoding.GetString(B);<br />
if (R.EndsWith("\0"))<br />
R = R.Substring(0, R.Length - 1);<br />
return R;<br />
}<br />
<br />
private Rational GetRational(byte[] B)<br />
{<br />
Rational R = new Rational();<br />
byte[] N = new byte[4];<br />
byte[] D = new byte[4];<br />
Array.Copy(B, 0, N, 0, 4);<br />
Array.Copy(B, 4, D, 0, 4);<br />
R.Denominator = this.GetInt32(D);<br />
R.Numerator = this.GetInt32(N);<br />
return R;<br />
}<br />
<br />
#endregion<br />
<br />
#region " IDisposable implementation "<br />
<br />
public void Dispose()<br />
{<br />
this._Image.Dispose();<br />
}<br />
<br />
#endregion<br />
<br />
}<br />
<br />
}<br />
<br />
|