절름발이 프로그래머/Delphi

WIC(Windows Imaging Component)를 Delphi 2010에서 사용하기

훅크선장 2010. 9. 10. 23:37
http://www.delphipraxis.net/147190-how-work-wic.html 를 참고하였습니다.

WIC 라는 개념이 Delphi 2010부터 지원됩니다. 여러 종류의 그래픽 파일들의 자유로운 읽기, 쓰기, 변환이 지원된다고 합니다. RAW 파일까지도 자유롭게 다룰 수 있다고 합니다.

아래와 같은 소스를 만들어서 테스트해보시면 알 수 있습니다. 중요한 점은 Wincodec 을 uses 구문에 추가하는 것입니다.
-------------------------------------------------------------------------------------------------------------
unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Wincodec;

type
  TForm1 = class(TForm)
    OpenPictureDialog: TOpenDialog;
    btnOpen: TButton;
    edtIamgeFilePath: TEdit;
    Image: TImage;
    procedure btnOpenClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Function() to test if this engine working correctly
function WICImageFormatDescription(const WIC: TWICImage): string;
begin
  Result := '';

  case WIC.ImageFormat of
    wifBmp: Result := 'Bitmap';
    wifPng: Result := 'PNG';
    wifJpeg: Result := 'JPEG';
    wifGif: Result := 'GIF';
    wifTiff: Result := 'TIFF';
    wifWMPhoto: Result := 'JPEG XR';
    wifOther:
    begin
      if GUIDToString(WIC.EncoderContainerFormat) = GUIDToString(GUID_ContainerFormatIco) then
        Result := 'Icon'
      else
        Result := 'other'
      ;
    end;
  end;
end;


procedure TForm1.btnOpenClick(Sender: TObject);
var
  WIC: TWICImage;
begin
  WIC := TWICImage.Create;
  try
    if OpenPictureDialog.Execute then
    begin
      edtIamgeFilePath.Text := OpenPictureDialog.FileName;
      WIC.LoadFromFile(OpenPictureDialog.FileName);
      Image.Picture.Assign(WIC);
      ShowMessage(WICImageFormatDescription(WIC));
      WIC.ImageFormat := wifWMPhoto;
      WIC.SaveToFile('output.wdp');
    end;
  finally
    WIC.Free;
  end;
end;

end.