Remove all but two fields in EXIF data of a jpeg image using C#

纵饮孤独 提交于 2019-12-30 14:39:12

问题


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. .

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.

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.

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);
      }

回答1:


How about this:

Bitmap bmp = new Bitmap("C:\\Test\\test.jpg");

foreach (System.Drawing.Imaging.PropertyItem item in bmp.PropertyItems)
{
    if (item.Id == 0x0112 || item.Id == 0x0132)
        continue;

    System.Drawing.Imaging.PropertyItem modItem = item;
    modItem.Value = new byte[]{0};
    bmp.SetPropertyItem(modItem);
}

bmp.Save("C:\\Test\\noexif.jpg");

Here's the Id table for reference: https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id(v=vs.110).aspx

0x0112 - Orientation

0x0132 - Date/Time




回答2:


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;
  }


来源:https://stackoverflow.com/questions/50974937/remove-all-but-two-fields-in-exif-data-of-a-jpeg-image-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!