June 24, 2006

Getting Visual Studio 2005 and IIS 5.1 to Play Nice

Visual Studio 2005The other day I installed Visual Studio 2005 on one of my workstations and proceeded to create an ASP.NET web service. For the most part the Casinni webserver that's built into Visual Studio served my testing needs. But I eventually reached a point where I wanted to run the application on IIS just to make sure it encountered no problems in the target environment.

IISSo I installed IIS 5.1 on my Windows XP box and created a virtual directory containing the project. I set IIS to run it as an ASP.NET 2.0 application and was greeted with this error message when I browsed to the .asmx file:

Failed to access IIS metabase.
The process account used to run ASP.NET must have read access to the IIS metabase (e.g. IIS://servername/W3SVC).

I was referred to a Microsoft Knowledge Base article. The problem supposedly happened because the ASP.NET worker process didn't have permissions to the mystery that is the IIS metabase. My guess is that this ocurred because I installed IIS after I installed Visual Studio. Microsoft recommended running the following command line as a remedy:

aspnet_regiis –ga [WindowsUserAccount]

I substituted ASPNET (the ASP.NET account on Windows XP) in for [WindowsUserAccount]. Unfortunately this did not solve the problem and I continued getting the same error mentioned above.

After searching some forums I found others who had encountered this issue who advised reinstalling the .NET 2.0 Framework. I went ahead and did this as follows and it resolved the incident:

  • Run the Visual Studio 2005 Disk

  • Select "Change or Remove Visual Studio 2005"

  • Choose Repair/Reinstall

June 11, 2006

Retrieving "Date Taken" EXIF Metadata From a JPG

For the most part the GDI+ API in the .NET Framework 2.0 is powerful enough to match almost any image related challenge. However, I encountered some functionality that I found to be lacking in regards to how EXIF embedded metadata is retrieved from JPG files. My goal was to get the "Date Taken" property from an image using C#.

Unfortunately each slice of the image's metadata (AKA the PropertyItem object) can only be retrieved using a rather unintuitive integer-based ID look up. For example, you have to know that the "Date Taken" information can be accessed using a PropertyItem ID of 36867. Perhaps an enumeration of these IDs will be added to a future version of the Framework to help make this task easier for programmers? Justin Rogers made such an object available here to help folks out in the meantime.

Once the "Date Taken" PropertyItem has been defined we still run into problems after decoding it to a UTF8 string. To have the most flexibility in addressing this information it would be ideal to convert it to a DateTime object. However, the PropertyItem string value is decoded in a "YYYY:MM:DD HH:MM:SS" format. Sadly, the colons between the year and the month and the month and the day throw off both the Convert.ToDateTime() and DateTime.Parse() methods.

I've listed the GetDateTaken() method below as an example of the remaining string manipulation necessary to complete this task:

public DateTime GetDateTaken(Image targetImg)
//Property Item 36867 corresponds to the Date Taken
PropertyItem propItem = targetImg.GetPropertyItem(36867);
DateTime dtaken;

//Convert date taken metadata to a DateTime object
string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
string firsthalf = sdate.Substring(0, 10);
firsthalf = firsthalf.Replace(":", "-");
sdate = firsthalf + secondhalf;
dtaken = DateTime.Parse(sdate);
return dtaken;