在Jpg vb.net上编辑IPTC元数据

时间:2013-07-11 10:24:51

标签: .net vb.net jpeg iptc

我需要将IPTC数据编辑为jpgs。

我使用此代码来阅读已有的关键字,但我无法编写新关键字:

IPTC .NET read/write c# library

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try

        Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read)
        Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
        Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)
        If metadata IsNot Nothing Then
            Keywords.Text = metadata.Keywords.Aggregate(Function(old, val) Convert.ToString(old) & "; " & Convert.ToString(val))
        End If


    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

任何帮助表示赞赏!感谢

1 个答案:

答案 0 :(得分:3)

    Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read)
    Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
    Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)
    If metadata IsNot Nothing Then
        Keywords.Text = metadata.Keywords.Aggregate(Function(old, val) Convert.ToString(old) & "; " & Convert.ToString(val))
    End If
    Dim bitmapFrame = decoder.Frames(0)
    metadata = bitmapFrame.Metadata.Clone()
    metadata.Keywords = New ReadOnlyCollection(Of String)(New List(Of String)(New String() {"test1", "test2"}))
    Dim stream1 = New FileStream(imagepath1, FileMode.Create)
    Dim encoder = New JpegBitmapEncoder()
    encoder.Frames.Add(bitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts))
    encoder.Save(stream1)

<强>更新 新代码替换了图像。

Imports System.Windows.Media.Imaging
Imports System.IO
Imports System.Collections.ObjectModel
Class MainWindow
    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        Dim imagepath = ".\img.jpg" ' path to file
        Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
        Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)
        If metadata IsNot Nothing Then
            Keywords.Text = metadata.Keywords.Aggregate(Function(old, val) Convert.ToString(old) & "; " & Convert.ToString(val))
        End If
        Description.Text = metadata.GetQuery("/app13/irb/8bimiptc/iptc/Caption") 'get description
        Dim bitmapFrame = decoder.Frames(0)
        metadata = bitmapFrame.Metadata.Clone()
        Dim newkeywords As New List(Of String)(New String() {"test1", "test2"})
        newkeywords.AddRange(metadata.Keywords) ' this strin adds old keywords
        metadata.Keywords = New ReadOnlyCollection(Of String)(newkeywords) 'replace keywords
        metadata.SetQuery("/app13/irb/8bimiptc/iptc/Caption", "My test picture1.") ' set new description
        Dim memstream As New MemoryStream() ' create temp storage in memory
        Dim encoder = New JpegBitmapEncoder()
        encoder.Frames.Add(bitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts))
        encoder.Save(memstream) ' save in memory
        stream.Close()
        stream = New FileStream(imagepath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
        memstream.Seek(0, System.IO.SeekOrigin.Begin) ' go to stream start
        Dim bytes(memstream.Length) As Byte
        memstream.Read(bytes, 0, memstream.Length)
        stream.Write(bytes, 0, bytes.Length)
        stream.Close()
        memstream.Close()
    End Sub
End Class

jpeg metadata here
IPTC字段here