  #case capital

  'AD HOC DEFINITIONS
  '==================

  #define WINGDIPAPI
  #define const
  #define GDIPCONST
  #define __out
  #define __in
  #define __inout
  '
  typedef bool     BOOL
  typedef byte     BYTE
  typedef word     WORD
  typedef sys      DebugEventProc,HBITMAP,HPALETTE
  typedef void     GpGraphics, GpImage,IStream,GpBitmap,*LPVOID
  typedef single   REAL
  typedef sys      ULONG_PTR, HDC, GpStatus, ImageType
  typedef long     LONG,INT,UINT
  typedef dword    DWORD,UINT,UINT32,ARGB
  typedef wchar    WCHAR


  '================
  'GDIPLUS MINI API
  '================

  'Pixel Formats
  'http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx

  typedef INT PixelFormat;

  #define    PixelFormatIndexed      0x00010000 // Indexes into a palette
  #define    PixelFormatGDI          0x00020000 // Is a GDI-supported format
  #define    PixelFormatAlpha        0x00040000 // Has an alpha component
  #define    PixelFormatPAlpha       0x00080000 // Pre-multiplied alpha
  #define    PixelFormatExtended     0x00100000 // Extended color 16 bits/channel
  #define    PixelFormatCanonical    0x00200000 

  #define    PixelFormatUndefined       0
  #define    PixelFormatDontCare        0

  #define    PixelFormat1bppIndexed     (1 | ( 1 << 8) | PixelFormatIndexed | PixelFormatGDI)
  #define    PixelFormat4bppIndexed     (2 | ( 4 << 8) | PixelFormatIndexed | PixelFormatGDI)
  #define    PixelFormat8bppIndexed     (3 | ( 8 << 8) | PixelFormatIndexed | PixelFormatGDI)
  #define    PixelFormat16bppGrayScale  (4 | (16 << 8) | PixelFormatExtended)
  #define    PixelFormat16bppRGB555     (5 | (16 << 8) | PixelFormatGDI)
  #define    PixelFormat16bppRGB565     (6 | (16 << 8) | PixelFormatGDI)
  #define    PixelFormat16bppARGB1555   (7 | (16 << 8) | PixelFormatAlpha | PixelFormatGDI)
  #define    PixelFormat24bppRGB        (8 | (24 << 8) | PixelFormatGDI)
  #define    PixelFormat32bppRGB        (9 | (32 << 8) | PixelFormatGDI)
  #define    PixelFormat32bppARGB       (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical)
  #define    PixelFormat32bppPARGB      (11 | (32 << 8) | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI)
  #define    PixelFormat48bppRGB        (12 | (48 << 8) | PixelFormatExtended)
  #define    PixelFormat64bppARGB       (13 | (64 << 8) | PixelFormatAlpha  | PixelFormatCanonical | PixelFormatExtended)
  #define    PixelFormat64bppPARGB      (14 | (64 << 8) | PixelFormatAlpha  | PixelFormatPAlpha | PixelFormatExtended)
  #define    PixelFormat32bppCMYK       (15 | (32 << 8))
  #define    PixelFormatMax             16




  typedef struct tagGDIStartup {
    UINT32         GdiplusVersion;
    DebugEventProc DebugEventCallback;
    BOOL           SuppressBackgroundThread;
    BOOL           SuppressExternalCodecs;
  } GdiplusStartupInput;


  typedef void *NotificationHookProc,*NotificationUnhookProc
  'typedef sys NotificationHookProc,NotificationUnhookProc

  typedef struct {
    NotificationHookProc   NotificationHook;
    NotificationUnhookProc NotificationUnhook;
  } GdiplusStartupOutput;


  'Status
  '======

  typedef enum  {
  Ok                          = 0,
  GenericError                = 1,
  InvalidParameter            = 2,
  OutOfMemory                 = 3,
  ObjectBusy                  = 4,
  InsufficientBuffer          = 5,
  NotImplemented              = 6,
  Win32Error                  = 7,
  WrongState                  = 8,
  Aborted                     = 9,
  FileNotFound                = 10,
  ValueOverflow               = 11,
  AccessDenied                = 12,
  UnknownImageFormat          = 13,
  FontFamilyNotFound          = 14,
  FontStyleNotFound           = 15,
  NotTrueTypeFont             = 16,
  UnsupportedGdiplusVersion   = 17,
  GdiplusNotInitialized       = 18,
  PropertyNotFound            = 19,
  PropertyNotSupported        = 20,
  ProfileNotFound             = 21 
  } Status;


  enum ImageFlags
  {
    ImageFlagsNone                = 0,

    // Low-word: shared with SINKFLAG_x

    ImageFlagsScalable            = 0x0001,
    ImageFlagsHasAlpha            = 0x0002,
    ImageFlagsHasTranslucent      = 0x0004,
    ImageFlagsPartiallyScalable   = 0x0008,

    // Low-word: color space definition

    ImageFlagsColorSpaceRGB       = 0x0010,
    ImageFlagsColorSpaceCMYK      = 0x0020,
    ImageFlagsColorSpaceGRAY      = 0x0040,
    ImageFlagsColorSpaceYCBCR     = 0x0080,
    ImageFlagsColorSpaceYCCK      = 0x0100,
 
    // Low-word: image size info

    ImageFlagsHasRealDPI          = 0x1000,
    ImageFlagsHasRealPixelSize    = 0x2000,

    // High-word

    ImageFlagsReadOnly            = 0x00010000,
    ImageFlagsCaching             = 0x00020000
  };



  enum RotateFlipType
  {
    RotateNoneFlipNone = 0,
    Rotate90FlipNone   = 1,
    Rotate180FlipNone  = 2,
    Rotate270FlipNone  = 3,

    RotateNoneFlipX    = 4,
    Rotate90FlipX      = 5,
    Rotate180FlipX     = 6,
    Rotate270FlipX     = 7,

    RotateNoneFlipY    = Rotate180FlipX,
    Rotate90FlipY      = Rotate270FlipX,
    Rotate180FlipY     = RotateNoneFlipX,
    Rotate270FlipY     = Rotate90FlipX,

    RotateNoneFlipXY   = Rotate180FlipNone,
    Rotate90FlipXY     = Rotate270FlipNone,
    Rotate180FlipXY    = RotateNoneFlipNone,
    Rotate270FlipXY    = Rotate90FlipNone
  };


  //---------------------------------------------------------------------------
  // Image encoder parameter related types
  //---------------------------------------------------------------------------

  enum EncoderParameterValueType
  {
    EncoderParameterValueTypeByte           = 1,    // 8-bit unsigned int
    EncoderParameterValueTypeASCII          = 2,    // 8-bit byte containing one 7-bit ASCII
                                                    // code. NULL terminated.
    EncoderParameterValueTypeShort          = 3,    // 16-bit unsigned int
    EncoderParameterValueTypeLong           = 4,    // 32-bit unsigned int
    EncoderParameterValueTypeRational       = 5,    // Two Longs. The first Long is the
                                                    // numerator, the second Long expresses the
                                                    // denomintor.
    EncoderParameterValueTypeLongRange      = 6,    // Two longs which specify a range of
                                                    // integer values. The first Long specifies
                                                    // the lower end and the second one
                                                    // specifies the higher end. All values
                                                    // are inclusive at both ends
    EncoderParameterValueTypeUndefined      = 7,    // 8-bit byte that can take any value
                                                    // depending on field definition
    EncoderParameterValueTypeRationalRange  = 8,    // Two Rationals. The first Rational
                                                    // specifies the lower end and the second
                                                    // specifies the higher end. All values
                                                    // are inclusive at both ends
  '#if (GDIPVER >= 0x0110)
      EncoderParameterValueTypePointer        = 9     // a pointer to a parameter defined data.
  '#endif //(GDIPVER >= 0x0110)
  };


  #define GDIPCONST

  typedef struct {
  dword d1
  word  w1,w2
  byte  b1,b2,b3,b4,b5,b6,b7,b8
  } GUID,CLSID,*refiid
  '

  #define DEFINE_GUID(gu,d1,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) GUID gu<=(d1,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8)



  '======================
  'Encoder parameter sets
  '======================

  DEFINE_GUID(EncoderCompression, 0xe09d739d,0xccd4,0x44ee,0x8e,0xba,0x3f,0xbf,0x8b,0xe4,0xfc,0x58);
  DEFINE_GUID(EncoderColorDepth, 0x66087055,0xad66,0x4c7c,0x9a,0x18,0x38,0xa2,0x31,0x0b,0x83,0x37);
  DEFINE_GUID(EncoderScanMethod, 0x3a4e2661,0x3109,0x4e56,0x85,0x36,0x42,0xc1,0x56,0xe7,0xdc,0xfa);
  DEFINE_GUID(EncoderVersion, 0x24d18c76,0x814a,0x41a4,0xbf,0x53,0x1c,0x21,0x9c,0xcc,0xf7,0x97);
  DEFINE_GUID(EncoderRenderMethod, 0x6d42c53a,0x229a,0x4825,0x8b,0xb7,0x5c,0x99,0xe2,0xb9,0xa8,0xb8);
  DEFINE_GUID(EncoderQuality, 0x1d5be4b5,0xfa4a,0x452d,0x9c,0xdd,0x5d,0xb3,0x51,0x05,0xe7,0xeb);
  DEFINE_GUID(EncoderTransformation,0x8d0eb2d1,0xa58e,0x4ea8,0xaa,0x14,0x10,0x80,0x74,0xb7,0xb6,0xf9);
  DEFINE_GUID(EncoderLuminanceTable,0xedb33bce,0x0266,0x4a77,0xb9,0x04,0x27,0x21,0x60,0x99,0xe7,0x17);
  DEFINE_GUID(EncoderChrominanceTable,0xf2e455dc,0x09b3,0x4316,0x82,0x60,0x67,0x6a,0xda,0x32,0x48,0x1c);
  DEFINE_GUID(EncoderSaveFlag,0x292266fc,0xac40,0x47bf,0x8c, 0xfc, 0xa8, 0x5b, 0x89, 0xa6, 0x55, 0xde);


  type EncoderParameter {
  GUID guid
  long NumberOfValues
  long dwType
  long value 'pointer to value block
  }


  type EncoderParameters {
  UINT Count;
  EncoderParameter Parameter[16]; 'pointers to EncoderParameter / arbitrary max count of 16
  };


  '=============================
  'SELECECTED GDIPLUS PROCEDURES
  '=============================

  extern library "gdiplus.dll"

  GpStatus WINGDIPAPI
  GdiplusStartup( ULONG_PTR *token, GdiplusStartupInput *input,  GdiplusStartupOutput *output);
  void GdiplusShutdown(ULONG_PTR token);

  GpStatus WINGDIPAPI
  GdipCreateFromHDC(HDC hdc, GpGraphics **graphics);

  GpStatus WINGDIPAPI
  GdipCreateBitmapFromStream(IStream* stream, GpBitmap **bitmap);

  GpStatus WINGDIPAPI 
  GdipCreateBitmapFromScan0(int width, height, stride, PixelFormat format, BYTE* scan0, GpBitmap** bitmap)

  GpStatus WINGDIPAPI
  GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename, GpBitmap **bitmap);

  GpStatus WINGDIPAPI
  GdipCreateBitmapFromStreamICM(IStream* stream, GpBitmap **bitmap);

  GpStatus WINGDIPAPI
  GdipCreateStreamOnFile(GDIPCONST WCHAR * filename, UINT access, 
                         IStream **stream);

  GpStatus WINGDIPAPI
  GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
  PixelFormat format, GpBitmap *srcBitmap, GpBitmap **dstBitmap)
  'creates a new Bitmap object by copying a portion of this bitmap.

  GpStatus WINGDIPAPI 
  GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
  PixelFormat format, GpBitmap *srcBitmap, GpBitmap **dstBitmap)
  'creates a new Bitmap object by copying a portion of this bitmap.

  GpStatus WINGDIPAPI
  GdipGetImageType(GpImage *image, ImageType *type);

  GpStatus WINGDIPAPI
  GdipGetImageWidth(GpImage *image, UINT *width);

  GpStatus WINGDIPAPI
  GdipGetImageHeight(GpImage *image, UINT *height);


  typedef sys GetThumbnailImageAbort
  '
  GpStatus WINGDIPAPI
  GdipGetImageThumbnail(GpImage *image, UINT thumbWidth, UINT thumbHeight,
                        GpImage **thumbImage,
                        GetThumbnailImageAbort callback, VOID * callbackData);

  GpStatus WINGDIPAPI
  GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y);

  GpStatus WINGDIPAPI
  GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x, INT y);

  GpStatus WINGDIPAPI
  GdipDrawImageRect(GpGraphics *graphics, GpImage *image, REAL x, REAL y, REAL width, REAL height);

  GpStatus WINGDIPAPI
  GdipDrawImageRectI(GpGraphics *graphics, GpImage *image, INT x, INT y, INT width, INT height);

  GpStatus WINGDIPAPI
  GdipImageRotateFlip(GpImage *image, RotateFlipType rfType);


  GpStatus WINGDIPAPI
  GdipDisposeImage(GpImage *image);

  GpStatus WINGDIPAPI
  GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
                      GDIPCONST CLSID* clsidEncoder, 
                      GDIPCONST EncoderParameters* encoderParams);

  GpStatus WINGDIPAPI
  GdipSaveImageToStream(GpImage *image, IStream* stream,
                        GDIPCONST CLSID* clsidEncoder, 
                        GDIPCONST EncoderParameters* encoderParams);

  GpStatus WINGDIPAPI
  GdipDeleteGraphics(GpGraphics *graphics);

  GpStatus WINGDIPAPI
  GdipGetDC(GpGraphics* graphics, HDC * hdc);

  GpStatus WINGDIPAPI
  GdipReleaseDC(GpGraphics* graphics, HDC hdc);

  GpStatus WINGDIPAPI
  GdipLoadImageFromFile(GDIPCONST WCHAR* filename, GpImage **image);

  GpStatus WINGDIPAPI
  GdipBitmapGetPixel( GpBitmap* bitmap, INT x, INT y, ARGB *color );

  GpStatus WINGDIPAPI
  GdipBitmapSetPixel( GpBitmap* bitmap, INT x, INT y, ARGB color );

  GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)

  GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap, HBITMAP* hbmReturn, ARGB background);

  '========================
  'ImageCodecInfo structure
  '========================

  'itr const prefix

  type ImageCodecInfo
    CLSID Clsid;
    GUID  FormatID;
    WCHAR* CodecName;
    WCHAR* DllName;
    WCHAR* FormatDescription;
    WCHAR* FilenameExtension;
    WCHAR* MimeType;
    DWORD Flags;
    DWORD Version;
    DWORD SigCount;
    DWORD SigSize;
    BYTE* SigPattern;
    BYTE* SigMask;
  end type
  '#recordof ImageCodecInfo



  GpStatus WINGDIPAPI
  GdipGetImageEncodersSize(UINT *numEncoders, UINT *size);

  GpStatus WINGDIPAPI
  GdipGetImageEncoders(UINT numEncoders,UINT size, ImageCodecInfo*Encoders);


