ShowExif.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
'' 
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Imaging.Exif
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' このサンプルでは、画像内で検出されたExifタグを出力します。
'' 同じコードを使用し、事前にExifタグを削除する例として ClearExif も参照してください。
Public Class ShowExif
    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim pad = 20
        Dim side = pixelSize.Width / 3
        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)
            Using testImage = New GcBitmap(Path.Combine("Resources", "ImagesBis", "fire.jpg"))
                Dim tl = g.CreateTextLayout()
                tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf"))
                tl.DefaultFormat.FontSize = 11
                tl.DefaultTabStops = 160
                tl.MarginTop = pad
                tl.MarginBottom = pad
                tl.MarginLeft = pad * 2 + side
                tl.MarginRight = pad
                tl.MaxWidth = pixelSize.Width
                AppendExifData(tl, testImage.ExifProfile)
                g.DrawImage(testImage, New RectangleF(pad, pad, side, side), Nothing, ImageAlign.ScaleImage)
                If tl.Lines.Count = 0 Then
                    tl.AppendLine("No EXIF tags found.")
                End If
                g.DrawTextLayout(tl, PointF.Empty)
            End Using
        End Using
        Return bmp
    End Function

    Private Sub AppendExifData(ByVal tl As TextLayout, ByVal ep As ExifProfile)
        Append(tl, ep, ExifTag.Make, ep.Make)
        Append(tl, ep, ExifTag.Model, ep.Model)
        Append(tl, ep, ExifTag.XResolution, ep.XResolution?.ToString())
        Append(tl, ep, ExifTag.YResolution, ep.YResolution?.ToString())
        Append(tl, ep, ExifTag.ResolutionUnit, ep.ResolutionUnit.ToString())
        Append(tl, ep, ExifTag.Software, ep.Software)
        Append(tl, ep, ExifTag.DateTime, ep.DateTimeRaw)
        Append(tl, ep, ExifTag.Artist, ep.Artist)
        Append(tl, ep, ExifTag.Copyright, ep.Copyright)
        Append(tl, ep, ExifTag.ExposureTime, ep.ExposureTime?.ToString())
        Append(tl, ep, ExifTag.FNumber, ep.FNumber?.ToString())
        Append(tl, ep, ExifTag.ExposureProgram, ep.ExposureProgram.ToString())
        Append(tl, ep, ExifTag.PhotographicSensitivity, ep.PhotographicSensitivity?.ToString())
        Append(tl, ep, ExifTag.ExifVersion, ep.ExifVersion)
        Append(tl, ep, ExifTag.DateTimeOriginal, ep.DateTimeOriginalRaw)
        Append(tl, ep, ExifTag.DateTimeDigitized, ep.DateTimeDigitizedRaw)
        Append(tl, ep, ExifTag.ShutterSpeedValue, ep.ShutterSpeedValue?.ToString())
        Append(tl, ep, ExifTag.ApertureValue, ep.ApertureValue?.ToString())
        Append(tl, ep, ExifTag.ExposureBiasValue, ep.ExposureBiasValue?.ToString())
        Append(tl, ep, ExifTag.MaxApertureValue, ep.MaxApertureValue?.ToString())
        Append(tl, ep, ExifTag.MeteringMode, ep.MeteringMode.ToString())
        Append(tl, ep, ExifTag.LightSource, ep.LightSource.ToString())
        Append(tl, ep, ExifTag.Flash, ep.Flash.ToString())
        Append(tl, ep, ExifTag.FocalLength, ep.FocalLength?.ToString())
        '' UserCommentの最初の8文字はエンコーディングを指定します。
        Append(tl, ep, ExifTag.UserComment, ep.UserComment?.Substring(8))
        Append(tl, ep, ExifTag.SubsecTimeOriginal, ep.SubsecTimeOriginal?.ToString())
        Append(tl, ep, ExifTag.SubsecTimeDigitized, ep.SubsecTimeDigitized?.ToString())
        Append(tl, ep, ExifTag.ColorSpace, ep.ColorSpace.ToString())
        Append(tl, ep, ExifTag.SensingMethod, ep.SensingMethod.ToString())
        Append(tl, ep, ExifTag.FileSource, ep.FileSource.ToString())
        Append(tl, ep, ExifTag.SceneType, ep.SceneType?.ToString())
        Append(tl, ep, ExifTag.CFAPattern, ep(ExifTag.CFAPattern)?.ToString())
        Append(tl, ep, ExifTag.CustomRendered, ep.CustomRendered.ToString())
        Append(tl, ep, ExifTag.ExposureMode, ep.ExposureMode.ToString())
        Append(tl, ep, ExifTag.WhiteBalance, ep.WhiteBalance.ToString())
        Append(tl, ep, ExifTag.DigitalZoomRatio, ep.DigitalZoomRatio?.ToString())
        Append(tl, ep, ExifTag.FocalLengthIn35mmFilm, ep.FocalLengthIn35mmFilm?.ToString())
        Append(tl, ep, ExifTag.SceneCaptureType, ep.SceneCaptureType.ToString())
        Append(tl, ep, ExifTag.Contrast, ep.Contrast.ToString())
        Append(tl, ep, ExifTag.Saturation, ep.Saturation.ToString())
        Append(tl, ep, ExifTag.Sharpness, ep.Sharpness.ToString())
        Append(tl, ep, ExifTag.SubjectDistanceRange, ep.SubjectDistanceRange.ToString())
        Append(tl, ep, ExifTag.BodySerialNumber, ep.BodySerialNumber)
        Append(tl, ep, ExifTag.LensSpecification, ep(ExifTag.LensSpecification)?.ToString())
        Append(tl, ep, ExifTag.LensModel, ep.LensModel)
    End Sub

    Private Sub Append(ByVal tl As TextLayout, ByVal ep As ExifProfile, ByVal tag As ExifTag, ByVal text As String)
        If ep.HasValue(tag) Then
            tl.AppendLine(tag.ToString() + vbTab + text)
        End If
    End Sub
End Class