Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exif and IPTC Reading and Writing

Status
Not open for further replies.

murratore

Programmer
Oct 24, 2002
10
CH
Hi there

Does somebody know a class library which offers basic image manipulation and also gets and sets exif and iptc metadata? If possible for free...I found some components, but these were quite too expensive...(max 40$).

thx for any recommendation!
 
the image class has the possibility to read an write these attributes. Look at the propertyitems property.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
This is my class to get EXIF info from regular .jpg picture

using System;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CreateHTML {

/* This classs was rewrited from EXIF.py script from Gene Cash
Contains code from "exifdump.py" originally written by Thierry Bousch
<bousch@topo.math.u-psud.fr> and released into the public domain.

Updated and turned into general-purpose library by Gene Cash
<email gcash at cfl.rr.com>
Copyright 2002 All rights reserved

*/

public class GetExifInfo {

public const int EXIF_ORIGDATE = 36867;
public const int EXIF_USERCMNT = 37510;

private string mFile;
private PropertyItem[] mPropItems;

public string FileName {
set {
if (value != "" && value != mFile) {
mFile = value;
GetExifProperties();
}
}
}

public GetExifInfo(string aFile) {
mFile = aFile;
GetExifProperties();
}

/* public override string ToString() {
string line = "";
foreach (PropertyItem prop in mPropItems) {
line = line + prop.Id + " - " + prop.Type + " - " + ByteToString(prop.Value) + "\n";
}
return line;
} */



private string UniToCode(params byte[] aBytes) {
StringBuilder sb = new StringBuilder();

foreach (byte byt in aBytes) {
switch ((int) byt) {
case 154: {
sb.Append("š");
break;
}
case 240: {
sb.Append("ð");
break;
}
case 232: {
sb.Append("è");
break;
}
case 230: {
sb.Append("æ");
break;
}
case 158: {
sb.Append("ž");
break;
}
case 138: {
sb.Append("Š");
break;
}
case 208: {
sb.Append("Ð");
break;
}
case 200: {
sb.Append("È");
break;
}
case 198: {
sb.Append("Æ");
break;
}
case 142: {
sb.Append("Ž");
break;
}
default: {
sb.Append((char) byt);
break;
}
}
}
return sb.ToString();
}

public GetExifInfo() {
mFile = "";
}

public string Info(int aInfoType, Encoding aEncod) {
return aEncod.GetString(GetInfo(aInfoType));
}

public string Info(int aInfoType) {
RunMain.gLog.WriteLog("EXIF Info for file " + mFile + "\n" + HexInfo(aInfoType,10));
if (aInfoType == EXIF_USERCMNT) {
byte[] bytStr = GetInfo(aInfoType);
string retv = UniToCode(bytStr);
return retv.Substring(8, retv.Length - 8).Replace((char)0,' ').Trim();
}
else return Encoding.ASCII.GetString(GetInfo(aInfoType)).Replace((char) 0, ' ').Trim();
}

public string HexInfo(int aInfoType, int aCols) {
string retvHex = "";
int poz = 0;
int tcol = 0;
byte[] bts = GetInfo(aInfoType);
foreach (byte bt in bts) {
if (aInfoType == EXIF_USERCMNT) {
if (poz > 7) {
if (poz < bts.Length - 3) {
if (
(bts[poz] == (char) 0 &&
bts[poz + 1] == (char) 0 &&
bts[poz + 2] == (char) 0) ||
(bts[poz] == (char) 32 &&
bts[poz + 1] == (char) 32 &&
bts[poz + 2] == (char) 32) ||
(bts[poz] == (char) 46 &&
bts[poz + 1] == (char) 46 &&
bts[poz + 2] == (char) 46)
) {
return retvHex;
}
}
if (tcol == aCols) {
retvHex += "\n";
tcol = 0;
}
tcol++;
retvHex = retvHex + String.Format("{0:x2}", (int) bt) + " ";
}
poz++;
}
else {
if (tcol == aCols) {
retvHex += "\n";
tcol = 0;
}
tcol++;
retvHex = retvHex + String.Format("{0:x2}", (int) bt) + " ";
}
}
return retvHex;
}

private byte[] GetInfo(int aInfoType) {
foreach (PropertyItem prop in mPropItems) {
if (prop.Id == aInfoType) return prop.Value;
}
byte[] b = new byte[1];
return b;
}

private void GetExifProperties() {
RunMain.gLog.WriteLog("GetExifProperties for file " + mFile);
FileStream stream = new FileStream(mFile, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream, true, false);
mPropItems = image.PropertyItems;
stream.Close();
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top