数码相机的照片很多,而且文件名也不直观,所以写了个程序批量改名,就是把文件名都改成相机的拍照日期,呵呵,谁知道拍照日期不是那么好取,在csdn里开了一帖,谢谢网友帮忙,终于搞定!把代码放在下面了,说不定谁会用得着。
原帖地址: http://community.csdn.net/Expert/topic/4295/4295730.xml?temp=.662182
原文地址: http://www.codeproject.com/csharp/EXIFextractor.asp
////调用
string strFile="fffff.jpg";//文件名
System.Drawing.Bitmap bmp = new Bitmap(strFile);
EXIF.EXIFextractor er = new EXIF.EXIFextractor(ref bmp, "\n");
Console.Write(strFile + "\r\n");
Console.Write(er["DTDigitized"]);//拍照日期
foreach (EXIF.Pair pr in er)
{
Console.Write(pr.First + ":" + pr.Second + "\r\n");
}
//////////////////////////////////////////////////////////////////////////////////////
///下面的两个文件处理JPG的EXIF信息
///EXIFextractor.cs
///
using System;
using System.Text;
using System.Collections;
using System.Drawing.Imaging;
using System.Reflection;
using System.IO;
namespace EXIF
{
///
1<summary>
2/// EXIFextractor Class
3///
4/// </summary>
public class EXIFextractor : IEnumerable
{
///
1<summary>
2/// Get the individual property value by supplying property name
3/// These are the valid property names :
4///
5/// "Exif IFD"
6/// "Gps IFD"
7/// "New Subfile Type"
8/// "Subfile Type"
9/// "Image Width"
10/// "Image Height"
11/// "Bits Per Sample"
12/// "Compression"
13/// "Photometric Interp"
14/// "Thresh Holding"
15/// "Cell Width"
16/// "Cell Height"
17/// "Fill Order"
18/// "Document Name"
19/// "Image Description"
20/// "Equip Make"
21/// "Equip Model"
22/// "Strip Offsets"
23/// "Orientation"
24/// "Samples PerPixel"
25/// "Rows Per Strip"
26/// "Strip Bytes Count"
27/// "Min Sample Value"
28/// "Max Sample Value"
29/// "X Resolution"
30/// "Y Resolution"
31/// "Planar Config"
32/// "Page Name"
33/// "X Position"
34/// "Y Position"
35/// "Free Offset"
36/// "Free Byte Counts"
37/// "Gray Response Unit"
38/// "Gray Response Curve"
39/// "T4 Option"
40/// "T6 Option"
41/// "Resolution Unit"
42/// "Page Number"
43/// "Transfer Funcition"
44/// "Software Used"
45/// "Date Time"
46/// "Artist"
47/// "Host Computer"
48/// "Predictor"
49/// "White Point"
50/// "Primary Chromaticities"
51/// "ColorMap"
52/// "Halftone Hints"
53/// "Tile Width"
54/// "Tile Length"
55/// "Tile Offset"
56/// "Tile ByteCounts"
57/// "InkSet"
58/// "Ink Names"
59/// "Number Of Inks"
60/// "Dot Range"
61/// "Target Printer"
62/// "Extra Samples"
63/// "Sample Format"
64/// "S Min Sample Value"
65/// "S Max Sample Value"
66/// "Transfer Range"
67/// "JPEG Proc"
68/// "JPEG InterFormat"
69/// "JPEG InterLength"
70/// "JPEG RestartInterval"
71/// "JPEG LosslessPredictors"
72/// "JPEG PointTransforms"
73/// "JPEG QTables"
74/// "JPEG DCTables"
75/// "JPEG ACTables"
76/// "YCbCr Coefficients"
77/// "YCbCr Subsampling"
78/// "YCbCr Positioning"
79/// "REF Black White"
80/// "ICC Profile"
81/// "Gamma"
82/// "ICC Profile Descriptor"
83/// "SRGB RenderingIntent"
84/// "Image Title"
85/// "Copyright"
86/// "Resolution X Unit"
87/// "Resolution Y Unit"
88/// "Resolution X LengthUnit"
89/// "Resolution Y LengthUnit"
90/// "Print Flags"
91/// "Print Flags Version"
92/// "Print Flags Crop"
93/// "Print Flags Bleed Width"
94/// "Print Flags Bleed Width Scale"
95/// "Halftone LPI"
96/// "Halftone LPIUnit"
97/// "Halftone Degree"
98/// "Halftone Shape"
99/// "Halftone Misc"
100/// "Halftone Screen"
101/// "JPEG Quality"
102/// "Grid Size"
103/// "Thumbnail Format"
104/// "Thumbnail Width"
105/// "Thumbnail Height"
106/// "Thumbnail ColorDepth"
107/// "Thumbnail Planes"
108/// "Thumbnail RawBytes"
109/// "Thumbnail Size"
110/// "Thumbnail CompressedSize"
111/// "Color Transfer Function"
112/// "Thumbnail Data"
113/// "Thumbnail ImageWidth"
114/// "Thumbnail ImageHeight"
115/// "Thumbnail BitsPerSample"
116/// "Thumbnail Compression"
117/// "Thumbnail PhotometricInterp"
118/// "Thumbnail ImageDescription"
119/// "Thumbnail EquipMake"
120/// "Thumbnail EquipModel"
121/// "Thumbnail StripOffsets"
122/// "Thumbnail Orientation"
123/// "Thumbnail SamplesPerPixel"
124/// "Thumbnail RowsPerStrip"
125/// "Thumbnail StripBytesCount"
126/// "Thumbnail ResolutionX"
127/// "Thumbnail ResolutionY"
128/// "Thumbnail PlanarConfig"
129/// "Thumbnail ResolutionUnit"
130/// "Thumbnail TransferFunction"
131/// "Thumbnail SoftwareUsed"
132/// "Thumbnail DateTime"
133/// "Thumbnail Artist"
134/// "Thumbnail WhitePoint"
135/// "Thumbnail PrimaryChromaticities"
136/// "Thumbnail YCbCrCoefficients"
137/// "Thumbnail YCbCrSubsampling"
138/// "Thumbnail YCbCrPositioning"
139/// "Thumbnail RefBlackWhite"
140/// "Thumbnail CopyRight"
141/// "Luminance Table"
142/// "Chrominance Table"
143/// "Frame Delay"
144/// "Loop Count"
145/// "Pixel Unit"
146/// "Pixel PerUnit X"
147/// "Pixel PerUnit Y"
148/// "Palette Histogram"
149/// "Exposure Time"
150/// "F-Number"
151/// "Exposure Prog"
152/// "Spectral Sense"
153/// "ISO Speed"
154/// "OECF"
155/// "Ver"
156/// "DTOrig"
157/// "DTDigitized"
158/// "CompConfig"
159/// "CompBPP"
160/// "Shutter Speed"
161/// "Aperture"
162/// "Brightness"
163/// "Exposure Bias"
164/// "MaxAperture"
165/// "SubjectDist"
166/// "Metering Mode"
167/// "LightSource"
168/// "Flash"
169/// "FocalLength"
170/// "Maker Note"
171/// "User Comment"
172/// "DTSubsec"
173/// "DTOrigSS"
174/// "DTDigSS"
175/// "FPXVer"
176/// "ColorSpace"
177/// "PixXDim"
178/// "PixYDim"
179/// "RelatedWav"
180/// "Interop"
181/// "FlashEnergy"
182/// "SpatialFR"
183/// "FocalXRes"
184/// "FocalYRes"
185/// "FocalResUnit"
186/// "Subject Loc"
187/// "Exposure Index"
188/// "Sensing Method"
189/// "FileSource"
190/// "SceneType"
191/// "CfaPattern"
192/// "Gps Ver"
193/// "Gps LatitudeRef"
194/// "Gps Latitude"
195/// "Gps LongitudeRef"
196/// "Gps Longitude"
197/// "Gps AltitudeRef"
198/// "Gps Altitude"
199/// "Gps GpsTime"
200/// "Gps GpsSatellites"
201/// "Gps GpsStatus"
202/// "Gps GpsMeasureMode"
203/// "Gps GpsDop"
204/// "Gps SpeedRef"
205/// "Gps Speed"
206/// "Gps TrackRef"
207/// "Gps Track"
208/// "Gps ImgDirRef"
209/// "Gps ImgDir"
210/// "Gps MapDatum"
211/// "Gps DestLatRef"
212/// "Gps DestLat"
213/// "Gps DestLongRef"
214/// "Gps DestLong"
215/// "Gps DestBearRef"
216/// "Gps DestBear"
217/// "Gps DestDistRef"
218/// "Gps DestDist"
219/// </summary>
public object this[string index]
{
get
{
return properties[index];
}
}
//
private System.Drawing.Bitmap bmp;
//
private string data;
//
private translation myHash;
//
private Hashtable properties;
//
internal int Count
{
get
{
return this.properties.Count;
}
}
//
string sp;
///
1<summary>
2///
3/// </summary>
///
1<param name="id"/>
///
1<param name="len"/>
///
1<param name="type"/>
///
1<param name="data"/>
public void setTag(int id, string data)
{
Encoding ascii = Encoding.ASCII;
this.setTag(id, data.Length, 0x2, ascii.GetBytes(data));
}
///
1<summary>
2///
3/// </summary>
///
1<param name="id"/>
///
1<param name="len"/>
///
1<param name="type"/>
///
1<param name="data"/>
public void setTag(int id, int len, short type, byte[] data)
{
PropertyItem p = CreatePropertyItem(type, id, len, data);
this.bmp.SetPropertyItem(p);
buildDB(this.bmp.PropertyItems);
}
///
1<summary>
2///
3/// </summary>
///
1<param name="type"/>
///
1<param name="tag"/>
///
1<param name="len"/>
///
1<param name="value"/>
///
1<returns></returns>
private static PropertyItem CreatePropertyItem(short type, int tag, int len, byte[] value)
{
PropertyItem item;
// Loads a PropertyItem from a Jpeg image stored in the assembly as a resource.
Assembly assembly = Assembly.GetExecutingAssembly();
Stream emptyBitmapStream = assembly.GetManifestResourceStream("EXIFextractor.decoy.jpg");
System.Drawing.Image empty = System.Drawing.Image.FromStream(emptyBitmapStream);
item = empty.PropertyItems[0];
// Copies the data to the property item.
item.Type = type;
item.Len = len;
item.Id = tag;
item.Value = new byte[value.Length];
value.CopyTo(item.Value, 0);
return item;
}
///
1<summary>
2///
3/// </summary>
///
1<param name="bmp"/>
///
1<param name="sp"/>
public EXIFextractor(ref System.Drawing.Bitmap bmp, string sp)
{
properties = new Hashtable();
//
this.bmp = bmp;
this.sp = sp;
//
myHash = new translation();
buildDB(this.bmp.PropertyItems);
}
string msp = "";
public EXIFextractor(ref System.Drawing.Bitmap bmp, string sp, string msp)
{
properties = new Hashtable();
this.sp = sp;
this.msp = msp;
this.bmp = bmp;
//
myHash = new translation();
this.buildDB(bmp.PropertyItems);
}
public static PropertyItem[] GetExifProperties(string fileName)
{
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream,
/* useEmbeddedColorManagement = / true,
/ validateImageData = */ false);
return image.PropertyItems;
}
public EXIFextractor(string file, string sp, string msp)
{
properties = new Hashtable();
this.sp = sp;
this.msp = msp;
myHash = new translation();
//
this.buildDB(GetExifProperties(file));
}
///
1<summary>
2///
3/// </summary>
private void buildDB(System.Drawing.Imaging.PropertyItem[] parr)
{
properties.Clear();
//
data = "";
//
Encoding ascii = Encoding.ASCII;
//
foreach (System.Drawing.Imaging.PropertyItem p in parr)
{
string v = "";
string name = (string)myHash[p.Id];
// tag not found. skip it
if (name == null) continue;
//
data += name + ": ";
//
//1 = BYTE An 8-bit unsigned integer.,
if (p.Type == 0x1)
{
v = p.Value[0].ToString();
}
//2 = ASCII An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.,
else if (p.Type == 0x2)
{
// string
v = ascii.GetString(p.Value);
}
//3 = SHORT A 16-bit (2 -byte) unsigned integer,
else if (p.Type == 0x3)
{
// orientation // lookup table
switch (p.Id)
{
case 0x8827: // ISO
v = "ISO-" + convertToInt16U(p.Value).ToString();
break;
case 0xA217: // sensing method
{
switch (convertToInt16U(p.Value))
{
case 1: v = "Not defined"; break;
case 2: v = "One-chip color area sensor"; break;
case 3: v = "Two-chip color area sensor"; break;
case 4: v = "Three-chip color area sensor"; break;
case 5: v = "Color sequential area sensor"; break;
case 7: v = "Trilinear sensor"; break;
case 8: v = "Color sequential linear sensor"; break;
default: v = " reserved"; break;
}
}
break;
case 0x8822: // aperture
switch (convertToInt16U(p.Value))
{
case 0: v = "Not defined"; break;
case 1: v = "Manual"; break;
case 2: v = "Normal program"; break;
case 3: v = "Aperture priority"; break;
case 4: v = "Shutter priority"; break;
case 5: v = "Creative program (biased toward depth of field)"; break;
case 6: v = "Action program (biased toward fast shutter speed)"; break;
case 7: v = "Portrait mode (for closeup photos with the background out of focus)"; break;
case 8: v = "Landscape mode (for landscape photos with the background in focus)"; break;
default: v = "reserved"; break;
}
break;
case 0x9207: // metering mode
switch (convertToInt16U(p.Value))
{
case 0: v = "unknown"; break;
case 1: v = "Average"; break;
case 2: v = "CenterWeightedAverage"; break;
case 3: v = "Spot"; break;
case 4: v = "MultiSpot"; break;
case 5: v = "Pattern"; break;
case 6: v = "Partial"; break;
case 255: v = "Other"; break;
default: v = "reserved"; break;
}
break;
case 0x9208: // light source
{
switch (convertToInt16U(p.Value))
{
case 0: v = "unknown"; break;
case 1: v = "Daylight"; break;
case 2: v = "Fluorescent"; break;
case 3: v = "Tungsten"; break;
case 17: v = "Standard light A"; break;
case 18: v = "Standard light B"; break;
case 19: v = "Standard light C"; break;
case 20: v = "D55"; break;
case 21: v = "D65"; break;
case 22: v = "D75"; break;
case 255: v = "other"; break;
default: v = "reserved"; break;
}
}
break;
case 0x9209:
{
switch (convertToInt16U(p.Value))
{
case 0: v = "Flash did not fire"; break;
case 1: v = "Flash fired"; break;
case 5: v = "Strobe return light not detected"; break;
case 7: v = "Strobe return light detected"; break;
default: v = "reserved"; break;
}
}
break;
default:
v = convertToInt16U(p.Value).ToString();
break;
}
}
//4 = LONG A 32-bit (4 -byte) unsigned integer,
else if (p.Type == 0x4)
{
// orientation // lookup table
v = convertToInt32U(p.Value).ToString();
}
//5 = RATIONAL Two LONGs. The first LONG is the numerator and the second LONG expresses the//denominator.,
else if (p.Type == 0x5)
{
// rational
byte[] n = new byte[p.Len / 2];
byte[] d = new byte[p.Len / 2];
Array.Copy(p.Value, 0, n, 0, p.Len / 2);
Array.Copy(p.Value, p.Len / 2, d, 0, p.Len / 2);
uint a = convertToInt32U(n);
uint b = convertToInt32U(d);