|
I did it
the EXIF standard book specifies the data source are byte[],so propertyitem.value return byte[]. To assign such type of value must do some convertion rather than simply use "=".
these codes works:
System.Drawing.Imaging.PropertyItem pi = System.Drawing.Image.FromFile(@"C:\Documents and Settings\Wang Zhifang\桌面\my gps\exif_source_demo\Demo\EXIFextractor.decoy.jpg").PropertyItems[0];
pi.Type = 5;
pi.Len = 24;
pi.Id = 0x2;
pi.Value = new byte[24];
//construct .value
byte[] n = new byte[4];//numerator
byte[] d = new byte[4];//denominator
n = BitConverter.GetBytes(34);
d = BitConverter.GetBytes(1);
Array.Copy(n, 0, pi.Value, 0, 4);
Array.Copy(d, 0, pi.Value, 4, 4);
n = BitConverter.GetBytes(47);
Array.Copy(n, 0, pi.Value, 8, 4);
Array.Copy(d, 0, pi.Value, 12, 4);
n = BitConverter.GetBytes(707);
d = BitConverter.GetBytes(20);
Array.Copy(n, 0, pi.Value, 16, 4);
Array.Copy(d, 0, pi.Value, 20, 4);
bmp.SetPropertyItem(pi);//纬度
//经度
//create propertyitem
pi.Type = 5;
pi.Len = 24;
pi.Id = 0x4;
pi.Value = new byte[24];
//construct .value
n = BitConverter.GetBytes(108);
d = BitConverter.GetBytes(1);
Array.Copy(n, 0, pi.Value, 0, 4);
Array.Copy(d, 0, pi.Value, 4, 4);
n = BitConverter.GetBytes(25);
Array.Copy(n, 0, pi.Value, 8, 4);
Array.Copy(d, 0, pi.Value, 12, 4);
n = BitConverter.GetBytes(707);
d = BitConverter.GetBytes(20);
Array.Copy(n, 0, pi.Value, 16, 4);
Array.Copy(d, 0, pi.Value, 20, 4);
bmp.SetPropertyItem(pi);//纬度
//设置经纬度参考
System.Drawing.Imaging.PropertyItem ppii = System.Drawing.Image.FromFile(@"C:\Documents and Settings\Wang Zhifang\桌面\my gps\exif_source_demo\Demo\EXIFextractor.decoy.jpg").PropertyItems[0];
ppii.Id = 0x1;//纬度参考
ppii.Len = 2;//以null结尾
ppii.Type = 2;
ppii.Value=new byte[2];
byte[] cref=new byte[2];
cref = System.Text.Encoding.ASCII.GetBytes("N\0");
Array.Copy(cref, 0, ppii.Value, 0, 2);
bmp.SetPropertyItem(ppii);
//设置经经度参考
ppii.Id = 0x3;//经度参考
ppii.Len = 2;//以null结尾
ppii.Type = 2;
cref = System.Text.Encoding.ASCII.GetBytes("E\0");//\0是经过转义的字符,会自动变成字符串的结尾,默认的string没有这个结尾
Array.Copy(cref, 0, ppii.Value, 0, 2);
bmp.SetPropertyItem(ppii);
the codes set the value of 4 items:GPS Lat.,GPS Long., GPS Lat.Ref, GPS Long.Ref,the specific value bases on the propertyitem.type,could be refer to MSDN or EXIF standard.
Finally,thanks for your code, and wish you all the best in your work
|
|
|
|
|
Hi,
You're welcome. And thanks for your research, I haven't had time to look into it, but I'll have it in mind.
Lev
|
|
|
|
|
A very, very useful bit of code. Thanks.
Here's an oddity you might be interested in. The first image I tried the code on was a Jpeg created in Photoshop.
It crashed in ExifTagCollection.ReadTags() trying to add a duplicate tag to the tag collection. Evidently my image has some duplicated Exif data.
I fixed it simply by changing:
_tags.Add(tag.Id, tag);
to
if (_tags.ContainsKey(tag.Id))
{
_tags[tag.Id] = tag;
}
else
{
_tags.Add(tag.Id, tag);
}
It may not be reasonable for duplicate tags to exist in the Exif, but this was created in Photoshop CS3, so it's likely to happen again.
Cheers,
Steve
|
|
|
|
|
Hi, Steve,
I do not have a Photoshop CS3, could you please send me a sample file?
Lev
PS. I don't like the ContainsKey(), I'll try to find something more efficient
|
|
|
|
|
Reading tags is very inefficient.
Why you try to load all supported tags from any image, and then eating exceptions? This is not only bad design but slow too.
Why not enumerate not all possible tags, but only existent tags? Check Image.PropertyItems property.
Liberavi animam meam!
|
|
|
|
|
Thanks, Eugene,
The problem is that PropertyItems contains a lot of stuff that has nothing to do with EXIF. So traversing through it will bring up the same problems.
I'm thinking of a better way to solve this problem. One possible way would be having a separate list of supported tags as a Hashtable (as it doesn't raise an exception when a non-existent key is being read) and checking for a null afterwards.
I think I will update the code soon to avoid that exceptions soon...
|
|
|
|
|
And yes, this is what I did. I got rid from the numerous exceptions, separated the list of supported tags, and now I'm traversing the PropertyItems array, see if the PropertyItem is in list of the supported tags and process it, if it is.
|
|
|
|
|
make it able to edit exif?
Regards,
unruledboy (at) gmail (dot) com
|
|
|
|
|
I'm planning to do that. At this point I'm trying to come up with some generic solution for type/content checks.
Lev
|
|
|
|
|
I like what you've done. I never knew image files contained such information. Thanks for sharing it.
|
|
|
|