如何:讀取影像中繼資料
某些影像檔案包含您可以讀取的中繼資料,以判斷影像的功能。 例如,數位相片可能包含您可以讀取的中繼資料,以判斷用來擷取影像之相機的製作和模型。 使用 GDI+,您可以讀取現有的中繼資料,也可以將新的中繼資料寫入影像檔。
GDI+ 會將個別的中繼資料片段儲存在 物件中 PropertyItem 。 您可以讀取 PropertyItems 物件的 屬性 Image ,以從檔案擷取所有中繼資料。 屬性 PropertyItems 會傳回 物件的陣列 PropertyItem 。
物件具有下列四個 PropertyItem 屬性: Id
、 Value
、 Len
和 Type
。
Id
識別中繼資料專案的標記。 下表顯示一些可指派給 Id 的值:
十六進位值 | 描述 |
---|---|
0x0320 0x010F 0x0110 0x9003 0x829A 0x5090 0x5091 |
影像標題 設備製造商 設備型號 ExifDTOriginal Exif 曝光時間 亮度表格 Chrominance 資料表 |
值
值的陣列。 值的格式是由 Type 屬性所決定。
Len
屬性所 Value 指向之值陣列的長度(以位元組為單位)。
類型
屬性所指向 Value
之陣列中值的資料類型。 下表顯示內容值所指示 Type
的格式:
數值 | 描述 |
---|---|
1 | 進行 Byte |
2 | 編碼為 ASCII 的物件陣列 Byte |
3 | 16 位整數 |
4 | 32 位整數 |
5 | 代表合理數位的兩 Byte 個 物件的陣列 |
6 | 未使用 |
7 | 未定義 |
8 | 未使用 |
9 | SLong |
10 | SRational |
範例
下列程式碼範例會讀取並顯示 檔案 FakePhoto.jpg
中的七個中繼資料片段。 清單中的第二個 (索引 1) 屬性專案有 Id 0x010F (設備製造商) 和 Type 2 (ASCII 編碼的位元組陣列)。 程式碼範例會顯示該屬性專案的值。
// Create an Image object.
Image image = new Bitmap(@"c:\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++;
}
// 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);
'Create an Image object.
Dim image As Bitmap = New Bitmap("c:\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
'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)
程式碼會產生類似下列的輸出:
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
The equipment make is Northwind Camera.
編譯程式碼
上述範例是為了搭配 Windows Form 使用而設計,且其需要 PaintEventArgse
,這是 Paint 事件處理常式的參數。 處理表單的事件 Paint ,並將此程式碼貼到繪製事件處理常式中。 您必須以系統上有效的映射名稱和路徑取代 FakePhoto.jpg
,並匯入 System.Drawing.Imaging
命名空間。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應