Direct2D (9) : 显示图像

Direct2D (9) : 显示图像


uses Direct2D;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  wic: TWICImage;
  R: TRect;
begin
  wic := TWICImage.Create;
  wic.LoadFromFile('C:\Temp\Test.png');

  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  cvs.BeginDraw;

  cvs.Draw(10, 10, wic);

  R := Bounds(wic.Width div 2 + 10, wic.Height div 2 + 10, wic.Width div 2, wic.Height div 2);
  cvs.StretchDraw(R, wic, 192);

  cvs.EndDraw;
  cvs.Free;
  wic.Free;
end;


效果图:

Direct2D (9) : 显示图像

用较原始的办法重做上面的例子(通过 wic):

uses Direct2D, D2D1, Wincodec, ActiveX;

{从指定文件建立 ID2D1Bitmap 对象的函数}
function GetD2D1Bitmap(RenderTarget: ID2D1RenderTarget; imgPath: string): ID2D1Bitmap;
var
  iWicFactory: IWICImagingFactory;
  iWICDecoder: IWICBitmapDecoder;
  iWICFrameDecode: IWICBitmapFrameDecode;
  iFormatConverter: IWICFormatConverter;
begin
  {获取建立 WIC 的工厂}
  CoCreateInstance(CLSID_WICImagingFactory, nil, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, iWicFactory);
  {打开并获取解码后的对象}
  iWicFactory.CreateDecoderFromFilename(PWideChar(imgPath), GUID_NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, iWICDecoder);
  {获取第一帧}
  iWicDecoder.GetFrame(0, iWICFrameDecode);
  {获取格式转换器}
  iWicFactory.CreateFormatConverter(iFormatConverter);
  {转换到与 D2D 兼容的格式}
  iFormatConverter.Initialize(iWICFrameDecode, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nil, 0, WICBitmapPaletteTypeMedianCut);
  {获取 ID2D1Bitmap}
  RenderTarget.CreateBitmapFromWicBitmap(iFormatConverter, nil, Result);
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  iBitmap: ID2D1Bitmap;
  fRect: TD2DRectF;
  fSize: TD2DSizeF;
begin
  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  iBitmap := GetD2D1Bitmap(cvs.RenderTarget, 'C:\Temp\Test.png');

  iBitmap.GetSize(fSize);
  fRect := D2D1RectF(0, 0, fSize.width, fSize.height);
  cvs.BeginDraw;
  cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(10, 10));
  cvs.RenderTarget.DrawBitmap(iBitmap, @fRect);

  fRect := D2D1RectF(0, 0, fSize.width/2, fSize.height/2);
  cvs.RenderTarget.SetTransform(TD2DMatrix3x2F.Translation(fRect.right+10, fRect.bottom+10));
  cvs.RenderTarget.DrawBitmap(iBitmap, @fRect, 0.75);

  cvs.EndDraw;
  cvs.Free;
end;


posted on 2011-04-01 15:48  万一  阅读(3809)  评论(6)  编辑  收藏
上一篇:Direct2D (10) : 文本输出初步


下一篇:Direct2D (4) : DrawGeometry() 与 FillGeometry()