Delphi 2010 新增功能之: TWICImage 类[1] - 方便的图像格式转换


Graphics 单元新增了 TWICImage 类, 该类的功能源于新增的 Wincodec.pas 单元.

WIC(Windows Imaging Component) 是 Windows 为 Vista 和 WPF 提供的用于图像编码、解码的 COM 组件, 但在 XP 下也能使用它.

WIC 支持的图像有 BMP、PNG、ICO、JPEG、GIF、TIFF、HDP(HDP 是微软随 Vista 推出的新的图像格式).

Wincodec 单元里的那些 API 函数还是挺繁琐的, 不过 TWICImage 可以非常方便地操作这些格式的图像.

这样以后使用 jpg、gif、png 等图片时, 有 TWICImage 就够了!

本例假定有这张图片存在: c:\temp\test.jpg, 然后把它转换为其他格式.


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

var

  WicImg: TWICImage;

begin

  WicImg := TWICImage.Create;

  WicImg.LoadFromFile('c:\temp\test.jpg');



  {在窗体上预览图片}

  Canvas.Draw(0, 0, WicImg);



  {转换为 png}

  WicImg.ImageFormat := TWICImageFormat.wifPng;

  WicImg.SaveToFile('c:\temp\test.png');



  WicImg.Free;

end;



end.


 
   

你可能感兴趣的:(Delphi)