【问题标题】:VB.NET writing comments to jpeg file programmaticallyVB.NET 以编程方式将注释写入 jpeg 文件
【发布时间】:2017-10-27 16:04:31
【问题描述】:

我搜索了stackoverflow,发现GetPropertyItem和SetPropertyItem可以编辑JPEG文件中的cmets

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)

我意识到我可以通过 GetPropertyItem 从 jpeg 文件中获取 cmets。但是,cmets 是基于 ascii 代码的。因此,我试图将要插入的注释转换为 ascii 代码。

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

这部分实际上是修改了jpeg文件中已经存在的cmets。 但是,如果 jpeg 文件中没有 cmets,propItem.value.setValue 将不起作用,因为没有可编辑的内容。

有没有办法只将 cmets 添加到 jpeg 文件中?

【问题讨论】:

  • 基于the MSDN page for PropertyItemA PropertyItem object is used to retrieve and to change the metadata of existing image files, not to create the metadata.,所以我认为这种方法不正确。请参阅 this answer 了解完全不同的方法,这似乎可以满足您的需求。
  • @djv 感谢您的回答。我想,我以前看过这个解决方案。而且,我试图将 c# 版本转换为 VB.NET。但是,它运行得不是很好。我希望我能找到将 cmets 添加到 jpeg 文件的 VB.NET 方法:(
  • 您是否尝试过使用online converter
  • 在答案中为您转换了代码

标签: vb.net metadata jpeg


【解决方案1】:

基于this answer in C#,可以这么简单:

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

这是课程:

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

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2014-02-20
    • 2023-03-19
    • 2017-02-01
    相关资源
    最近更新 更多