locked
GDIplus image rotation x64 RRS feed

  • Question

  • The following code does not work in Visual Studio 13 C++ x64

    I converted it from 32 bit code in another language... it says it can't load the file and its all downhill from there

    Any ideas?

    std::string rotate_drawing(std::string namefile,LONG rot)
    {
         LONG i,lnf;
         std::wstring wpath=strtowstr(namefile);
         const wchar_t* wcpath=wpath.c_str();
         std::string newnamefile,imgtype,stxt;
         Gdiplus::Image* rimg;
         CLSID sencoderclsid;

         //get image from file
         rimg=Gdiplus::Image::FromFile(wcpath);

         if(rimg == 0)
         {
           showtextmessage("Could not load "+namefile);
           return "";
         }

         if(rot == 90)
           rimg->RotateFlip(Gdiplus::RotateFlipType::Rotate90FlipNone);
         else if(rot == 180)
           rimg->RotateFlip(Gdiplus::RotateFlipType::Rotate180FlipNone);
         else if(rot == 270)
           rimg->RotateFlip(Gdiplus::RotateFlipType::Rotate270FlipNone);

         //save resized image to file
         lnf=(long)namefile.length();
         i=lastinstr(namefile,".");
         imgtype=lcase(mid(namefile,(i+1),(lnf-i)));
         newnamefile=left(namefile,(i-1))+"-r."+imgtype;

         if(imgtype == "jpg")
           imgtype="jpeg";
         else if(imgtype == "tif")
           imgtype="tiff";

         stxt="image/"+imgtype;

         // Save the altered image
         if(getencoderclsid(stxt,&sencoderclsid) == FALSE)
         {
           showtextmessage("Could not identify "+namefile);
           delete rimg;  
           return "";
         }

         wpath=strtowstr(newnamefile);
         wcpath=wpath.c_str();

         rimg->Save(wcpath,&sencoderclsid,NULL);

         delete rimg;

         return newnamefile;
    }

    BOOL getencoderclsid(std::string fmt,CLSID* pClsid)
    {
         UINT num,size,x;

         std::wstring wfmt=strtowstr(fmt);
         Gdiplus::ImageCodecInfo* pImageCodecInfo=NULL;

         Gdiplus::GetImageEncodersSize(&num,&size);

         if(size == 0)
           return FALSE;  // Failure

         pImageCodecInfo=(Gdiplus::ImageCodecInfo*)(malloc(size));

         if(pImageCodecInfo == NULL)
           return FALSE;  // Failure

         GetImageEncoders(num,size,pImageCodecInfo);

         for(x=0;x<num;++x)
         {
           if(wcscmp(pImageCodecInfo[x].MimeType,wfmt.c_str()) == 0)
           {
             *pClsid = pImageCodecInfo[x].Clsid;
             free(pImageCodecInfo);
             return TRUE;  // Success
           }    
         }

         free(pImageCodecInfo);
         return FALSE;  // Failure
    }

    Wednesday, December 20, 2017 10:10 PM

Answers

  • Yes, called from another routine before this function is called

    But never mind; I found the problem; it was with the drawing file and the inability to load was legitimate...

    Thanks anyway...

    • Marked as answer by TallGuy63 Thursday, December 21, 2017 1:48 PM
    Thursday, December 21, 2017 1:47 PM

All replies

  • Hi TallGuy63,

    thanks for posting here.

    >>The following code does not work in Visual Studio 13 C++ x64

    I converted it from 32 bit code in another language... it says it can't load the file and its all downhill from there

    Do you mean the Gdiplus::Image::FromFile failed? Have you tried to debug your code to see if the wcpath value is correct?

    Please provide more information about the strtowstr method. What's the implementation of it?

    You could use MultiByteToWideChar function to convert between a character string and a UTF-16 (wide character) string. Or follow the document below to convert between various string types.

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Baron Bi Thursday, December 21, 2017 6:11 AM
    Thursday, December 21, 2017 6:10 AM
  • Yes, Gdiplus::Image::FromFile failed

    yes the wpath is correct

    std::wstring strtowstr(std::string s)
    {
         std::wstring ws(s.begin(),s.end());
         return ws;
    }

    Thursday, December 21, 2017 12:44 PM
  • Have you called GdiplusStartup according to documentation and samples?

    Thursday, December 21, 2017 12:51 PM
  • Yes, called from another routine before this function is called

    But never mind; I found the problem; it was with the drawing file and the inability to load was legitimate...

    Thanks anyway...

    • Marked as answer by TallGuy63 Thursday, December 21, 2017 1:48 PM
    Thursday, December 21, 2017 1:47 PM