使用C#删除jpeg图像的EXIF数据中除两个字段外的所有字段 [英] Remove all but two fields in EXIF data of a jpeg image using C#

查看:370
本文介绍了使用C#删除jpeg图像的EXIF数据中除两个字段外的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和ImageFactory库(来自ImageProcessor.org)来极大地修改jpg图像.它可以进行拉直,裁剪,阴影细节增强等.

I am using C# and the ImageFactory library (from ImageProcessor.org) to greatly modify a jpg image. It does straightening, cropping, shadow detail enhancement, etc. .

它可以完全正常工作,并成功将新图像写入文件. 但是,此文件包含原始EXIF数据,现在大多数数据不正确或不相关.

It is fully working and successfully writes the new image to a file. But this file contains the original EXIF data, most of which is now incorrect or irrelevant.

我确实需要在EXIF数据中保留方向标记,因为正确地定向修改后的图像是必需的.我想保留DateTime.但是其他所有EXIF数据都应该消失.

I definitely need to keep the orientation flag in the EXIF data, as it is needed to correctly orient the modified image. And I want to keep the DateTime. But all the other EXIF data should go away.

我可以找到在图像元数据中添加或修改EXIF属性项的方法,但是无法删除它.

I can find ways to add or modify an EXIF propertyitem in the image metadata, but no way to remove one.

     using (ImageFactory ifact = new ImageFactory()) {
        ifact.PreserveExifData = true;
        ifact.Load(edat.ImageFilename);

        // save the image in a bitmap that will be manipulated
        //ifact.PreserveExifData = false;  // tried this but b1 still had EXIF data
        Bitmap b1 = (Bitmap)ifact.Image;

        //lots of processsing here...

        // write the image to the output file
        b1.Save(outfilename, ImageFormat.Jpeg);
      }

推荐答案

我终于弄清楚了如何删除所有不需要的EXIF标签.

I finally figured out how to remove all unwanted EXIF tags.

剩余的那些也可以被修改.

Those that remain may also be modified.

  // remove unneeded EXIF data
  using (ImageFactory ifact = new ImageFactory()) {
    ifact.PreserveExifData = true;
    ifact.Load(ImageFilename);
    // IDs to keep: model, orientation, DateTime, DateTimeOriginal
    List<int> PropIDs = new List<int>(new int[] { 272, 274, 306, 36867 });
    // get the property items from the image
    ConcurrentDictionary<int, PropertyItem> EXIF_Dict = ifact.ExifPropertyItems;
    List<int> foundList = new List<int>();
    foreach (KeyValuePair<int, PropertyItem> kvp in EXIF_Dict) foundList.Add(kvp.Key);
    // remove EXIF tags unless they are in the PropIDs list
    foreach (int id in foundList) {
      PropertyItem junk;
      if (!PropIDs.Contains(id)) {
        // the following line removes a tag
        EXIF_Dict.TryRemove(id, out junk);
      }
    }
    // change the retained tag's values here if desired
    EXIF_Dict[274].Value[0] = 1;
    // save the property items back to the image
    ifact.ExifPropertyItems = EXIF_Dict;
  }

这篇关于使用C#删除jpeg图像的EXIF数据中除两个字段外的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