Direct2D (9) : 显示图像
Direct2D (9) : 显示图像
涂孟超 发表于3年前
Direct2D (9) : 显示图像
  • 发表于 3年前
  • 阅读 11
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   


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;


效果图:



用较原始的办法重做上面的例子(通过 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;


共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: