NO

Author Topic: GDI+ and ERROR_SHARING_VIOLATION  (Read 3433 times)

MX

  • Guest
GDI+ and ERROR_SHARING_VIOLATION
« on: July 31, 2011, 11:45:31 AM »
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

Code: [Select]
#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;
}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: GDI+ and ERROR_SHARING_VIOLATION
« Reply #1 on: July 31, 2011, 12:51:39 PM »
Read:
http://msdn.microsoft.com/en-us/library/ms535407(v=vs.85).aspx

Quote
Remarks

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

MX

  • Guest
Re: GDI+ and ERROR_SHARING_VIOLATION
« Reply #2 on: July 31, 2011, 02:30:15 PM »
Danke Thx