delphi - VCL.位图到 FMX.位图

标签 delphi firemonkey delphi-xe4

我在网上找到了这段代码,但是FMX.Bitmap没有扫描线。 是否可以以某种方式将 VCL.TBitmap 复制或绘制到 FMX.Bitmap ?

{$IFDEF MSWINDOWS}
type
  TBitmap = FMX.Types.TBitmap;
  TVclBitmap = Vcl.Graphics.TBitmap;

procedure TakeScreenshot(Dest: FMX.Types.TBitmap);
var
  DC: HDC;
  Size: TPointF;
  VCLBitmap: TVclBitmap;
  Y: Integer;
begin
  VCLBitmap := nil;
  //Size := FMX.Platform.IFMXScreenService.GetScreenSize;
  DC := GetDC(0);
  try
    VCLBitmap := TVclBitmap.Create;
    VCLBitmap.PixelFormat := pf32bit;
    VCLBitmap.SetSize(Trunc(Size.X), Trunc(Size.Y));
    BitBlt(VCLBitmap.Canvas.Handle, 0, 0, VCLBitmap.Width, VCLBitmap.Height,
      DC, 0, 0, SRCCOPY);
    Dest.SetSize(VCLBitmap.Width, VCLBitmap.Height);
    { The format of a FMX bitmap and a 32 bit VCL bitmap is the same, so just
      copy the scanlines. - not true- FMX bitmap does not have ScanLine? }
    for Y := Dest.Height - 1 downto 0 do
      Move(VCLBitmap.ScanLine[Y]^, Dest.ScanLine[Y]^, Dest.Width * 4);
    {Dest.Canvas.DrawBitmap(); Not possible to assign or draw}
  finally
    ReleaseDC(0, DC);
    VCLBitmap.Free;
  end;
end;
{$ENDIF}

最佳答案

您可以使用流:

{$IFDEF MSWINDOWS}

type

  TVclBitmap = Vcl.Graphics.TBitmap;

procedure TakeScreenshot(Dest: TBitmap);
var
  DC: HDC;
  Size: TPointF;
  VCLBitmap: TVclBitmap;
  Y: Integer;
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  VCLBitmap := nil;
  // Size := FMX.Platform.IFMXScreenService.GetScreenSize;
  DC := GetDC(0);
  Size.X := 500;
  Size.Y := 500;
  try
    VCLBitmap := TVclBitmap.Create;
    VCLBitmap.PixelFormat := pf32bit;
    VCLBitmap.SetSize(Trunc(Size.X), Trunc(Size.Y));
    BitBlt(VCLBitmap.Canvas.Handle, 0, 0, VCLBitmap.Width, VCLBitmap.Height, DC,
      0, 0, SRCCOPY);
    Dest.SetSize(VCLBitmap.Width, VCLBitmap.Height);
    { The format of a FMX bitmap and a 32 bit VCL bitmap is the same, so just
      copy the scanlines. - not true- FMX bitmap does not have ScanLine? }
    VCLBitmap.SaveToStream(MS);
    MS.Position := 0;
    Dest.LoadFromStream(MS);
    MS.Free;
    { Dest.Canvas.DrawBitmap(); Not possible to assign or draw }
  finally
    ReleaseDC(0, DC);
    VCLBitmap.Free;
  end;
end;
{$ENDIF}

关于delphi - VCL.位图到 FMX.位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212811/

相关文章:

delphi - OpenGL:增加行数会改变抗锯齿?

delphi - 调用嵌入表单的 Show() 应该显示父表单

android - 重新激活时 Firemonkey TCameraComponent 质量发生变化

Delphi XE4 Firemonkey 网格控件 - 单独设置单元格样式

delphi - delphi中初始化对象

delphi - 如何在泛型类中声明枚举类型的集合类型

delphi - 使用 OpenSSL 生成 key 对

delphi - Delphi 和/或 FreePascal 代码最常见的文档 block

delphi - 如何在 Delphi 中更改 TabControl 中事件 TAB 的颜色

delphi - 异步 TADOQuery 的 OnFetchComplete 未同步到主线程