使用System.Drawing.Image时,如何确定jpeg中标题的Id

时间:2018-01-23 01:19:43

标签: c# asp.net metadata jpeg exif

到目前为止,我创建了一个简单的上传函数,下载函数和一个更改文件属性的类,但是现在我如何确定我想要更改的属性或属性的Id。标题,作者,主题等目前我正在使用System.Drawing.Imaging。dll为我的图书馆。

这是我在ChangeFileProperties课程中取得的成就:

public class ChangeFileProperties
{ 
    public void ChangeFileAuthor(string FilePath)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath);

        //??
        PropertyItem propitem = image.GetPropertyItem(0x0320);
        //??
        propitem.Id = 0x0321;
        propitem.Len = propitem.Value.Length;
        propitem.Type = 1;
        propitem.Value = Encoding.UTF8.GetBytes("MyImageInfo");
        image.SetPropertyItem(propitem);


        image.Save(Environment.CurrentDirectory + @"\test.jpeg");
    }
}

所以现在,代码中没有错误,但是当我运行网页时,??评论部分会弹出错误,错误说

  

类型' System.ArgumentException'的例外情况发生在System.Drawing.dll中但未在用户代码中处理

     

其他信息:无法找到财产。

似乎Id是错误的,所以我怎么知道每个属性的Id是什么。

1 个答案:

答案 0 :(得分:1)

如果您使用Image.PropertyItems集合,则可以获取所有属性并循环遍历它们:

var image = System.Drawing.Image.FromFile(@"C:\Users\wired\Pictures\1496422850103.jpeg");

foreach (var prop in image.PropertyItems)
{
    var id = prop.Id;
    //etc
}