【发布时间】: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 PropertyItem,
A 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?
-
在答案中为您转换了代码