随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


TWICImage 的大多功能都非常容易使用, 它最强大的功能是 ImagingFactory: IWICImagingFactory 属性, 这是个类工厂的接口.

所谓类工厂是 COM 里的概念, COM 通过类工厂可以自己实例化一些类, 实例化后我们只管使用就是.

通过 TWICImage.ImagingFactory 获取类工厂后, 再调用 CreateBitmapFlipRotator 建立旋转变换接口(IWICBitmapFlipRotator).

然后用 IWICBitmapFlipRotator.Initialize 执行变换, 再把变换后的数据赋给 TWICImage, 至此完成变换.

//可以使用的变换方式有:
WICBitmapTransformRotate0             = 0;
WICBitmapTransformRotate90            = $1;
WICBitmapTransformRotate180           = $2;
WICBitmapTransformRotate270           = $3;
WICBitmapTransformFlipHorizontal      = $8;
WICBitmapTransformFlipVertical        = $10;


测试代码:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Wincodec;

procedure TForm1.Button1Click(Sender: TObject);
var
  WicImg: TWICImage;
  Factory: IWICImagingFactory;        {类工厂接口}
  FlipRotator: IWICBitmapFlipRotator; {旋转变换接口}
begin
  {先建立 TWICImage 类并载入图片}
  WicImg := TWICImage.Create;
  WicImg.LoadFromFile('c:\temp\test.jpg');

  {获取类工厂接口}
  Factory := WicImg.ImagingFactory;

  {让类工厂建立旋转变换接口}
  Factory.CreateBitmapFlipRotator(FlipRotator);

  {执行旋转变换, 这里是旋转 90度角; 注意旋转后的数据在 FlipRotator}
  FlipRotator.Initialize(WicImg.Handle, WICBitmapTransformRotate90);

  {更新 TWICImage 中的图像}
  WicImg.Handle := IWicBitmap(FlipRotator);

  {看看}
  Canvas.Draw(0, 0, WicImg);

  {释放}
  FlipRotator := nil;
  Factory := nil;
  WicImg.Free;
end;

//还是上面的程序, 给了些返回值的判断
procedure TForm1.Button2Click(Sender: TObject);
var
  WicImg: TWICImage;
  Factory: IWICImagingFactory;
  FlipRotator: IWICBitmapFlipRotator;
  hr: HRESULT;
begin
  WicImg := TWICImage.Create;
  WicImg.LoadFromFile('c:\temp\test.jpg');

  Factory := WicImg.ImagingFactory;

  hr := E_INVALIDARG; {先给个无效的返回值}
  hr := Factory.CreateBitmapFlipRotator(FlipRotator);

  if Succeeded(hr) then {如果上一个函数的返回值成功}
    hr := FlipRotator.Initialize(WicImg.Handle, WICBitmapTransformFlipVertical);

  if Succeeded(hr) then
    WicImg.Handle := IWicBitmap(FlipRotator);

  Canvas.Draw(0, 0, WicImg);

  FlipRotator := nil;
  Factory := nil;
  WicImg.Free;
end;

end.

posted on 2009-10-20 13:35  万一  阅读(...)  评论(...编辑  收藏