GDI+ and ERROR_SHARING_VIOLATION

Started by MX, July 31, 2011, 11:45:31 AM

Previous topic - Next topic

MX

Hi
I´m trying to open a jpg File, change compression value and save it.
Code works fine when using another filename.
But i want to overwrite the existing file. Last Error = 32 ERROR_SHARING_VIOLATION
Can anyone help ?
Schöne Grüße MX

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>


// Get CLSID
int GetEncoderClsid(const WCHAR * format, CLSID * pClsid)
{
   UINT num = 0;   // number of image encoders
   UINT size = 0;   // size of the image encoder array in bytes

   IMAGECODECINFO *pImageCodecInfo = NULL;

   GdipGetImageEncodersSize(&num, &size);
   if (size == 0)
      return -1;   // Failure

   pImageCodecInfo = malloc(size);
   if (pImageCodecInfo == NULL)
      return -1;   // Failure

   GdipGetImageEncoders(num, size, pImageCodecInfo);

   for (UINT j = 0; j < num; ++j)
   {
      if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;   // Success
      }
   }

   free(pImageCodecInfo);
   return -1;   // Failure
}

int main(void)
{
   ULONG_PTR gdiplusToken;
   char snom[MAX_PATH ];
   WCHAR sficnom[MAX_PATH ];
   char sdir[MAX_PATH ];
   GpStatus status;

                // INIT GDI
   GdiplusStartup(&gdiplusToken, &g_GdiplusStartupInput, NULL);

   CLSID encoderClsid;
   GetEncoderClsid(L"image/jpeg", &encoderClsid);

   ENCODERPARAMETERS encoderParameters;
   ULONG quality;
   long t,nt;

status = GdipLoadImageFromFile(L"test.jpg",(GP_GPIMAGE **)&t);
  if (status==0)
    status=GdipCloneImage((GP_GPIMAGE *)t,(GP_GPIMAGE **)&nt);
      if (status==0)
        status = GdipDisposeImage((GP_GPIMAGE *)t);
          if (status==0)
{

   encoderParameters.Count = 1;
   encoderParameters.Parameter[0].Guid = EncoderQuality;
   encoderParameters.Parameter[0].Type = eEncoderParameterValueTypeLong;
   encoderParameters.Parameter[0].NumberOfValues = 1;

   quality = 85;
   encoderParameters.Parameter[0].Value = &quality;

   status = GdipSaveImageToFile( (GP_GPIMAGE *) nt,L"test.jpg", &encoderClsid, &encoderParameters);

                // CLOSE GDI.
   GdipDisposeImage ( (GP_GPIMAGE *) nt );
   GdiplusShutdown(gdiplusToken);
}
return 0;
}

TimoVJL

Read:
http://msdn.microsoft.com/en-us/library/ms535407(v=vs.85).aspx

QuoteRemarks

GDI+ does not allow you to save an image to the same file that you used to construct the image. The following code creates an Image object by passing the file name MyImage.jpg to an Image constructor. That same file name is passed to the Image::Save method of the Image object, so the Image::Save method fails.
May the source be with you