Export (0) Print
Expand All

PropertyItem Class

Encapsulates a metadata property to be included in an image file. Not inheritable.

Namespace: System.Drawing.Imaging
Assembly: System.Drawing (in system.drawing.dll)

public sealed class PropertyItem
public final class PropertyItem
public final class PropertyItem
Not applicable.

The data consists of: an identifier, the length (in bytes) of the property, the property type, and a pointer to the property value.

The following code example demonstrates how to read and display the metadata in an image file using the PropertyItem class and the Image.PropertyItems property.

This example is designed to be used in a Windows Form that imports the System.Drawing.Imaging namespace. Paste the code into the form and change the path to fakePhoto.jpg to point to an image file on your system. Call the ExtractMetaData method when handling the form's Paint event, passing e as PaintEventArgs.

private void ExtractMetaData(PaintEventArgs e)
{
    try
    {
        // Create an Image object. 
        Image theImage = new Bitmap("c:\\fakePhoto.jpg");

        // Get the PropertyItems property from image.
        PropertyItem[] propItems = theImage.PropertyItems;

        // Set up the display.
        Font font1 = new Font("Arial", 10);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        int X = 0;
        int Y = 0;

        // For each PropertyItem in the array, display the id, 
        // type, and length.
        int count = 0;
        foreach ( PropertyItem propItem in propItems )
        {
            e.Graphics.DrawString("Property Item " + 
                count.ToString(), font1, blackBrush, X, Y);
            Y += font1.Height;

            e.Graphics.DrawString("   ID: 0x" + 
                propItem.Id.ToString("x"), font1, blackBrush, X, Y);
            Y += font1.Height;

            e.Graphics.DrawString("   type: " +
                propItem.Type.ToString(), font1, blackBrush, X, Y);
            Y += font1.Height;

            e.Graphics.DrawString("   length: " + 
                propItem.Len.ToString() + 
                " bytes", font1, blackBrush, X, Y);
            Y += font1.Height;
            count += 1;
        }
        font1.Dispose();
    }
    catch(Exception)
    {
        MessageBox.Show("There was an error." + 
            "Make sure the path to the image file is valid.");
    }

}

private void ExtractMetaData(PaintEventArgs e)
{
    try {
        // Create an Image object. 
        Image theImage = new Bitmap("c:\\fakePhoto.jpg");

        // Get the PropertyItems property from image.
        PropertyItem propItems[] = theImage.get_PropertyItems();

        // Set up the display.
        Font font1 = new Font("Arial", 10);
        SolidBrush blackBrush = new SolidBrush(Color.get_Black());
        int X = 0;
        int Y = 0;

        // For each PropertyItem in the array, display the id, 
        // type, and length.
        int count = 0;

        //foreach (PropertyItem propItem in propItems)
        for (int iCtr = 0; iCtr < propItems.length; iCtr++) {
            PropertyItem propItem = propItems[iCtr];
            e.get_Graphics().DrawString("Property Item "  
                + System.Convert.ToString(count), font1, blackBrush, X, Y);
            Y += font1.get_Height();
            e.get_Graphics().DrawString("   ID: 0x" 
                + ((System.Int32)propItem.get_Id()).ToString("x"), font1, 
                blackBrush, X, Y);
            Y += font1.get_Height();
            e.get_Graphics().DrawString("   type: "  
                + System.Convert.ToString(propItem.get_Type()), font1, 
                blackBrush, X, Y);
            Y += font1.get_Height();
            e.get_Graphics().DrawString("   length: "  
                + System.Convert.ToString(propItem.get_Len()) + " bytes", 
                font1, blackBrush, X, Y);
            Y += font1.get_Height();
            count += 1;
        }
        font1.Dispose();
    }
    catch (System.Exception exp) {
        MessageBox.Show(("There was an error."  
            + "Make sure the path to the image file is valid."));
    }
} //ExtractMetaData

System.Object
  System.Drawing.Imaging.PropertyItem

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft