delphi - 如何使用FireMonkey截图(多平台)

标签 delphi screenshot delphi-xe2 firemonkey

我还没有在 FMX.Platform 中找到获取屏幕截图的功能(无论如何,其他地方都没有......)。

对于 VCL,有很多答案(stackoverflow、google...)。

但是如何在 Windows 和 Mac OS X 中获取图像(位图或其他)的屏幕截图?

问候,

W。

更新: link from Tipiweb为 OS X 提供了一个很好的解决方案。

关于Windows部分:我已经编码了这个,但我不喜欢使用VCL和Stream来实现它...... 有更好的建议、意见吗?

谢谢。

W。

uses ..., FMX.Types, Winapi.Windows, Vcl.Graphics;

...

function DesktopLeft: Integer;
begin
  Result := GetSystemMetrics(SM_XVIRTUALSCREEN);
end;

function DesktopWidth: Integer;
begin
  Result := GetSystemMetrics(SM_CXVIRTUALSCREEN);
end;

function DesktopTop: Integer;
begin
  Result := GetSystemMetrics(SM_YVIRTUALSCREEN);
end;

function DesktopHeight: Integer;
begin
  Result := GetSystemMetrics(SM_CYVIRTUALSCREEN);
end;


procedure GetScreenShot(var dest: FMX.Types.TBitmap);
var
  cVCL  : Vcl.Graphics.TCanvas;
  bmpVCL: Vcl.Graphics.TBitmap;
  msBmp : TMemoryStream;
begin
  bmpVCL      := Vcl.Graphics.TBitmap.Create;
  cVCL        := Vcl.Graphics.TCanvas.Create;
  cVCL.Handle := GetWindowDC(GetDesktopWindow);
  try
    bmpVCL.Width := DesktopWidth;
    bmpVCL.Height := DesktopHeight;
    bmpVCL.Canvas.CopyRect(Rect(0, 0, DesktopWidth, DesktopHeight),
                           cVCL,
                           Rect(DesktopLeft, DesktopTop, DesktopLeft + DesktopWidth, DesktopTop + DesktopHeight)
                          );
  finally
    ReleaseDC(0, cVCL.Handle);
    cVCL.Free;
  end;

  msBmp := TMemoryStream.Create;
  try
    bmpVCL.SaveToStream(msBmp);
    msBmp.Position := 0;
    dest.LoadFromStream(msBmp);
  finally
    msBmp.Free;
  end;

最佳答案

我构建了一个小应用程序来截取屏幕截图(Windows/Mac)并且它可以工作:-)!

为了兼容 Windows 和 Mac,我使用流。

API Mac Capture --> TStream

API Windows Capture --> Vcl.Graphics.TBitmap --> TStream.

之后,我将 Windows 或 Mac TStream 加载到 FMX.Types.TBitmap 中(从流加载)

Windows 单元代码:

unit tools_WIN;

interface
{$IFDEF MSWINDOWS}
uses Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF}, System.SysUtils, FMX.Types, VCL.Forms, VCL.Graphics;


  procedure TakeScreenshot(Dest: FMX.Types.TBitmap);
{$ENDIF MSWINDOWS}

implementation

{$IFDEF MSWINDOWS}


procedure WriteWindowsToStream(AStream: TStream);
var
  dc: HDC; lpPal : PLOGPALETTE;
  bm: TBitMap;
begin
{test width and height}
  bm := TBitmap.Create;

  bm.Width := Screen.Width;
  bm.Height := Screen.Height;

  //get the screen dc
  dc := GetDc(0);
  if (dc = 0) then exit;
 //do we have a palette device?
  if (GetDeviceCaps(dc, RASTERCAPS) AND RC_PALETTE = RC_PALETTE) then
  begin
    //allocate memory for a logical palette
    GetMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
    //zero it out to be neat
    FillChar(lpPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
    //fill in the palette version
    lpPal^.palVersion := $300;
    //grab the system palette entries
    lpPal^.palNumEntries :=GetSystemPaletteEntries(dc,0,256,lpPal^.palPalEntry);
    if (lpPal^.PalNumEntries <> 0) then
    begin
      //create the palette
      bm.Palette := CreatePalette(lpPal^);
    end;
    FreeMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
  end;
  //copy from the screen to the bitmap
  BitBlt(bm.Canvas.Handle,0,0,Screen.Width,Screen.Height,Dc,0,0,SRCCOPY);

  bm.SaveToStream(AStream);

  FreeAndNil(bm);
  //release the screen dc
  ReleaseDc(0, dc);
end;


procedure TakeScreenshot(Dest: FMX.Types.TBitmap);
var
  Stream: TMemoryStream;
begin
  try
    Stream := TMemoryStream.Create;
    WriteWindowsToStream(Stream);
    Stream.Position := 0;
    Dest.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

{$ENDIF MSWINDOWS}
end.

Mac 单位代码:

unit tools_OSX;


interface
{$IFDEF MACOS}
uses

  Macapi.CoreFoundation, Macapi.CocoaTypes, Macapi.CoreGraphics, Macapi.ImageIO,
  FMX.Types,
  system.Classes, system.SysUtils;

  procedure TakeScreenshot(Dest: TBitmap);
{$ENDIF MACOS}

implementation
{$IFDEF MACOS}

{$IF NOT DECLARED(CGRectInfinite)}
const
  CGRectInfinite: CGRect = (origin: (x: -8.98847e+30; y: -8.98847e+307);
    size: (width: 1.79769e+308; height: 1.79769e+308));
{$IFEND}


function PutBytesCallback(Stream: TStream; NewBytes: Pointer;
  Count: LongInt): LongInt; cdecl;
begin
  Result := Stream.Write(NewBytes^, Count);
end;

procedure ReleaseConsumerCallback(Dummy: Pointer); cdecl;
begin
end;

procedure WriteCGImageToStream(const AImage: CGImageRef; AStream: TStream;
  const AType: string = 'public.png'; AOptions: CFDictionaryRef = nil);
var
  Callbacks: CGDataConsumerCallbacks;
  Consumer: CGDataConsumerRef;
  ImageDest: CGImageDestinationRef;
  TypeCF: CFStringRef;
begin
  Callbacks.putBytes := @PutBytesCallback;
  Callbacks.releaseConsumer := ReleaseConsumerCallback;
  ImageDest := nil;
  TypeCF := nil;
  Consumer := CGDataConsumerCreate(AStream, @Callbacks);
  if Consumer = nil then RaiseLastOSError;
  try
    TypeCF := CFStringCreateWithCharactersNoCopy(nil, PChar(AType), Length(AType),
      kCFAllocatorNull); //wrap the Delphi string in a CFString shell
    ImageDest := CGImageDestinationCreateWithDataConsumer(Consumer, TypeCF, 1, AOptions);
    if ImageDest = nil then RaiseLastOSError;
    CGImageDestinationAddImage(ImageDest, AImage, nil);
    if CGImageDestinationFinalize(ImageDest) = 0 then RaiseLastOSError;
  finally
    if ImageDest <> nil then CFRelease(ImageDest);
    if TypeCF <> nil then CFRelease(TypeCF);
    CGDataConsumerRelease(Consumer);
  end;
end;

procedure TakeScreenshot(Dest: TBitmap);
var
  Screenshot: CGImageRef;
  Stream: TMemoryStream;
begin
  Stream := nil;
  ScreenShot := CGWindowListCreateImage(CGRectInfinite,
    kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
  if ScreenShot = nil then RaiseLastOSError;
  try
    Stream := TMemoryStream.Create;
    WriteCGImageToStream(ScreenShot, Stream);
    Stream.Position := 0;
    Dest.LoadFromStream(Stream);
  finally
    CGImageRelease(ScreenShot);
    Stream.Free;
  end;
end;



 {$ENDIF MACOS}
end.

在您的 mainForm 单元中:

...
{$IFDEF MSWINDOWS}
  uses tools_WIN;
{$ELSE}
  uses tools_OSX;
{$ENDIF MSWINDOWS}

...
var
  imgDest: TImageControl;
...
TakeScreenshot(imgDest.Bitmap);

如果您有其他想法,请与我联系:-)

关于delphi - 如何使用FireMonkey截图(多平台),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10303256/

相关文章:

delphi - Delphi XE2 Firemonkey的Align属性设置为alScale如何影响坐标系?

delphi - 列表框过滤

delphi - 无法将 TMyClass 对象分配给其基本接口(interface)类型的变量

delphi - 如何在delphi中为azure blob存储生成MD5?

python - PyAutoGUI 截图我唯一的背景(Mac OS)

java - 当使用 JAVA 的 Selenium Webdriver 测试失败时自动截图并上传到 GDrive 或 Dropbox

android - 我需要用UNITY的Android和IOS应用程序设置保存图片的路径

delphi - System.IOUtils 中的 TPath 记录在哪里初始化?

c++ - 如何获取 C++ 接口(interface) VMT 的地址

delphi - Delphi 中的 Indy HTTPS POST 请求使用 TLS 1.0 而不是 TLS 1.2