방법: 이미지 메타데이터 읽기

일부 이미지 파일에는 이미지의 기능을 확인하기 위해 읽을 수 있는 메타데이터가 포함되어 있습니다. 예를 들어 디지털 사진에는 이미지를 캡처하는 데 사용되는 카메라의 메이크 및 모델을 결정하기 위해 읽을 수 있는 메타데이터가 포함될 수 있습니다. GDI+를 사용하면 기존 메타데이터를 읽을 수 있으며 이미지 파일에 새 메타데이터를 작성할 수도 있습니다.

GDI+는 PropertyItem 개체에 개별 메타데이터를 저장합니다. Image 개체의 PropertyItems 속성을 읽어 파일에서 모든 메타데이터를 검색할 수 있습니다. PropertyItems 속성은 PropertyItem 개체의 배열을 반환합니다.

PropertyItem 개체에는 Id, Value, Len, Type과 같은 네 가지 속성이 있습니다.

Id

메타데이터 항목을 식별하는 태그입니다. Id에 할당할 수 있는 일부 값은 다음 표에 나와 있습니다.

16 진수 값 Description
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
이미지 제목

장비 제조업체

장비 모델

ExifDTOriginal

Exif 노출 시간

광도 테이블

색차 테이블

값의 배열입니다. 값의 형식은 Type 속성에 의해 결정됩니다.

Len

Value 속성이 가리키는 값 배열의 길이(바이트)입니다.

Type

Value 속성이 가리키는 배열 값의 데이터 형식입니다. Type 속성 값으로 표시된 형식은 다음 표에 나와 있습니다.

숫자 값 Description
1 Byte
2 ASCII로 인코딩된 Byte 개체의 배열
3 16비트 정수
4 32비트 정수
5 유리수를 나타내는 두 Byte 개체의 배열
6 사용되지 않음
7 정의되지 않음
8 사용되지 않음
9 SLong
10 SRational

예제

다음 코드 예제에서는 파일 FakePhoto.jpg에 있는 7개의 메타데이터를 읽고 표시합니다. 목록의 두 번째(인덱스 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 forms에서 사용하도록 설계되었으며 PaintEventArgs 이벤트 처리기의 매개 변수인 ePaint가 필요합니다. 양식의 Paint 이벤트를 처리하고 이 코드를 페인트 이벤트 처리기에 붙여넣습니다. FakePhoto.jpg를 시스템에서 유효한 이미지 이름 및 경로로 바꾸고 System.Drawing.Imaging 네임스페이스를 가져와야 합니다.

참고 항목