Share via


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 okuyabileceğiniz meta veriler içerir. Örneğin dijital fotoğraf, görüntüyü yakalamak için kullanılan kameranın modelini ve modelini belirlemek için okuyabileceğiniz meta veriler içerebilir. GDI+ ile mevcut meta verileri okuyabilir ve görüntü dosyalarına yeni meta veriler yazabilirsiniz.

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

Bir PropertyItem nesne şu dört özelliğe sahiptir: Id, Value, Lenve Type.

Id

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

Onaltılık değer Tanım
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
Resim başlığı

Ekipman üreticisi

Ekipman modeli

ExifDTOriginal

Açığa çıkarma süresi

Parlaklık 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 edilen değer dizisinin uzunluğu (bayt cinsinden).Value

Tür

Özelliği tarafından işaret edilen dizideki değerlerin Value veri türü. Özellik değerleri tarafından Type belirtilen biçimler aşağıdaki tabloda gösterilmiştir:

Sayısal değer Tanım
Kategori 1 Byte
2 ASCII olarak kodlanmış bir nesne dizisi Byte
3 16 bit tamsayı
4 32 bit tamsayı
5 Rasyonel bir sayıyı temsil eden iki Byte nesneden oluşan bir dizi
6 Kullanılmıyor
7 Undefined
8 Kullanılmıyor
9 SLong
10 SRational

Örnek

Aşağıdaki kod örneği dosyasında FakePhoto.jpgyedi meta veri parçasını okur ve görüntüler. Listedeki ikinci (dizin 1) özellik öğesi 0x010F (ekipman üreticisi) ve Type 2 (ASCII kodlu bayt dizisi) içerirId. 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ılmak üzere tasarlanmıştır ve olay işleyicisinin Paint parametresi olan öğesini gerektirir.PaintEventArgse Formun Paint olayını işleyip bu kodu paint olay işleyicisine yapıştırın. öğesini sisteminizde geçerli bir görüntü adı ve yolla değiştirmeniz FakePhoto.jpg ve ad alanını System.Drawing.Imaging içeri aktarmanız gerekir.

Ayrıca bkz.