gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:49:51 25 4
gpt4 key购买 nike

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

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

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

任何帮助表示赞赏!谢谢

最佳答案

    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 元数据 here .
IPTC领域 here .

关于.net - 在 Jpg vb.net 上编辑 IPTC 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17590952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com