1

ここに BMP の GUID があります。

const      
  GBmp: TGUID =  '{b96b3cab-0728-11d3-9d7b-0000f81ef32e}';

GdiplusStartup(...) で初期化しています。次に、JPG をロードすると、ShowMessage に「OK」と表示されます

  Err :=GdipLoadImageFromFile('C:\a.jpg', GdiImage);
  ShowMessage(ShowError(TGPStatus(err)));

ここで BMP に保存しようとしましたが、「FileNotFound」が表示されます。

  Err :=GdipSaveImageToFile(GdiImage, 'C:\b.bmp', @GBmp, nil);
  ShowMessage(ShowError(TGPStatus(err)));

GDI+ で PNG または BMP に保存するにはどうすればよいですか? サード パーティのライブラリは使用したくありません。DLL のみを使用します。

以下は、Delphi7+ の完全なコードです。

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 := '<UnKnown>';
  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.
4

1 に答える 1