typedef struct tagBITMAP {
  LONG   bmType;
  LONG   bmWidth;
  LONG   bmHeight;
  LONG   bmWidthBytes;
  WORD   bmPlanes;
  WORD   bmBitsPixel;
  LPVOID bmBits;

} BITMAP, *PBITMAP;

  long GdipCreateBitmapFromGraphics(sys Width, Height, Graphics, *pBitmap) 
  long GdipGetImageGraphicsContext(BITMAP *Bitmap, sys *Hdc)
  long GdipGetDC(sys Graphics, *Hdc)
  long GdipReleaseDC(sys Graphics, Hdc)


  end extern 'gdiplus


  function GDIplus(int mode) as sys
  =================================
  static sys hr,token
  select mode
  case 0 : if token then GdiplusShutdown token : token=0
  case 2 : 'return token only
  case 1 :
  GdiplusStartupInput StartupInput
  StartupInput.GdiplusVersion = 1
  hr=GdiplusStartup Token, StartupInput, null
  if hr then print "Error initializing GDIplus: " hex hr
  end select
  return token
  end function


  function GetEncoderClsid(string MimeType,GUID*clsid) as int
  '==========================================================
  '
  'MIME TYPES
  'image/bmp'  'image/jpeg'  'image/gif'  'image/tiff'  'image/png'
  '
  sys             a,i,hr,numencoders,nsize
  string          ici
  ImageCodecInfo* pImageCodecInfo
  '
  hr = GdipGetImageEncodersSize numEncoders, nSize
  string buffer=nuls nSize
  @pImageCodecInfo = strptr buffer
  '
  hr=GdipGetImageEncoders numEncoders, nSize, pImageCodecInfo
 '
 for i = 1 to numEncoders
    ici=pImageCodecInfo.MimeType 'autoconvert to ascii string
    a=instr 1,ici,MimeType
    if a then
      function=1 'success
      copy @clsid, @pImageCodecInfo.Clsid, sizeof GUID
      exit for
    end if
    @pImageCodecInfo+=sizeof ImageCodecInfo 'next info record
  next
  '
  end function

  sub SaveImage(string fname,typimg, int quality, LPVOID pBitMap)
  '==============================================================
  int hstatus
  EncoderParameters eps
  eps.Count=1
  '
  EncoderParameter*ep
  @ep=@eps.parameter
  ep.NumberOfValues=1
  copy @ep.guid, @EncoderQuality,sizeof GUID
  ep.dwtype=EncoderParameterValueTypeLong
  ep.Value=@quality
  GUID CodecId
  GetEncoderClsid typimg, CodecId
  '
  'fname autoconvert
  hstatus=GdipSaveImageToFile pBitMap, fname, codecId, eps
  '
  'print guidtxt codecId
  end sub  


  function SaveClipBoardImage(sys hwnd=0, string fname="tim50.jpg", typimg="image/jpeg", int quality=50) as int
  '============================================================================================================
  '
  'typimg:    image/jpeg image/png image/gif image/tiff image/bmp
  'quality:   1..100 percent
  '
  sys a,b,e,p
  OpenClipBoard hwnd
  a=GetClipboardData CF_BITMAP
  b=GetClipboardData CF_PALETTE
  e=GdipCreateBitmapFromHBITMAP a,b,p
  if p then
    SaveImage fname,typimg,quality,p
    GdipDisposeImage p
    function=1
  end if
  CloseClipBoard hwnd
  end function


  function SaveClientImage(sys hwnd,string fname="tim50.jpg", typimg="image/jpeg", int quality=50) as int
  '======================================================================================================
  '
  'typimg:    image/jpeg image/png image/gif image/tiff image/bmp
  'quality:   1..100 percent
  '
  sys hstatus,hdcs,hdcm,width,height,hbitmap,mode
  LPVOID pBitmap
  RECT rect
  'hdcs = GetWindowDC hWnd
  'GetWindowRect hwnd, @rect
  hdcs=GetDC hwnd
  GetClientRect hwnd, @rect
  width   = rect.right - rect.left
  height  = rect.bottom - rect.top
  hdcm    = CreateCompatibleDC hdcs
  hBitmap = CreateCompatibleBitmap hdcs, width, height
  Selectobject hdcm,hbitmap
  % CAPTUREBLT 0x40000000 ' Include layered windows
  % SRCCOPY    0x00CC0020 ' dest = source
  mode = SRCCOPY or CAPTUREBLT
  BitBlt hdcm, 0, 0, width, height, hdcs, 0, 0, mode
  hstatus=GdipCreateBitmapFromHBITMAP hBitmap,0,pBitMap
  DeleteObject hbitmap
  DeleteDC  hdcm
  ReleaseDC hwnd,hdcs
  if pBitMap then
    SaveImage fname,typimg,quality,pBitMap
    GdipDisposeImage pBitMap
    function=1
  end if
  end function


  function TakePixelPhoto(sys hwnd,string fname,typimg, int quality, tr, any*pix, int wi, ht) as int
  '=================================================================================================
  '
  'typimg: image/jpeg image/png image/gif image/tiff image/bmp
  'quality: 1..100
  '
  int hstatus
  sys pBitmap
  int pf
  int width   = wi
  int height  = ht
  % PixelFormat24bppRGB  137224
  % PixelFormat32bppARGB 2498570
  'http://forums.purebasic.com/english/viewtopic.php?p=205981
  if tr
    pf=PixelFormat32bppARGB
  else
    pf=PixelFormat24bppRGB
  end if
  sys a=@GdipCreateBitmapFromScan0
  call a wi,ht,0,pf,0,@pBitmap
  sys b,c,p,q,x,y
  q=@pix
  if tr
    b=*q and 0xffffff 'assume uniform background
  end if
  for y=0 to <ht
    for x=0 to <wi
      c=*q
      if tr
        if b=(c and 0xffffff)
          c and=0xffffff 'make pixel transparent
        end if
      end if
      'RED BLUE SWAP
      addr ecx,c
      mov al,[rcx]
      mov ah,[rcx+2]
      mov [rcx],ah
      mov [rcx+2],al
      '
      GdipBitmapSetPixel pBitMap,x,ht-y-1,c 'REVERSE Y
      q+=4
    next   
  next
  if pBitMap then
    SaveImage fname,typimg,quality,pBitMap
    GdipDisposeImage pBitMap
    function=1
  end if
  end function


  function TakeSnapShot(sys hwnd,string fname="tim50.jpg", typimg="image/jpeg", int quality=50, tr=0, gl=0, wa=0) as int
  '=====================================================================================================================
  '
  'itr problem with resize
  '
  'typimg:    image/jpeg image/png image/gif image/tiff image/bmp
  'quality:   1..100 percent
  'tr         tansparent backround (not jpeg)
  'gl         use opengl to read pixels from client area
  'wa         window area (0 client rect  1 window rect)
  '
  int hstatus,width,height,a
  sys pBitmap,hDC
  RECT r
  if wa=1 then 
    GetWindowRect hwnd, @r
    a=0
  else
    GetClientRect hwnd, @r
    a=hWnd
  end if
  hDC=GetDC a
  width   = r.right - r.left
  height  = r.bottom - r.top
  'http://www.jose.it-berater.org/gdiplus/reference/constants/gdipimagepixelformatconstants.htm
  % PixelFormat24bppRGB  137224
  % PixelFormat32bppARGB 2498570
  'http://forums.purebasic.com/english/viewtopic.php?p=205981
  'GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(int width, height, stride, pixelformat, BYTE* scan0, GpBitmap** bitmap)
  sys b,c,e,p,q,x,y
  #define GL_BGRA 0x80E1
  p=getmemory width*height*4
  if tr
    if gl
      #ifdef glReadPixels
        glPixelStorei(GL_PACK_ALIGNMENT, 1)
        glReadPixels 0,0,1,1,GL_BGRA,GL_UNSIGNED_BYTE,p
        b=*p and 0xffffff 'assume uniform background
      #endif
    else
      b=0xffffff and GetPixel hDC,1,1
    end if
  end if
  if gl
    #ifdef glReadPixels
      glPixelStorei(GL_PACK_ALIGNMENT, 1)
      glReadPixels 0,r.top,width,height,GL_BGRA,GL_UNSIGNED_BYTE,p
    #endif
  end if
  int pf
  pf=PixelFormat32bppARGB
  if tr
    q=0
  else
    q=p
  endif
  GdipCreateBitmapFromScan0(width,height,width*4,pf,byval q,pBitmap)
  if tr=0
    GdipImageRotateFlip(pBitmap, 6) 'Y flip
    goto done
  endif
  '
  for y=r.top to  < r.bottom
    q=p+y*width*4
    for x=r.left to < r.right
      if gl
        c=*q
      else
        c=GetPixel hDC,x,y
      end if
      if tr
        if b=(c and 0xffffff)
          c and =0xffffff 'make pixel transparent
        end if
      end if
      if gl
         GdipBitmapSetPixel pBitMap,x,height-y-1,c 'REVERSE Y
        q+=4
      else
        GdipBitmapSetPixel pBitMap,x,y,c
      end if
    next   
  next
  '
  done:
  '
  if pBitMap then
    hstatus=SaveImage fname,typimg,quality,pBitMap
    GdipDisposeImage pBitMap
    function=1
  end if
  ReleaseDC hDC
  freememory p
  end function



  type Pixel4
    dword color
    =
    byte r,g,b,a
  end type




  sub LoadPixImage(string name, sys size, string*pixels, optional int *owi,*oht, *pflip)
  '=====================================================================================
  '
  typedef void *pvoid
  '
  int     hstatus,wi,ht,flip
  pvoid   pImage
  wstring wName
  '
  wName = name 'widestring
  hStatus = GdipLoadImageFromFile wName, pImage
  hStatus=GdipGetImageWidth  pImage,wi
  hStatus=GdipGetImageHeight pImage,ht
  if @owi then owi=wi
  if @oht then oht=ht
  '
  sys pb,w
  int x,y,xe,ye
  '
  pb=pimage
  '
  /*
  enum RotateFlipType
  {
    RotateNoneFlipNone = 0,
    Rotate90FlipNone   = 1,
    Rotate180FlipNone  = 2,
    Rotate270FlipNone  = 3,

    RotateNoneFlipX    = 4,
    Rotate90FlipX      = 5,
    Rotate180FlipX     = 6,
    Rotate270FlipX     = 7,

    RotateNoneFlipY    = Rotate180FlipX,
    Rotate90FlipY      = Rotate270FlipX,
    Rotate180FlipY     = RotateNoneFlipX,
    Rotate270FlipY     = Rotate90FlipX,

    RotateNoneFlipXY   = Rotate180FlipNone,
    Rotate90FlipXY     = Rotate270FlipNone,
    Rotate180FlipXY    = RotateNoneFlipNone,
    Rotate270FlipXY    = Rotate90FlipNone
  };
  */
  if @pflip then flip=pflip else flip=6
  hStatus = GdipImageRotateFlip pb, flip ' 4 flipx 6 flipy
  pixel4 pix
  if size then
    wi=size
    ht=size
  elseif wi then
  else
    wi=64 : ht=64 
  end if
  pixels=nuls 4*wi*ht
  xe=wi-1
  ye=ht-1
  w=strptr pixels
  for y=0 to ye
    for x=0 to xe
      GdipBitmapGetPixel pb, x, y, pix.color
      'swap pix.r, pix.b
      #if sizeof sys = 8
        lea rcx,pix.color
        mov rax,[rcx]
        mov [rcx+2],al
        shr rax,16
        mov [rcx],al
      #else
        lea ecx,pix.color
        mov eax,[ecx]
        mov [ecx+2],al
        shr eax,16
        mov [ecx],al
      #endif
      *w=pix.color : w+=4
      'GdipBitmapSetPixel pb, x, y, pix.color
    next
  next
  '
  'Cleanup
  '
  if pImage then GdipDisposeImage pImage
  end sub

