C# 在 JPG 'Property not found' 异常中设置和获取 PropertyItem
C# Setting and getting PropertyItem in JPG 'Property not found' exception
在 C# 中,我试图在 jpg 文件中设置 属性 以下。但是当我尝试取回它们时,我得到 属性 not found 异常。此外,Set属性Item 调用无法报告 success\failure,因此很难理解哪里出了问题。
0x5111
属性TagPixelPerUnitX
0x5112
属性TagPixelPerUnitY
string file = "New.jpg";
double x = 24524.5555598;
double y = 234123.4123423;
Image img = Image.FromFile(file);
PropertyItem[] props = img.PropertyItems;
PropertyItem newProp1 = props.FirstOrDefault();
newProp1.Id = 0x5111;
newProp1.Type = 1;
newProp1.Value = BitConverter.GetBytes(x);
newProp1.Len = newProp1.Value.Length;
PropertyItem newProp2 = props.FirstOrDefault();
newProp2.Id = 0x5112;
newProp2.Type = 1;
newProp2.Value = BitConverter.GetBytes(y);
newProp2.Len = newProp1.Value.Length;
img.SetPropertyItem(newProp1);
img.SetPropertyItem(newProp2);
img.Save("New1.jpg", ImageFormat.Jpeg);
以及检索它们的代码,
string file = "New1.jpg";
Image img = Image.FromFile(file);
PropertyItem prop = img.GetPropertyItem(0x5111);
上面一行抛出异常'Property not found'
我不知道他们为什么将 PropertyItem 构造函数设为内部,并且不提供任何其他创建 属性 项目的方法。但是,您可以只使用反射来解决这个奇怪的问题,它会起作用:
string file = @"New1.jpg";
double x = 24524.5555598;
double y = 234123.4123423;
var img = System.Drawing.Image.FromFile(file);
// note how to force Activator.CreateInstance to call internal constructor,
// it's important to call this overload
var newProp1 = (PropertyItem) Activator.CreateInstance(typeof(PropertyItem), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], CultureInfo.InvariantCulture);
newProp1.Id = 0x5111;
newProp1.Type = 1;
newProp1.Value = BitConverter.GetBytes(x);
newProp1.Len = newProp1.Value.Length;
var newProp2 = (PropertyItem)Activator.CreateInstance(typeof(PropertyItem), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], CultureInfo.InvariantCulture);
newProp2.Id = 0x5112;
newProp2.Type = 1;
newProp2.Value = BitConverter.GetBytes(y);
newProp2.Len = newProp1.Value.Length;
img.SetPropertyItem(newProp1);
img.SetPropertyItem(newProp2);
img.Save("New1.jpg", ImageFormat.Jpeg);
在 C# 中,我试图在 jpg 文件中设置 属性 以下。但是当我尝试取回它们时,我得到 属性 not found 异常。此外,Set属性Item 调用无法报告 success\failure,因此很难理解哪里出了问题。
0x5111 属性TagPixelPerUnitX
0x5112 属性TagPixelPerUnitY
string file = "New.jpg";
double x = 24524.5555598;
double y = 234123.4123423;
Image img = Image.FromFile(file);
PropertyItem[] props = img.PropertyItems;
PropertyItem newProp1 = props.FirstOrDefault();
newProp1.Id = 0x5111;
newProp1.Type = 1;
newProp1.Value = BitConverter.GetBytes(x);
newProp1.Len = newProp1.Value.Length;
PropertyItem newProp2 = props.FirstOrDefault();
newProp2.Id = 0x5112;
newProp2.Type = 1;
newProp2.Value = BitConverter.GetBytes(y);
newProp2.Len = newProp1.Value.Length;
img.SetPropertyItem(newProp1);
img.SetPropertyItem(newProp2);
img.Save("New1.jpg", ImageFormat.Jpeg);
以及检索它们的代码,
string file = "New1.jpg";
Image img = Image.FromFile(file);
PropertyItem prop = img.GetPropertyItem(0x5111);
上面一行抛出异常'Property not found'
我不知道他们为什么将 PropertyItem 构造函数设为内部,并且不提供任何其他创建 属性 项目的方法。但是,您可以只使用反射来解决这个奇怪的问题,它会起作用:
string file = @"New1.jpg";
double x = 24524.5555598;
double y = 234123.4123423;
var img = System.Drawing.Image.FromFile(file);
// note how to force Activator.CreateInstance to call internal constructor,
// it's important to call this overload
var newProp1 = (PropertyItem) Activator.CreateInstance(typeof(PropertyItem), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], CultureInfo.InvariantCulture);
newProp1.Id = 0x5111;
newProp1.Type = 1;
newProp1.Value = BitConverter.GetBytes(x);
newProp1.Len = newProp1.Value.Length;
var newProp2 = (PropertyItem)Activator.CreateInstance(typeof(PropertyItem), BindingFlags.Instance | BindingFlags.NonPublic, null, new object[0], CultureInfo.InvariantCulture);
newProp2.Id = 0x5112;
newProp2.Type = 1;
newProp2.Value = BitConverter.GetBytes(y);
newProp2.Len = newProp1.Value.Length;
img.SetPropertyItem(newProp1);
img.SetPropertyItem(newProp2);
img.Save("New1.jpg", ImageFormat.Jpeg);