002

#include <windows.h>
#pragma comment(lib,"gdiplus.lib")
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;

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;

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

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

   GetImageEncoders(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()
{
   // Initialize GDI+.
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

   CLSID             encoderClsid;
   Status            stat;

   // Get a image file
   Image* image = new Image(L"13lhc-600.jpg");



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

//Draw someting
   Graphics graphics(image);
   Pen      pen(Color(255, 0, 0, 255));
   graphics.DrawLine(&pen, 0, 0, 50, 50);

   // Save the image.
   stat = image->Save(L"change.jpg", &encoderClsid);

   if(stat == Ok)
      wprintf(L"%s saved successfully.\n", L"change.jpg");
   else
      wprintf(L"%d  Attempt to save %s failed.\n", stat, L"change.jpg");

   delete image;
   GdiplusShutdown(gdiplusToken);
   return 0;
}

你可能感兴趣的:(J#)