ClearExif.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) 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

'' Clear all Exif tags found in an image.
'' This sample is almost identical to @(ShowExif} but prior to printing
'' Exif tags found in the image it clears all those tags.
'' The expected result is that no tags are found.
Public Class ClearExif
    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"))
                '' Remove all existing values from the image's Exif profile:
                testImage.ExifProfile.Clear()
                '' Note: Individual tags can be removed via testImage.ExifProfile.RemoveValue(tag)

                '' The remaining code is same as in ShowExif:
                Dim tl = g.CreateTextLayout()
                tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.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())
        '' First 8 symbols in UserComment specify encoding:
        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