views:

19

answers:

0

How to using gdi+ to write Exif info into a exist image file in C++ windows?

I get a part of code in the following way, however I think most of the following code is not necessary:

  BOOL bRet = FALSE;
  CString cstrFormat = TransferImageFormatToString(eFormat);
  if ( !cstrFormat.IsEmpty() )
  {
   CLSID clsid;
   int fd = _open(CSTRING_TO_STDSTRING(csPath), O_RDONLY);
   if ( fd >= 0 )
   {
    DWORD dwSize = filelength(fd);
    HGLOBAL glo = GlobalAlloc(GMEM_FIXED, dwSize);

    if ( _read(fd, glo, dwSize) >= 0 )
    {
     _close(fd);

     IStream* pStream;
     if ( CreateStreamOnHGlobal(glo, TRUE, &pStream) == S_OK)
     {
      Bitmap* pBitmap = Bitmap::FromStream(pStream);

      Status sta;
      if ( pBitmap != NULL )
      {
       int nSize = pBitmap->GetPropertyItemSize(PropertyTagOrientation);

       PropertyItem* pItem = (PropertyItem*)malloc(nSize);
       if ( pItem != NULL )
       {
        pBitmap->GetPropertyItem(PropertyTagOrientation, nSize, pItem);
        if( nSize != 0 )
        {
         *((SHORT*)pItem->value) = sValue;
         HRESULT hRes = pBitmap->SetPropertyItem(pItem);
         CLSID clsid;

         GetEncoderClsid(cstrFormat, &clsid);

         bRet = (pBitmap->Save(csPath.GetBuffer(), &clsid, NULL) == Ok);

         SAFE_DELETE(pBitmap);
        }
       }
       else
       {
        bRet = TRUE;
       }

       SAFE_DELETE(pItem);
      }

      if ( pStream != NULL )
      {
       pStream->Release();
      }
     }
    }
    else
    {
     _close(fd);
    }
   }
  }
  return bRet;

Is there any simpler way to modify exif rotate informatation?

Many thanks!