Skip to content

Capturing & Video

Screen/element capture and video recording, ported from FlaUI.Core.Capturing. Capture provides static helpers that return a CaptureImage (savable via to_file and usable as a context manager). VideoRecorder records the screen to a file and requires ffmpeg (see VideoRecorder.download_ffmpeg).

flaui.core.capturing.Capture

Static screen/element capture helpers (mirrors FlaUI.Core.Capturing.Capture).

element(element, settings=None) staticmethod

Capture a single automation element.

Parameters:

Name Type Description Default
element 'AutomationElement'

The element to capture.

required
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def element(element: "AutomationElement", settings: Optional[CaptureSettings] = None) -> CaptureImage:
    """Capture a single automation element.

    :param element: The element to capture.
    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(raw_image=CSCapture.Element(element.raw_element, _cs_settings(settings)))

element_rectangle(element, rectangle, settings=None) staticmethod

Capture a rectangle (relative to the element) inside an element.

Parameters:

Name Type Description Default
element 'AutomationElement'

The element whose region to capture.

required
rectangle Rectangle

The rectangle relative to the element's top-left corner.

required
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def element_rectangle(
    element: "AutomationElement", rectangle: Rectangle, settings: Optional[CaptureSettings] = None
) -> CaptureImage:
    """Capture a rectangle (relative to the element) inside an element.

    :param element: The element whose region to capture.
    :param rectangle: The rectangle relative to the element's top-left corner.
    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(
        raw_image=CSCapture.ElementRectangle(element.raw_element, rectangle.raw_value, _cs_settings(settings))
    )

main_screen(settings=None) staticmethod

Capture the primary screen.

Parameters:

Name Type Description Default
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def main_screen(settings: Optional[CaptureSettings] = None) -> CaptureImage:
    """Capture the primary screen.

    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(raw_image=CSCapture.MainScreen(_cs_settings(settings)))

rectangle(bounds, settings=None) staticmethod

Capture a desktop-relative rectangle.

Parameters:

Name Type Description Default
bounds Rectangle

The screen region to capture.

required
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def rectangle(bounds: Rectangle, settings: Optional[CaptureSettings] = None) -> CaptureImage:
    """Capture a desktop-relative rectangle.

    :param bounds: The screen region to capture.
    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(raw_image=CSCapture.Rectangle(bounds.raw_value, _cs_settings(settings)))

screen(screen_index=-1, settings=None) staticmethod

Capture a screen (or the whole virtual desktop when screen_index is out of range).

Parameters:

Name Type Description Default
screen_index int

Zero-based monitor index; -1 captures all monitors.

