【问题标题】:Value of image property (C#)图像属性的值 (C#)
【发布时间】:2013-03-06 02:46:34
【问题描述】:

我正在尝试解决更改 Bitmap 对象的 ImageDescription 值的问题。为文件添加描述。搜索相关主题,我没有找到解决方案。

我的代码:

public Bitmap ImageWithComment(Bitmap image)
{
   string filePath = @"C:\1.jpg";
   var data = Encoding.UTF8.GetBytes("my comment"); 
   var propItem = image.PropertyItems.FirstOrDefault();
   propItem.Type = 2;
   propItem.Id = 40092;
   propItem.Len = data.Length;
   propItem.Value = data;
   image.SetPropertyItem(propItem);
   image.Save(filePath);
   return image;
}

但是带有新评论的图像不会保存在文件夹中((请帮助我

【问题讨论】:

  • 图片来源是同一个文件吗?这可能是问题所在。
  • 你有什么异常吗?
  • System.ArgumentException:参数无效。在 System.Drawing.Image.get_Width() 中 System.Drawing.Bitmap .. ctor (Image image) in WallpapersSite.Code.ImageActions.SaveImage (Bitmap image)

标签: c# image bitmap


【解决方案1】:

根据MSDN - Property Tags,您必须为Id 使用正确的int 值

示例

 using (var image = new Bitmap(@"C:\Desert.jpg"))
            {
                string filePath = @"C:\Desertcopy.jpg";
                var data = Encoding.UTF8.GetBytes("my comment");
                var propItem = image.PropertyItems.FirstOrDefault();
                propItem.Type = 2;
                propItem.Id = 0x010E; // <-- Image Description
                propItem.Len = data.Length;
                propItem.Value = data;
                image.SetPropertyItem(propItem);
                image.Save(filePath);
            }

使用 MSDN 中的以下号码

运行代码后,你可以看到它对图像的影响

之前

之后

【讨论】:

  • 我需要更改文件的描述,而不是 EXIF。此属性编号 - 40092 (int)。
  • propItem.Id = 40092;
  • 我需要一个方法来接受对象类型Bitmap,改变它的属性(描述)和返回对象类型Bitmap
  • public Bitmap ImageWithComment(Bitmap image) { var data = Encoding.UTF8.GetBytes("my comment"); var propItem = image.PropertyItems.FirstOrDefault(); propItem.Type = 2;道具.Id = 40092; propItem.Len = 数据长度; propItem.Value = 数据; image.SetPropertyItem(propItem);返回图像; }
  • 这个函数必须修改必要的属性,但这并没有发生
【解决方案2】:

ID 40092 转换为 0x9C9C。根据this,这不是有效的属性项ID。 根据this

如果图像格式支持属性项但不支持 您尝试设置的特定属性,此方法会忽略 尝试但不抛出异常。

从外观上看,您希望您的 ID 为 0x010E。 另外,请参阅here 了解每个属性项 ID 的详细信息。

【讨论】:

  • 我需要一个方法来接受对象类型Bitmap,改变它的属性(描述)和返回对象类型Bitmap
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2014-06-08
  • 2012-12-31
  • 2019-05-16
  • 2015-09-19
  • 2019-03-14
  • 2014-05-25
相关资源
最近更新 更多