PropertyItem 类

封装要包括到图像文件中的元数据属性。不可继承。

**命名空间:**System.Drawing.Imaging
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
Public NotInheritable Class PropertyItem
用法
Dim instance As PropertyItem
public sealed class PropertyItem
public ref class PropertyItem sealed
public final class PropertyItem
public final class PropertyItem

备注

该数据由标识符、属性的长度(以字节为单位)、属性类型和一个指向属性值的指针组成。

示例

下面的代码示例演示如何使用 PropertyItem 类和 Image.PropertyItems 属性读取和显示一个图像文件中的元数据。

此示例旨在用于 Windows 窗体,Windows 窗体导入 System.Drawing.Imaging 命名空间。将该代码粘贴到窗体中,并将路径更改为 fakePhoto.jpg,以指向系统中的一个图像文件。处理窗体的 Paint 事件时,调用 ExtractMetaData 方法,将 e 作为 PaintEventArgs 传递。

Private Sub ExtractMetaData(ByVal e As PaintEventArgs)

    Try
        'Create an Image object. 
        Dim theImage As Image = New Bitmap("c:\fakePhoto.jpg")

        'Get the PropertyItems property from image.
        Dim propItems As PropertyItem() = theImage.PropertyItems

        'Set up the display.
        Dim font As New font("Arial", 10)
        Dim blackBrush As New SolidBrush(Color.Black)
        Dim X As Integer = 0
        Dim Y As Integer = 0

        'For each PropertyItem in the array, display the id, type, and length.
        Dim count As Integer = 0
        Dim propItem As PropertyItem
        For Each propItem In propItems

            e.Graphics.DrawString("Property Item " + count.ToString(), _
               font, blackBrush, X, Y)
            Y += font.Height

            e.Graphics.DrawString("   iD: 0x" & propItem.Id.ToString("x"), _
               font, blackBrush, X, Y)
            Y += font.Height

            e.Graphics.DrawString("   type: " & propItem.Type.ToString(), _
               font, blackBrush, X, Y)
            Y += font.Height

            e.Graphics.DrawString("   length: " & propItem.Len.ToString() & _
                " bytes", font, blackBrush, X, Y)
            Y += font.Height

            count += 1
        Next propItem

        font.Dispose()
    Catch ex As ArgumentException
        MessageBox.Show("There was an error. Make sure the path to the image file is valid.")
    End Try

End Sub
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 = gcnew Bitmap( "c:\\fakePhoto.jpg" );
         
         // Get the PropertyItems property from image.
         array<PropertyItem^>^propItems = theImage->PropertyItems;
         
         // Set up the display.
         System::Drawing::Font^ font1 = gcnew System::Drawing::Font( "Arial",10 );
         SolidBrush^ blackBrush = gcnew SolidBrush( Color::Black );
         int X = 0;
         int Y = 0;
         
         // For each PropertyItem in the array, display the id, 
         // type, and length.
         int count = 0;
         System::Collections::IEnumerator^ myEnum = propItems->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            PropertyItem^ propItem = safe_cast<PropertyItem^>(myEnum->Current);
            e->Graphics->DrawString( String::Format( "Property Item {0}", count ), font1, blackBrush, (float)X, (float)Y );
            Y += font1->Height;
            e->Graphics->DrawString( String::Format( "   ID: 0x{0}", propItem->Id.ToString( "x" ) ), font1, blackBrush, (float)X, (float)Y );
            Y += font1->Height;
            e->Graphics->DrawString( String::Format( "   type: {0}", propItem->Type ), font1, blackBrush, (float)X, (float)Y );
            Y += font1->Height;
            e->Graphics->DrawString( String::Format( "   length: {0} bytes", propItem->Len ), font1, blackBrush, (float)X, (float)Y );
            Y += font1->Height;
            count += 1;
         }
         delete font1;
      }
      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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

PropertyItem 成员
System.Drawing.Imaging 命名空间

其他资源

如何:读取图像元数据