-1
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def screen(screen_index: int = -1, settings: Optional[CaptureSettings] = None) -> CaptureImage:
    """Capture a screen (or the whole virtual desktop when ``screen_index`` is out of range).

    :param screen_index: Zero-based monitor index; ``-1`` captures all monitors.
    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(raw_image=CSCapture.Screen(screen_index, _cs_settings(settings)))

screens_with_element(element, settings=None) staticmethod

Capture every screen the given element overlaps.

Parameters:

Name Type Description Default
element 'AutomationElement'

The element to locate across screens.

required
settings Optional[CaptureSettings]

Optional output sizing settings.

None

Returns:

Type Description
CaptureImage

The captured image.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def screens_with_element(element: "AutomationElement", settings: Optional[CaptureSettings] = None) -> CaptureImage:
    """Capture every screen the given element overlaps.

    :param element: The element to locate across screens.
    :param settings: Optional output sizing settings.
    :return: The captured image.
    """
    from FlaUI.Core.Capturing import Capture as CSCapture  # pyright: ignore

    return CaptureImage(raw_image=CSCapture.ScreensWithElement(element.raw_element, _cs_settings(settings)))

flaui.core.capturing.CaptureImage

Bases: BaseModel

A captured image (mirrors FlaUI.Core.Capturing.CaptureImage).

Wraps the C# result of a :class:Capture call. Use :meth:to_file to persist it, or use it as a context manager to dispose the underlying bitmap automatically.

bitmap property

Return the underlying C# Bitmap.

original_bounds property

Return the desktop-relative bounding rectangle this image was captured from.

__enter__()

Enter the context manager.

Returns:

Type Description
'CaptureImage'

This :class:CaptureImage.

Source code in flaui/core/capturing.py
def __enter__(self) -> "CaptureImage":
    """Enter the context manager.

    :return: This :class:`CaptureImage`.
    """
    return self

__exit__(*exc_info)

Dispose the image on context-manager exit.

Parameters:

Name Type Description Default
exc_info Any

Standard (exc_type, exc, tb) tuple (unused).

()
Source code in flaui/core/capturing.py
def __exit__(self, *exc_info: Any) -> None:
    """Dispose the image on context-manager exit.

    :param exc_info: Standard ``(exc_type, exc, tb)`` tuple (unused).
    """
    self.dispose()

apply_overlays(*overlays)

Draw the given C# ICaptureOverlay objects onto the image.

Parameters:

Name Type Description Default
overlays Any

Raw C# overlay instances (e.g. MouseOverlay, InfoOverlay).

()

Returns:

Type Description
'CaptureImage'

This :class:CaptureImage (for chaining).

Source code in flaui/core/capturing.py
@handle_csharp_exceptions
def apply_overlays(self, *overlays: Any) -> "CaptureImage":
    """Draw the given C# ``ICaptureOverlay`` objects onto the image.

    :param overlays: Raw C# overlay instances (e.g. ``MouseOverlay``, ``InfoOverlay``).
    :return: This :class:`CaptureImage` (for chaining).
    """
    self.raw_image.ApplyOverlays(*overlays)
    return self

dispose()

Dispose the underlying bitmap and release its memory.

Source code in flaui/core/capturing.py
@handle_csharp_exceptions
def dispose(self) -> None:
    """Dispose the underlying bitmap and release its memory."""
    self.raw_image.Dispose()

to_file(file_path)

Save the image to a file; the extension selects the format (defaults to PNG).

Parameters:

Name Type Description Default
file_path str

Destination path (.png, .jpg, .gif, .tif, .bmp).

required
Source code in flaui/core/capturing.py
@handle_csharp_exceptions
def to_file(self, file_path: str) -> None:
    """Save the image to a file; the extension selects the format (defaults to PNG).

    :param file_path: Destination path (``.png``, ``.jpg``, ``.gif``, ``.tif``, ``.bmp``).
    """
    self.raw_image.ToFile(file_path)

flaui.core.capturing.CaptureSettings

Bases: BaseModel

Output sizing options for a capture (mirrors FlaUI.Core.Capturing.CaptureSettings).

cs_object property

Return the underlying C# CaptureSettings built from this model.

flaui.core.capturing.VideoRecorder

Bases: BaseModel

Records the screen (or a custom frame source) to a video file.

Mirrors FlaUI.Core.Capturing.VideoRecorder. Start a recording with :meth:start, then call :meth:stop (or use it as a context manager). Requires ffmpeg — see the module note.

target_video_path property

Return the output video file path.

__enter__()

Enter the context manager.

Returns:

Type Description
'VideoRecorder'

This :class:VideoRecorder.

Source code in flaui/core/capturing.py
def __enter__(self) -> "VideoRecorder":
    """Enter the context manager.

    :return: This :class:`VideoRecorder`.
    """
    return self

__exit__(*exc_info)

Stop and dispose the recorder on context-manager exit.

Parameters:

Name Type Description Default
exc_info Any

Standard (exc_type, exc, tb) tuple (unused).

()
Source code in flaui/core/capturing.py
def __exit__(self, *exc_info: Any) -> None:
    """Stop and dispose the recorder on context-manager exit.

    :param exc_info: Standard ``(exc_type, exc, tb)`` tuple (unused).
    """
    self.dispose()

dispose()

Dispose the recorder (stops it if still running).

Source code in flaui/core/capturing.py
@handle_csharp_exceptions
def dispose(self) -> None:
    """Dispose the recorder (stops it if still running)."""
    self.raw_recorder.Dispose()

download_ffmpeg(target_folder) staticmethod

Download a static ffmpeg build into target_folder (blocking).

Parameters:

Name Type Description Default
target_folder str

Folder to download ffmpeg into.

required

Returns:

Type Description
str

The path to the downloaded ffmpeg executable.

Source code in flaui/core/capturing.py
@staticmethod
@handle_csharp_exceptions
def download_ffmpeg(target_folder: str) -> str:
    """Download a static ffmpeg build into ``target_folder`` (blocking).

    :param target_folder: Folder to download ffmpeg into.
    :return: The path to the downloaded ffmpeg executable.
    """
    from FlaUI.Core.Capturing import VideoRecorder as CSVideoRecorder  # pyright: ignore

    return CSVideoRecorder.DownloadFFMpeg(target_folder).Result

start(settings, capture_method=None) classmethod

Start recording.

Parameters:

Name Type Description Default
settings VideoRecorderSettings

Recorder settings (must include target_video_path).

required
capture_method Optional[Callable[['VideoRecorder'], CaptureImage]]

Optional callback (recorder) -> CaptureImage producing each frame. Defaults to capturing the whole virtual desktop.

None

Returns:

Type Description
'VideoRecorder'

The running :class:VideoRecorder.

Source code in flaui/core/capturing.py
@classmethod
@handle_csharp_exceptions
def start(
    cls,
    settings: VideoRecorderSettings,
    capture_method: Optional[Callable[["VideoRecorder"], CaptureImage]] = None,
) -> "VideoRecorder":
    """Start recording.

    :param settings: Recorder settings (must include ``target_video_path``).
    :param capture_method: Optional callback ``(recorder) -> CaptureImage`` producing each frame.
        Defaults to capturing the whole virtual desktop.
    :return: The running :class:`VideoRecorder`.
    """
    from System import Func  # pyright: ignore
    from FlaUI.Core.Capturing import (  # pyright: ignore
        Capture as CSCapture,
        CaptureImage as CSCaptureImage,
        VideoRecorder as CSVideoRecorder,
    )

    if capture_method is None:

        def _frame(_recorder: Any) -> Any:
            """Default frame source: capture the whole desktop."""
            return CSCapture.Screen()
    else:

        def _frame(recorder: Any) -> Any:
            """Bridge the C# frame request to the user's Python capture callback."""
            result = capture_method(cls(raw_recorder=recorder))
            return result.raw_image if isinstance(result, CaptureImage) else result

    delegate = Func[CSVideoRecorder, CSCaptureImage](_frame)
    raw = CSVideoRecorder(settings.cs_object, delegate)
    return cls(raw_recorder=raw, keep_alive=(_frame, delegate))

stop()

Stop recording and finish encoding the output file.

Source code in flaui/core/capturing.py
@handle_csharp_exceptions
def stop(self) -> None:
    """Stop recording and finish encoding the output file."""
    self.raw_recorder.Stop()

flaui.core.capturing.VideoRecorderSettings

Bases: BaseModel

Settings for :class:VideoRecorder (mirrors FlaUI.Core.Capturing.VideoRecorderSettings).

cs_object property

Return the underlying C# VideoRecorderSettings built from this model.

flaui.core.capturing.VideoFormat

Bases: Enum

Video codec used by :class:VideoRecorder (mirrors FlaUI.Core.Capturing.VideoFormat).