Bagikan melalui


Cara: Membaca Metadata Gambar

Beberapa file gambar berisi metadata yang dapat Anda baca untuk menentukan fitur gambar. Misalnya, foto digital mungkin berisi metadata yang dapat Anda baca untuk menentukan pembuatan dan model kamera yang digunakan untuk mengambil gambar. Dengan GDI+, Anda dapat membaca metadata yang ada, dan Anda juga dapat menulis metadata baru ke file gambar.

GDI+ menyimpan satu bagian metadata individual dalam objek PropertyItem . Anda dapat membaca PropertyItems properti Image objek untuk mengambil semua metadata dari file. Properti PropertyItems mengembalikan array PropertyItem objek.

Objek PropertyItem memiliki empat properti berikut: Id, , Value, Lendan Type.

Id

Tag yang mengidentifikasi item metadata. Beberapa nilai yang dapat ditetapkan Id diperlihatkan dalam tabel berikut:

Nilai heksadesimal Deskripsi
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
Judul gambar

Produsen peralatan

Model peralatan

ExifDTOriginal

Waktu paparan eksif

Tabel luminance

Tabel Chrominance

Nilai

Array nilai. Format nilai ditentukan oleh Type properti .

Len

Panjang (dalam byte) dari array nilai yang ditujukkan oleh Value properti .

Jenis

Jenis data nilai dalam array yang ditujukkan oleh Value properti . Format yang ditunjukkan Type oleh nilai properti diperlihatkan dalam tabel berikut:

Nilai numerik Deskripsi
1 Byte
2 Array Byte objek yang dikodekan sebagai ASCII
3 Bilangan bulat 16-bit
4 Bilangan bulat 32-bit
5 Array dari dua Byte objek yang mewakili angka rasional
6 Tidak digunakan
7 Tidak terdefinisi
8 Tidak digunakan
9 SLong
10 SRational

Contoh

Contoh kode berikut membaca dan menampilkan tujuh bagian metadata dalam file FakePhoto.jpg. Item properti kedua (indeks 1) dalam daftar memiliki Id 0x010F (produsen peralatan) dan Type 2 (array byte yang dikodekan ASCII). Contoh kode menampilkan nilai item properti tersebut.

// 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)

Kode menghasilkan output yang mirip dengan yang berikut:

 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.

Mengompilasi Kode

Contoh sebelumnya dirancang untuk digunakan dengan Formulir Windows, dan memerlukan PaintEventArgse, yang merupakan parameter penanganan Paint aktivitas. Tangani peristiwa formulir Paint dan tempelkan kode ini ke dalam penanganan aktivitas cat. Anda harus mengganti FakePhoto.jpg dengan nama gambar dan jalur yang valid pada sistem Anda dan mengimpor System.Drawing.Imaging namespace.

Baca juga