使用System.Drawing.Image进行标题编辑对我不起作用



我已经创建了一些函数来编辑 Jpeg 图像的 Exif 元数据,现在我可以编辑注释,但是当我尝试编辑标题时它不起作用,没有显示任何错误,我想通过替换原始文件来保存它,这是我到目前为止创建的代码:

System.Drawing.Image image = null;
protected void EditCommentBtn_Click(object sender, EventArgs e)
{
image = System.Drawing.Image.FromFile(filepath);
PropertyItem propItem = image.PropertyItems[0];
using (var file = System.Drawing.Image.FromFile(filepath))
{
propItem.Id = 0x9286; //Usercomment
propItem.Type = 2;
propItem.Value = System.Text.Encoding.UTF8.GetBytes("HelloWorld");
propItem.Len = propItem.Value.Length;
file.SetPropertyItem(propItem);
PropertyItem propItem1 = file.PropertyItems[file.PropertyItems.Count() - 1];
string newFilename = "newfilename.jpg";
file.Save(newFilename, ImageFormat.Jpeg);
}
}
protected void EditTitleBtn_Click(object sender,EventArgs e)
{
image = System.Drawing.Image.FromFile(filepath);
PropertyItem propItem = image.PropertyItems[0];
using (var file = System.Drawing.Image.FromFile(filepath))
{
propItem.Id = 0x0320; //Title
propItem.Type = 2;
propItem.Value = System.Text.Encoding.UTF8.GetBytes("testtitle");
propItem.Len = propItem.Value.Length;
file.SetPropertyItem(propItem);
PropertyItem propitem1 = file.PropertyItems[file.PropertyItems.Count() - 1];
string newFilename = "newfilename.jpg";
if (File.Exists(filepath))
{
image.Dispose();
File.Delete(filepath);
}
file.Save(newFilename, ImageFormat.Jpeg);
}
}

并且在保存文件时显示错误,因为我想替换我编辑的 jpeg,我尝试使用 File.Exitits 和 File.Delete,但它对我不起作用,这是它显示的错误

进程无法访问文件"文件路径",因为它正在 由其他进程使用

即使我事先处理了图像,但我可以删除其他文件,只是不是我用来在if块中检查条件的文件。


所以现在我现在遇到的
问题是1.无法更改 Jpeg 图像标题的 Exif 元数据(已解决)
2.无法用编辑后的 jpeg 文件替换原始 jpeg 文件

好吧,显然我找到了它对我不起作用的原因,这是因为我对标题使用了错误的 ID,因此无论我尝试多少次我都无法更改标题。

标题的正确 ID 是propItem.Id = 0x010E;其余代码与 EditComment 事件几乎相同,因为它们使用相同的数据类型。

最新更新