Nasıl yapılır: Görüntü Meta Verilerini Okuma

Bazı görüntü dosyaları, görüntünün özelliklerini belirlemek için okuyabilecek meta veriler içerir. Örneğin dijital fotoğrafta, görüntüyü yakalamak için kullanılan kameranın make ve modelini belirlemek için okuyabilecek meta veriler yer alıyor olabilir. Bu GDI+, mevcut meta verileri okuyabilir ve görüntü dosyalarına yeni meta veriler de yazabilirsiniz.

GDI+ tek bir meta veri parçasını bir nesnede PropertyItem depolar. Bir dosyadan PropertyItems tüm meta verileri almak için bir nesnenin özelliğini Image okuyabilirsiniz. özelliği PropertyItems bir nesne dizisi PropertyItem döndürür.

Bir PropertyItem nesnenin şu dört özelliği vardır: , , ve IdValueLenType .

Id

Meta veri öğesini tanımlayan bir etiket. Atanabilir bazı değerler Id aşağıdaki tabloda gösterilmiştir:

Onaltılık değer Açıklama
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
Görüntü başlığı

Ekipman üreticisi

Ekipman modeli

ExifDTOriginal

Maruz kalma süresi

Luminance tablosu

Chrominance tablosu

Değer

Bir değer dizisi. Değerlerin biçimi özelliği tarafından Type belirlenir.

Len

özelliği tarafından işaret eden değer dizisinin uzunluğu (bayt Value cinsinden).

Tür

Özelliği tarafından işaret eden dizide yer alan değerlerin veri Value türü. Özellik değerleriyle gösterilen Type biçimler aşağıdaki tabloda gösterilmiştir:

Sayısal değer Açıklama
1 Byte
2 ByteASCII olarak kodlanmış nesneler dizisi
3 16 bit tamsayı
4 32 bit tamsayı
5 Rasyonal Byte sayını temsil eden iki nesne dizisi
6 Kullanılmıyor
7 Tanımsız
8 Kullanılmıyor
9 SLong
10 SRational

Örnek

Aşağıdaki kod örneği dosyasındaki yedi meta veriyi okur ve FakePhoto.jpg görüntüler. Listede ikinci (dizin 1) özellik öğesinde Id 0x010F (ekipman üreticisi) Type ve 2 (ASCII ile kodlanmış byte dizisi) vardır. Kod örneği, bu özellik öğesinin değerini görüntüler.

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

Kod aşağıdakine benzer bir çıkış oluşturur:

 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.

Kod Derleniyor

Yukarıdaki örnek, Windows Forms ile kullanım için tasarlanmıştır ve olay PaintEventArgse işleyicinin bir parametresi Paint olan 'i gerektirir. Formun olayı Paint işleyicisi ve bu kodu boya olay işleyicisine yapıştırın. yerine sisteminiz FakePhoto.jpg üzerinde geçerli olan bir görüntü adı ve yol yazın ve ad alanını içeri System.Drawing.Imaging aktarın.

Ayrıca bkz.