1

I searched stackoverflow and I realized that GetPropertyItem and SetPropertyItem can edit comments in JPEG file

Dim images As Image = System.Drawing.Image.FromFile("C:\\Sample.jpeg")
Dim MSGF As New ArrayList
Dim ID() As String = {"hello ","i am here"}
Dim propItem As PropertyItem = images.GetPropertyItem(40092)
Dim encoderParameters As New EncoderParameters(1)
encoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 100L)

For i = 0 To ID.Length - 1
    Dim TEMP As String = ID(i)
    For II = 0 To TEMP.Length - 1
        MSGF.Add(Convert.ToInt32(TEMP(II)))
    Next
Next
For i = 0 To MSGF.Count - 1
    propItem.Value.SetValue(Convert.ToByte(MSGF(i)), i)
Next

images.SetPropertyItem(propItem)
images.Save(TextBox1.Text & "\" & "1" & TextBox2.Text)

What I realized was I can get comments from jpeg file by GetPropertyItem. However, comments is based on the ascii code. Therefore, I was trying to convert comment that I wanted to insert to ascii code.

propItem.Value.SetValue(Convert.ToByte(MSGF(i)), i)

This part was actually changed comments which already existed in the jpeg file. However, if there is no comments in jpeg file, propItem.value.setValue doesn't work because there is nothing to edit.

Is there anyway to just add comments to jpeg file?

djv
  • 15,168
  • 7
  • 48
  • 72
NBB
  • 137
  • 2
  • 6
  • 14
  • 1
    Based on [the MSDN page for PropertyItem](https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem(v=vs.110).aspx), `A PropertyItem object is used to retrieve and to change the metadata of existing image files, not to create the metadata.`, so I don't think the approach is correct. See [this answer](https://stackoverflow.com/a/23762564/832052) for an entirely different approach, which seems to do what you need. – djv May 26 '17 at 18:01
  • @djv thanks for your answer. i think, i saw this solution before. And, I was trying to convert c# version to VB.NET. But, it wasn't working very well. I wish i can find VB.NET way to add comments to jpeg file :( – NBB May 26 '17 at 18:45
  • Did you try using an [**online converter**](http://converter.telerik.com)? – Visual Vincent May 26 '17 at 20:42
  • Converted the code for you in an answer – djv May 26 '17 at 21:22

1 Answers1

2

Based on this answer in C#, it could be as simple as this:

Dim jpeg = New JpegMetadataAdapter(pathToJpeg)
jpeg.Metadata.Comment = "Some comments"
jpeg.Metadata.Title = "A title"
jpeg.Save()
' Saves the jpeg in-place
jpeg.SaveAs(someNewPath)
' Saves with a new path

Here is the class:

Public Class JpegMetadataAdapter
    Private ReadOnly path As String
    Private frame As BitmapFrame
    Public ReadOnly Metadata As BitmapMetadata

    Public Sub New(path As String)
        Me.path = path
        frame = getBitmapFrame(path)
        Metadata = DirectCast(frame.Metadata.Clone(), BitmapMetadata)
    End Sub

    Public Sub Save()
        SaveAs(path)
    End Sub

    Public Sub SaveAs(path As String)
        Dim encoder As New JpegBitmapEncoder()
        encoder.Frames.Add(BitmapFrame.Create(frame, frame.Thumbnail, Metadata, frame.ColorContexts))
        Using stream As Stream = File.Open(path, FileMode.Create, FileAccess.ReadWrite)
            encoder.Save(stream)
        End Using
    End Sub

    Private Function getBitmapFrame(path As String) As BitmapFrame
        Dim decoder As BitmapDecoder = Nothing
        Using stream As Stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
            decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)
        End Using
        Return decoder.Frames(0)
    End Function
End Class
djv
  • 15,168
  • 7
  • 48
  • 72