Graphics 单元新增了 TWICImage 类, 该类的功能源于新增的 Wincodec.pas 单元.
WIC 支持的图像有 BMP、PNG、ICO、JPEG、GIF、TIFF、HDP(HDP 是微软随 Vista 推出的新的图像格式).
TWICImage 的大多功能都非常容易使用, 它最强大的功能是 ImagingFactory: IWICImagingFactory 属性,
这是个类工厂的接口.
通过
TWICImage.ImagingFactory 获取类工厂后, 再调用 CreateBitmapFlipRotator
建立旋转变换接口(IWICBitmapFlipRotator).
然后用 IWICBitmapFlipRotator.Initialize
执行变换, 再把变换后的数据赋给 TWICImage, 至此完成变换.
图片格式的转换:
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;
图像的旋转变换:
procedure change(kind : Integer);
{
kind的取值:
WICBitmapTransformRotate0 = 0;
WICBitmapTransformRotate90 = $1;
WICBitmapTransformRotate180 = $2;
WICBitmapTransformRotate270 = $3;
WICBitmapTransformFlipHorizontal = $8;
WICBitmapTransformFlipVertical = $10;
}
var
wicImg : TWICImage;
factor : IWICImagingFactory;
flipRotator : IWICBitmapFlipRotator;
begin
wicImg := TWICImage.Create;
wicImg.LoadFromFile('C:\Users\Nick\Desktop\1.bmp');
factor := wicImg.ImagingFactory;
factor.CreateBitmapFlipRotator(flipRotator);
flipRotator.Initialize(wicImg.Handle, kind);
wicImg.Handle := IWICBitmap(flipRotator);
Form1.Canvas.Draw(0, 0, wicImg);
flipRotator := nil;
factor := nil;
wicImg.Free;
end;
图像的比例变换:
procedure stretch(kind : Integer);
{
kind的取值:
WICBitmapInterpolationModeNearestNeighbor = 0;
WICBitmapInterpolationModeLinear = $1;
WICBitmapInterpolationModeCubic = $2;
WICBitmapInterpolationModeFant = $3;
WICBITMAPINTERPOLATIONMODE_FORCE_DWORD = $7FFFFFFF;
}
var
wicImg : TWICImage;
factor : IWICImagingFactory;
scaler : IWICBitmapScaler;
begin
wicImg := TWICImage.Create;
wicImg.LoadFromFile('C:\Users\Nick\Desktop\1.bmp');
factor := wicImg.ImagingFactory;
factor.CreateBitmapScaler(scaler);
scaler.Initialize(wicImg.Handle, wicImg.Width * 2, wicImg.Height * 2,
kind);
wicImg.Handle := IWICBitmap(scaler);
Form1.Canvas.Draw(0, 0, wicImg);
factor := nil;
scaler := nil;
wicImg.Free;
end;
图像的裁剪:
procedure TForm1.Button1Click(Sender: TObject);
var
WicImg: TWICImage;
Factory: IWICImagingFactory;
Clipper: IWICBitmapClipper; {修剪图像的接口}
r: WICRect;
begin
WicImg := TWICImage.Create;
WicImg.LoadFromFile('c:\temp\test.jpg');
Factory := WicImg.ImagingFactory;
Factory.CreateBitmapClipper(Clipper);
r.X := WicImg.Width div 4;
r.Y := WicImg.Height div 4;
r.Width := WicImg.Width div 2;
r.Height := WicImg.Height div 2;
Clipper.Initialize(WicImg.Handle, r);
WicImg.Handle := IWICBitmap(Clipper);
Canvas.Draw(0, 0, WicImg);
Clipper := nil;
Factory := nil;
WicImg.Free;
end;