【问题标题】:Title editing with System.Drawing.Image not working for me使用 System.Drawing.Image 编辑标题对我不起作用
【发布时间】:2018-07-05 10:20:55
【问题描述】:

我已经创建了一些函数来编辑 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\0");
            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\0");
            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.Exits 和 File.Delete 但它对我不起作用,这是错误显示

进程无法访问文件“文件路径”,因为它正在 被另一个进程使用

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


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

【问题讨论】:

    标签: c# asp.net metadata jpeg exif


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多