使用GDI +更新c ++中的jpeg属性

时间:2013-12-05 17:26:35

标签: c++ gdi+ exif

我想在c ++中将属性从一个jpeg图像复制到另一个jpeg图像。由于我没有任何库,而且我正在使用Windows,我正在使用GDI +。 我的代码如下

 void CopyJpegProperties(string targetImageName,string sourceImageName)
    {
        CLSID  m_clsid;
        EncoderParameters m_encoderParameters;
        UINT size;
        UINT count;
        int quality=100;
        GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR m_gdiplusToken;
        // start GDI+
        GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
        {
            // we are processing images inside a block to make sure that all of objects are deleted when GDI is shutdown.

            // read source image properties
            std::wstring wcSourceImageName=StringTools::StringToWString(sourceImageName);
            Gdiplus::Bitmap sourceBitmap(wcSourceImageName.c_str());
            sourceBitmap.GetPropertySize(&size, &count);
            PropertyItem* pPropBuffer =(PropertyItem*)malloc(size);
            sourceBitmap.GetAllPropertyItems(size, count, pPropBuffer);

            // write to target image
            std::wstring wcTargetImageName=StringTools::StringToWString(targetImageName);
            Gdiplus::Bitmap targetBitmap(wcTargetImageName.c_str());
            for(int i=0; i<count; i++)
            {
                targetBitmap.SetPropertyItem(&pPropBuffer[i]);
            }
            JpegTools::GetEncoderClsid(L"image/jpeg", &m_clsid);
            m_encoderParameters.Count = 1;
            m_encoderParameters.Parameter[0].Guid = EncoderQuality;
            m_encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
            m_encoderParameters.Parameter[0].NumberOfValues = 1;
            m_encoderParameters.Parameter[0].Value = &quality;

            Status stat = targetBitmap.Save(wcTargetImageName.c_str(), &m_clsid, &m_encoderParameters);
             if(stat != Ok)
              {
                 throw exception("Error in saving");
               }
        }
        GdiplusShutdown(m_gdiplusToken);
    }

此函数失败,错误代码为7,win32error。

我认为问题在于,当我打开目标文件时,我无法写入它,因为它被open函数锁定。

我该如何解决?

有没有更好的方法在Windows中读取/写入图像属性?

0 个答案:

没有答案