共用方式為


讀取中繼資料

有些影像檔案中會包含中繼資料 (Metadata),您可以讀取這項資料來判斷影像的特性。例如,讀取數位相片中所包含的中繼資料,可以判斷出捕捉影像時所使用的相機品牌和型號。在 GDI+ 中,您可以讀取現有的中繼資料,也可以將新的中繼資料寫入影像檔案中。

GDI+ 會將各筆中繼資料儲存在 PropertyItem 物件中。您可以讀取 Image 物件的 PropertyItems 屬性來擷取檔案中的所有中繼資料。PropertyItems 屬性會傳回 PropertyItem 物件的陣列。

PropertyItem 物件具有下列四個屬性:

Id

識別中繼資料項目的標記 (Tag)。部份可指派給 Id 的值列在下列表格中。

十六進位值 說明
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

影像標題

設備製造商

設備型號

ExifDTOriginal

Exif 曝光時間

亮度表

色度表

Value

數值陣列。數值的格式是由 Type 屬性來決定。

Len

Value 屬性所指向數值陣列的長度 (以位元組為單位)。

Type

Value 屬性所指向陣列中數值的資料型別。Type 屬性值所指定的格式列在下列表格中。

數值 說明
1 位元組
2 以 ASCII 編碼的 Byte 物件陣列
3 16 位元整數
4 32 位元整數
5 代表有理數的兩個 Byte 物件陣列
6 未使用
7 未定義
8 未使用
9 SLong
10 SRational

下列範例會讀取並顯示 FakePhoto.jpg 檔案中的七筆中繼資料。

'Create an Image object. 
Dim image = New Bitmap("FakePhoto.jpg")
      
'Get the PropertyItems property from image.
Dim propItems As PropertyItem() = image.PropertyItems
      
'Set up the display.
Dim font As New Font("Arial", 12)
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
[C#]
//Create an Image object. 
Image image = new Bitmap("FakePhoto.jpg");

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

//Set up the display.
Font font = new Font("Arial", 12);
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(),
   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++;
}

上述程式碼所產生的輸出與下圖類似。

Property Item 0
  id: 0x320
  type: 2
  length: 16 bytes

Property Item 1
  id: 0x10f
  type: 2
  length: 17 bytes

Property Item 2
  id: 0x110
  type: 2
  length: 7 bytes

Property Item 3
  id: 0x9003
  type: 2
  length: 20 bytes

Property Item 4
  id: 0x829a
  type: 5
  length: 8 bytes

Property Item 5
  id: 0x5090
  type: 3
  length: 128 bytes

Property Item 6
  id: 0x5091
  type: 3
  length: 128 bytes

清單中第二個 (索引為 1) 屬性項目的 Id 為 0x010F (設備製造商),Type 為 2 (以 ASCII 編碼的 Byte 陣列)。下列程式碼是上述範例的延續,它會顯示這個屬性項目的值:

'Convert the value of the second property to a string, and display it.
Dim encoding As New System.Text.ASCIIEncoding()
Dim manufacturer As String = encoding.GetString(propItems(1).Value)
      
e.Graphics.DrawString( _
   "The equipment make is " + manufacturer + ".", _
   font, _
   blackBrush, _
   X, Y)
[C#]
//Convert the value of the second property to a string, and display it.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string manufacturer = encoding.GetString(propItems[1].Value);

e.Graphics.DrawString(
   "The equipment make is " + manufacturer + ".",
   font,
   blackBrush,
   X, Y);

上述程式碼所產生的輸出與下圖類似。

The equipment make is Northwind Camera.