tag:blogger.com,1999:blog-19178519879407338202024-02-08T10:25:37.898-08:00C# TutorialsFun Tutorials for C# programmers, expand you knowledge!Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-1917851987940733820.post-27752417006565187392011-05-25T11:47:00.000-07:002011-05-25T12:04:11.076-07:00How To Read Image Metadata In C#<span class="Apple-style-span" >Ok so lets get started, in this tutorial I will be showing you how you can extract metadata from an image file.<br /><br />Ok so what is metadata?, if you already know you can skip this paragraph, if not, metadata is hidden data which can be read to determine certain features of the image. features like the model of a camera and the date and time the photo was taken.<br /><br />We will be using the "System.Drawing.Imaging;<br />namespace for this tutorial, the reason for this is because it contains the "<span style="font-weight: bold; ">PropertyItem</span>" object which enables us to read the metadata from an image file. It does this using the following four properties:<br /><br /><ol><li>ID - the ID property can contain information about the camera used to take the photo. </li><li>Value - this is an array of values, the format is decided by the type property.</li><li>Len - this tells us the length (in bytes) of the value property.</li><li>Type - this gives us the datatype of the values in the array.</li></ol><br /><br />The only properties we will be taking notice of is ID and Value properties.<br /><br /><br /><span style="font-weight: bold; ">Step 1: loading in the image file</span><br />the first thing we need to do is load up visual studio, drag a button onto a windows form and call it 'Load'. Then we drag a PictureBox onto the form.<br /><br />Double click the button to create the 'Load_Click' in form1.cs.<br /><br />In form1.cs create a <span style="font-weight: bold; ">OpenFileDialog</span><br />obejct with the following properties as shown below:<br /><br /><span style="font-weight: bold; ">OpenFileDialog data = new OpenFileDialog();<br />private void Load_Click(object sender, EventArgs e)<br />{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>data.Title = "Open First Image File";<br /><span class="Apple-tab-span" style="white-space:pre"> </span>data.InitialDirectory = @"C:\Users\Karl\Downloads\Pictures";<br /><span class="Apple-tab-span" style="white-space:pre"> </span>data.Filter = "JPG files (*.jpg*)|*.jpg";<br /><span class="Apple-tab-span" style="white-space:pre"> </span>data.FilterIndex = 1;<br /><span class="Apple-tab-span" style="white-space:pre"> </span>data.RestoreDirectory = true;</span></span><div><span class="Apple-style-span" ><span style="font-weight: bold; "><br /><span class="Apple-tab-span" style="white-space:pre"> </span>if (data.ShowDialog() == DialogResult.OK)<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>pictureBox1.Load(data.FileName);<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br />}</span><br /><br /><br />these properties are all very straight forward but ill explain them just in case.<br /><br /><span style="font-weight: bold; ">data.Title</span><br />is the title that appears on top of the file dialog window.<br /><br /><span style="font-weight: bold; ">data.InitialDirectory</span><br />sets the default start point when looking for an image file.<br /><br /><span style="font-weight: bold; ">data.Filter</span><br />this will show only files with the .jpg extension.<br /><br /><span style="font-weight: bold; ">data.FilterIndex</span><br />this will give only one option when choosing file types<br /><br /><span style="font-weight: bold; ">data.RestoreDirectory</span><br />this always restores the directory to my pictures<br /><br /><br /><span style="font-weight: bold; ">Step two: retrieving the data </span><br /><br />In this step we are going to create a new method called<span style="font-weight: bold; ">public void meta()</span><br /><br /><br />in this method we need to add the following objects:<br /><span style="font-weight: bold; ">Bitmap image = new Bitmap(data.FileName);</span><br /><br /><span style="font-weight: bold; ">PropertyItem[] propItems = image.PropertyItems;</span><br /><br />as you can see the Bitmap object encapsulates the image which was loaded up in step one.<br /><br /><span style="font-weight: bold; ">data.Filename</span><br />now becomes the object ref <span style="font-weight: bold; ">image</span><br /><br />We use the PropertItem array to store all property items from the image as mentioned in the intro to this tutorial. this will store our meta information.<br /><br />Next we need to drag a listbox onto our windows form and in form1.cs add in a counter and a<br /><span style="font-weight: bold; ">foreach</span> loop to retrieve all information.<br /><br />the listbox will be populated with the metadata.<br /><br /><span style="font-weight: bold; ">int count = 0;<br />foreach (PropertyItem item in propItems)<br />{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox1.Items.Add("Property Item " + count.ToString());<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox1.Items.Add("iD: 0x" + item.Id.ToString("x"));<br /><span class="Apple-tab-span" style="white-space:pre"> </span>count++;<br />}</span><br /><br /><br />the <span style="font-weight: bold; ">int count</span> will +1 every time is goes through the foreach loop giving us the number of attributes in the metadata.<br />the <span style="font-weight: bold; ">item.Id</span> property should return a whole list of hex numbers.<br /><br />In this tutorial we are only interested in the following three:<br /><ol><li><span style="font-weight: bold; ">0x010F</span> - <i>this hex ID will give us the camera manufacturer</i> </li><li><span style="font-weight: bold; ">0x0110</span> - <i>and this will give us the model of the camera</i></li><li><span style="font-weight: bold; ">0x9003</span> - <i>this will give us the data and time the picture was taken</i></li></ol><br /><br /><br />Next we need to add the following code outside the foreach loop:<br /><br /><span style="font-weight: bold; ">ASCIIEncoding encodings = new ASCIIEncoding();<br /> try<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>string make = encodings.GetString(propItems[1].Value);<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("The equipment make is " + make + ".");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br /> catch<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("no Meta Data Found");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}</span><br /><br /><br />Here we are using the <span style="font-weight: bold; ">ASCIIEncoding</span> object to translate the array of Values mentioned earlier in the tutorial. this will give us, in plain English, the data we need.<br /><br /><span style="font-weight: bold; ">propItems[1].Value</span> - this gives us the Value of second item in the propItems array. which will be '0x010F' this is the manufacturer name.<br /><br />We could then add the following:<br /><br /><span style="font-weight: bold; ">try<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>string model = encodings.GetString(propItems[2].Value);<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("The model is " + model + ".");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br />catch<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("no Model Found");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br />try<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>string DT = encodings.GetString(propItems[15].Value);<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("The Date & Time is " + DT + ".");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br />catch<br /><span class="Apple-tab-span" style="white-space:pre"> </span>{<br /><span class="Apple-tab-span" style="white-space:pre"> </span>listBox3.Items.Add("no date Found");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}</span><br /><br /><br />this would give us the model of the camera and the date and time that the picture was taken.<br /><br />The only thing left to do now is drag a new button on the windows form and call it 'Meta Data' double click it to create 'MetaData_Click' in form1.cs and add call the meta method <span style="font-weight: bold; ">meta();</span><br />. and what you should see when the program is run is something similar to this:<br /><br /><a href="http://4.bp.blogspot.com/-hMCqjijS71c/Td1RcAPUm4I/AAAAAAAAADc/7Gktj1izDYw/s1600/finalfile.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 258px;" src="http://4.bp.blogspot.com/-hMCqjijS71c/Td1RcAPUm4I/AAAAAAAAADc/7Gktj1izDYw/s320/finalfile.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5610730252315958146" /></a></span><br /></div>Anonymoushttp://www.blogger.com/profile/03631013823036663113noreply@blogger.com0