/
EXIFData.cs
161 lines (137 loc) · 5.48 KB
/
EXIFData.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace ImageCaptionEdit
{
class EXIFData
{
enum ExifDataTypes
{
UnsignedByte = 1,
AsciiString = 2,
UnsignedShort = 3,
UnsignedLong = 4,
UnsignedRational = 5,
SignedByte = 6,
Undefined = 7,
SignedShort = 8,
SignedLong = 9,
SignedRational = 10,
SingleFloat = 11,
DoubleFloat = 12,
}
public static string header; //stored under id=0x9c9b (title)
public static string sideinfo; //stored under id=0x9286 (description)
public static string description;//stored under id=0x9c9c (comment)
public static void GetData(string filename)
{
//try
//{
//Open the file to look at its properties..
Image theImage = new Bitmap(filename);
PropertyItem[] propItems = theImage.PropertyItems;
// For each PropertyItem in the array, display the id,
// type, and length.
Encoding _Encoding;
//clear these out initially
header = "";
sideinfo = "";
description = "";
//default encoding
_Encoding = Encoding.UTF8;
foreach (PropertyItem propItem in propItems)
{
if (propItem.Value != null)
{
_Encoding = setEncoding(propItem.Value);
}
switch (propItem.Id)
{
case 0x9c9b: //Title (40091)
if (propItem.Value == null) break;
header = _Encoding.GetString(propItem.Value);
break;
//case 0x9286: //Description (37510)
// if (propItem.Value == null) break;
// sideinfo = _Encoding.GetString(propItem.Value);
// break;
case 0x9c9f: //Subject (40095)
if (propItem.Value == null) break;
sideinfo = _Encoding.GetString(propItem.Value);
break;
case 0x9c9c: //Comment (40092)
if (propItem.Value == null) break;
description = _Encoding.GetString(propItem.Value);
break;
default:
break;
}
}
//}
//catch (Exception ex)
//{
// MessageBox.Show("Error in GetData: " + ex.Message);
//}
}
private static Encoding setEncoding(byte[] str)
{
Encoding enc;
if (str == null)
{
return null;
}
if (str[1] == 0) enc = Encoding.Unicode;
else enc = Encoding.UTF8;
return enc;
}
public static void SaveData(string infilename, string outfilename)
{
//try
//{
string TempFile;
TempFile = System.IO.Path.GetTempPath() + "__ImageCaptionEdit__" + System.IO.Path.GetRandomFileName();
System.IO.File.Copy(infilename, TempFile);
Encoding _Encoding = Encoding.UTF8;
Image theImage = new Bitmap(TempFile);
//figure out how to create an EXIF propertyitem...
//PropertyItem BaseProperty = new PropertyItem();
PropertyItem BaseProperty;
BaseProperty = CreatePropertyItem();
theImage.SetPropertyItem(SetupProperty(BaseProperty, header, 0x9c9b));
theImage.SetPropertyItem(SetupProperty(BaseProperty, sideinfo, 0x9c9f));
theImage.SetPropertyItem(SetupProperty(BaseProperty, description, 0x9c9c));
theImage.Save(outfilename,ImageFormat.Jpeg);
//}
//catch (Exception excp)
//{
// MessageBox.Show("Error in SaveData: " + excp.Message);
//}
}
//Since there is no way to instantiate a new PropertyItem, we simply include a .jpg file in the exe and get
//a base property item from that.
static private PropertyItem CreatePropertyItem()
{
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("ImageCaptionEdit.meta.jpg");
Image theImage = new Bitmap(file);
PropertyItem BaseProperty;
BaseProperty = theImage.PropertyItems[0];
BaseProperty = SetupProperty(BaseProperty, "", 270);
return BaseProperty;
}
static private PropertyItem SetupProperty(PropertyItem BaseProperty, string Text, int Id)
{
Encoding _Encoding = Encoding.UTF8;
BaseProperty.Value = _Encoding.GetBytes(Text + '\0');
BaseProperty.Id = Id;
BaseProperty.Type = (short)ExifDataTypes.AsciiString;
BaseProperty.Len = (int)Text.Length;
return BaseProperty;
}
}
}