Taking a screenshot of a web page sounds like a single method call—until you encounter viewport bounds, lazy rendering, sticky headers, cookie banners, and memory limits on long pages.

In this guide, we start from the simplest browser.bitmap() call in Fuzio and build up to a segmented full-page screenshot pipeline tested on long dynamic pages.

Basic Viewport Capture 

Once the page has loaded and the browser has a non-empty size, browser.bitmap() captures the current viewport:

var bitmap = browser.bitmap();
var image = BitmapImage.toToolkit(bitmap);
ImageIO.write(image, "PNG", new File("screenshot.png"));

BitmapImage.toToolkit() converts Fuzio’s Bitmap into a standard BufferedImage for saving with ImageIO.

This captures only the visible viewport (for example, 1024 × 768 logical pixels, scaled by the display’s DPI factor), not the full scrollable document.

Resizing the Browser for Full-Page Capture 

Next, query the document’s scrollable dimensions and resize the browser to match:

var widthScript = """
        Math.max(
            document.body.scrollWidth,
            document.documentElement.scrollWidth)
        """;

var heightScript = """
        Math.max(
            document.body.scrollHeight,
            document.documentElement.scrollHeight)
        """;
var frame = browser.mainFrame().orElseThrow();
Double pageWidth = frame.executeJavaScript(widthScript);
Double pageHeight = frame.executeJavaScript(heightScript);

browser.resize(pageWidth.intValue(), pageHeight.intValue());
Thread.sleep(2000);

var bitmap = browser.bitmap();

Waiting after resize gives newly exposed content time to paint. This works well for medium-length pages, but very tall pages allocate massive bitmaps in Java heap memory.

Segmented Capture and Vertical Stitching 

To keep memory bounded, fix the viewport height, scroll in steps, save each slice, and stitch them vertically:

var shotCount = 15;
var viewportHeight = 1000;

browser.resize(pageWidth.intValue(), viewportHeight);

for (var count = 0; count < shotCount; count++) {
    var scrollY = count * viewportHeight;
    frame.executeJavaScript("window.scrollTo(0, %d)".formatted(scrollY));

    Thread.sleep(500);

    var bitmap = browser.bitmap();
    var image = BitmapImage.toToolkit(bitmap);
    var file = new File("screenshot-%03d.png".formatted(count));
    ImageIO.write(image, "PNG", file);
}

Stitch the slices vertically:

private static void mergeVertically(Path sourceDir, Path target)
        throws IOException {
    try (var paths = Files.list(sourceDir)) {
        var files = paths
                .filter(path -> path.toString().endsWith(".png"))
                .sorted(Comparator.comparing(Path::getFileName))
                .toList();

        var images = new ArrayList<BufferedImage>();
        for (var file : files) {
            images.add(ImageIO.read(file.toFile()));
        }

        var width = images.stream()
                .mapToInt(BufferedImage::getWidth)
                .max()
                .orElse(0);
        var height = images.stream()
                .mapToInt(BufferedImage::getHeight)
                .sum();

        var merged = new BufferedImage(
                width, height, BufferedImage.TYPE_INT_RGB);
        var graphics = merged.createGraphics();
        try {
            var y = 0;
            for (var image : images) {
                graphics.drawImage(image, 0, y, null);
                y += image.getHeight();
            }
        } finally {
            graphics.dispose();
        }

        ImageIO.write(merged, "PNG", target.toFile());
    }
}
mergeVertically(Path.of("."), Path.of("merged.png"));

Without cleanup, position: fixed and position: sticky headers or sidebars repeat in every slice.

After capturing the first slice (count == 1), hide fixed and sticky elements via JavaScript:

var hideFixedElements = """
        (() => {
            document.querySelectorAll('*').forEach(element => {
                const position = getComputedStyle(element).position;
                if (position === 'fixed' || position === 'sticky') {
                    element.style.display = 'none';
                }
            });
        })()
        """;

for (var count = 0; count < shotCount; count++) {
    if (count == 1) {
        frame.executeJavaScript(hideFixedElements);
    }
}

Site-specific overlays such as cookie banners can also be hidden before capture:

var hideCookieBanner = """
        (() => {
            const dialog = document.querySelector('reddit-cookie-banner');
            if (dialog) {
                dialog.style.display = 'none';
            }
        })()
        """;

frame.executeJavaScript(hideCookieBanner);

Conclusion 

For viewport captures, browser.bitmap() is enough. For long dynamic pages, segmented scrolling combined with hiding fixed elements keeps memory predictable and produces clean full-page screenshots.

Add a Chromium-based browser engine to your Java desktop app with Fuzio.

Try for free