tags:

views:

228

answers:

0

I have a C++/CLI metadata editor application which edits the exif data like GPS, Datatime etc. The problem is that when I try to delete any one detail the whole metadata is lost the code which I am using for metadata editor is appended below in the code snippet. I have checked the code but was not able to find out the error can some one help me out in solving the above problem.

String ^jpegPath = FileMyPictures;
   String ^jpegDirectory = System::IO::Path::GetDirectoryName(FileMyPictures);
   String ^jpegFileName =  System::IO::Path::GetFileNameWithoutExtension(FileMyPictures);
   String ^jpegExtension = ".jpg";

   System::Windows::Media::Imaging::BitmapDecoder ^decoder=nullptr;
   System::Windows::Media::Imaging::BitmapFrame ^bitmapFrame=nullptr;
   System::Windows::Media::Imaging::BitmapMetadata ^metadata=nullptr;
   if (System::IO::File::Exists(FileMyPictures))
   {
    //  
    // load the jpg file with a JpegBitmapDecoder  
    // 
    if (System::IO::Stream ^jpegStreamIn = File::Open(FileMyPictures, FileMode::Open, FileAccess::ReadWrite, FileShare::None))
    {
     decoder = gcnew System::Windows::Media::Imaging::JpegBitmapDecoder(jpegStreamIn, System::Windows::Media::Imaging::BitmapCreateOptions::PreservePixelFormat, System::Windows::Media::Imaging::BitmapCacheOption::OnLoad);
     jpegStreamIn->Close();
    }

    bitmapFrame = decoder->Frames[0];
    metadata = (System::Windows::Media::Imaging::BitmapMetadata^)bitmapFrame->Metadata;

    if (bitmapFrame != nullptr)
    {
     //  
     // now get an InPlaceBitmapMetadataWriter, modify the metadata and try to save  
     //  
     System::Windows::Media::Imaging::InPlaceBitmapMetadataWriter^ writer = bitmapFrame->CreateInPlaceBitmapMetadataWriter();
     writer->SetQuery("/app1/ifd/exif:{uint=306}", "\0");
     if (!writer->TrySave() == true)
     {
      //  
      // the size of the metadata has been increased and we can't save it  
      //   
      System::UInt32 padding = 2048;

      System::Windows::Media::Imaging::BitmapMetadata^ metaData = (System::Windows::Media::Imaging::BitmapMetadata^)bitmapFrame->Metadata->Clone();
      if (metaData != nullptr)
      {
       //  
       // Add padding  
       //  
       metaData->SetQuery("/app1/ifd/PaddingSchema:Padding", padding);
       //  
       // modify the metadata  
       //
       if(IfDeleteGPSdataClicked == true)
       {
        metaData->SetQuery("/app1/ifd/exif/{uint=0}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=1}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=2}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=3}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=4}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=5}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=6}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=7}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=8}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=9}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=10}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=11}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=12}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=13}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=14}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=15}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=16}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=17}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=18}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=19}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=20}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=21}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=22}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=23}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=24}", "\0");
        metaData->SetQuery("/app1/ifd/exif/{uint=34853}", "\0"); // For changing GPS Field 
       }
       if(IfDeleteDateTimeClicked ==true)
       {
        metaData->SetQuery("/app1/ifd/exif:{uint=36867}", "\0");
        metaData->SetQuery("/app1/ifd/exif:{uint=36868}", "\0");
        metaData->SetQuery("/app1/ifd/exif:{uint=37520}", "\0");
        metaData->SetQuery("/app1/ifd/exif:{uint=37521}", "\0");
        metaData->SetQuery("/app1/ifd/exif:{uint=37522}", "\0");
       }
       if(IfChangeTitleClicked == true)
       {
        if(Titles[i]->Length > 0)
        {
         metaData->SetQuery("/app1/ifd/{uint=270}", Titles[i]);  // For Changing Value of Title and Subject.
         metaData->SetQuery("/app1/ifd/exif/{uint=37510}", Titles[i]); // For changing Comment Field
         // metaData->SetQuery("/app1/ifd/exif/{uint=33432}", "http://www.pixelgarde.com/ (All Rights Reserved.) + NULL[00.H] + editor copyright + NULL[00.H]");
        }
       }
       //   
       // get an encoder to create a new jpg file with the addit'l metadata.  
       //  
       System::Windows::Media::Imaging::JpegBitmapEncoder^ encoder = gcnew System::Windows::Media::Imaging::JpegBitmapEncoder();
       encoder->Frames->Add(System::Windows::Media::Imaging::BitmapFrame::Create(bitmapFrame, bitmapFrame->Thumbnail, metaData, bitmapFrame->ColorContexts));
       // String^ FileMyPictures = System::IO::Path::Combine(jpegDirectory, "JpegTemp.jpg");
       if(System::IO::Stream^ jpegStreamOut = File::Open(FileMyPictures, FileMode::OpenOrCreate, FileAccess::ReadWrite))
       {
        encoder->Save(jpegStreamOut);
        jpegStreamOut->Close();
        this->smoothProgressBar1->val = i+1;
        this->smoothProgressBar1->Refresh();
        this->pictureBox2->Refresh();
        this->btnCancel->Refresh();
       }
       }