title: How to save an image to BMP/PNG/JPG with GDI+?
body: <p>Here I have a GUID for BMP:</p>
  
  <pre><code>const      
    GBmp: TGUID =  '{b96b3cab-0728-11d3-9d7b-0000f81ef32e}';
  </code></pre>
  
  <p>I have some initialization with GdiplusStartup(...).
  Then I load a JPG and the ShowMessage says 'Ok'</p>
  
  <pre><code>  Err :=GdipLoadImageFromFile('C:\a.jpg', GdiImage);
    ShowMessage(ShowError(TGPStatus(err)));
  </code></pre>
  
  <p>Now I try to save into BMP but I get 'FileNotFound':</p>
  
  <pre><code>  Err :=GdipSaveImageToFile(GdiImage, 'C:\b.bmp', @GBmp, nil);
    ShowMessage(ShowError(TGPStatus(err)));
  </code></pre>
  
  <p>How can I save to PNG or BMP with GDI+? I don't want to use third party libraries, just the DLL.</p>
  
  <p>Below is the full code for Delphi7+:</p>
  
  <pre><code>unit Unit1;
  
  interface
  
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;
  
  type
    TGDIStartup = packed record
      Version: Integer; // =1
      DebugEventCallback: Pointer; //For debug
      SuppressBackgroundThread: Bool;
      SuppressExternalCodecs: Bool; //Tru to use internal codecs
    end;
  
  type
    TEncoderParameter = record
      Guid : TGUID;
      NumberOfValues : ULONG;
      Type_ : ULONG;
      Value : Pointer;
    end;
    TEncoderParameters = record
      Count : UINT;
      Parameter : array[0..0] of TEncoderParameter;
    end;
    PEncoderParameters = ^TEncoderParameters;
  
  type
    TGPStatus = (
      Ok,
      GenericError,
      InvalidParameter,
      OutOfMemory,
      ObjectBusy,
      InsufficientBuffer,
      NotImplemented,
      Win32Error,
      WrongState,
      Aborted,
      FileNotFound,
      ValueOverflow,
      AccessDenied,
      UnknownImageFormat,
      FontFamilyNotFound,
      FontStyleNotFound,
      NotTrueTypeFont,
      UnsupportedGdiplusVersion,
      GdiplusNotInitialized,
      PropertyNotFound,
      PropertyNotSupported,
      ProfileNotFound
    );
  
  type
    TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
      { Private declarations }
    public
      { Public declarations }
    end;
  
    function GdipSaveImageToFile(image: Integer; filename: PWCHAR; clsidEncoder: PGUID; encoderParams: PEncoderParameters): Integer; stdcall;
    function GdipLoadImageFromFile(const Filename: PWideChar; out Image: Integer): Integer; stdcall;
    function GdiplusStartup(var Token: Longword; const Input, Output: Pointer): Integer; stdcall;
    function GdipGetImageHeight(Image: Integer; out Height: Integer): Integer; stdcall;
  
  var
    Form1: TForm1;
  
  implementation
  
  {$R *.dfm}
  
  function GdipSaveImageToFile;    external 'GdiPlus.dll'  name 'GdipSaveImageToFile';
  function GdipLoadImageFromFile;  external 'GdiPlus.dll'  name 'GdipLoadImageFromFile';
  function GdiplusStartup;         external 'GdiPlus.dll'  name 'GdiplusStartup';
  function GdipGetImageHeight;     external 'GdiPlus.dll'  name 'GdipGetImageHeight';
  
  function ShowError(Code: TGPStatus) : String;
  begin
    case Code of
      Ok                        : Result := 'Ok';
      GenericError              : Result := 'GenericError';
      InvalidParameter          : Result := 'InvalidParameter';
      OutOfMemory               : Result := 'OutOfMemory';
      ObjectBusy                : Result := 'ObjectBusy';
      InsufficientBuffer        : Result := 'InsufficientBuffer';
      NotImplemented            : Result := 'NotImplemented';
      Win32Error                : Result := 'Win32Error';
      WrongState                : Result := 'WrongState';
      Aborted                   : Result := 'Aborted';
      FileNotFound              : Result := 'FileNotFound';
      ValueOverflow             : Result := 'ValueOverflow';
      AccessDenied              : Result := 'AccessDenied';
      UnknownImageFormat        : Result := 'UnknownImageFormat';
      FontFamilyNotFound        : Result := 'FontFamilyNotFound';
      FontStyleNotFound         : Result := 'FontStyleNotFound';
      NotTrueTypeFont           : Result := 'NotTrueTypeFont';
      UnsupportedGdiplusVersion : Result := 'UnsupportedGdiplusVersion';
      GdiplusNotInitialized     : Result := 'GdiplusNotInitialized';
      PropertyNotFound          : Result := 'PropertyNotFound';
      PropertyNotSupported      : Result := 'PropertyNotSupported';
      ProfileNotFound           : Result := 'ProfileNotFound';
    else
      Result := '&lt;UnKnown&gt;';
    end;
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  const GuidBmp: TGUID = '{b96b3cab-0728-11d3-9d7b-0000f81ef32e}';
        GuidPng: TGUID = '{b96b3caf-0728-11d3-9d7b-0000f81ef32e}';
  var Err: Integer;
      Graphics: Integer;
  
      InitToken: LongWord;
      Startup: TGDIStartup;
      GdiImage: Integer;
      Hei: Integer;
  begin
    FillChar(Startup, sizeof(Startup), 0);
    Startup.Version := 1;
    GdiplusStartup(InitToken, @Startup, nil);
  
    Err := GdipLoadImageFromFile('C:\_MyDat\gdi\a.jpg', GdiImage);
    ShowMessage(ShowError(TGPStatus(Err)));
  
    GdipGetImageHeight(GdiImage, Hei);
    ShowMessage(IntToStr(Hei));
  
    Err := GdipSaveImageToFile(GdiImage, 'C:\_MyDat\gdi\b.bmp', @GuidPng, nil);
    ShowMessage(ShowError(TGPStatus(Err)));
  end;
  
  end.
  </code></pre>