Saturday, September 15, 2012

How to read image properties (Metadata) in asp.net

Some image files contain metadata that you can read to determine features of the image. For example, a digital photograph might contain metadata that you can read to determine the make, model of the camera used to capture the image and Date taken. With GDI+, you can read existing metadata, and you can also write new metadata to image files. This tutorial show you how to take the image date taken in asp.net.
Image Property Date Taken
 Copy the following server control to <form> tag.
 <asp:Label ID="label1" runat="server" Text =""></asp:Label>
In code-behind, look like as below
protected void Page_Load(object sender, EventArgs e)
{
    string path = @"G:\test.jpg"; //your image physical path here
    label1.Text = TakenDate(System.Drawing.Image.FromFile(path)).ToString();

}
public DateTime TakenDate(System.Drawing.Image img)
{
    DateTime dt = DateTime.MinValue;
    try
    {
        int DateTakenValue = 0x9003; // 36867
        if (img != null && img.PropertyIdList.Contains(DateTakenValue))
        {
            string dateTakenTag = System.Text.Encoding.UTF8.GetString(img.GetPropertyItem(DateTakenValue).Value);
            string[] parts = dateTakenTag.Split(':', ' ');
            int year = int.Parse(parts[0]);
            int month = int.Parse(parts[1]);
            int day = int.Parse(parts[2]);
            int hour = int.Parse(parts[3]);
            int minute = int.Parse(parts[4]);
            int second = int.Parse(parts[5]);
            dt = new DateTime(year, month, day, hour, minute, second);
        }
    }
    catch (Exception ex)
    {
    }
    return dt;
}
That's it. Run the application and see the image-date-taken in page.
More detail-how to read Image Metadata

No comments:

Post a Comment