View Capture Speed in Script

I cobbled together this script from snippets I found on the forum. It works great, except the actual view capture speed is very slow. I’m using Rhino 8.
image

import Rhino as rc
import rhinoscriptsyntax as rs
import scriptcontext as sc
import ghpythonlib as gh
import ghpythonlib.treehelpers as th
import os
import System

cameraLocs = loc

file_names = th.tree_to_list(file_name, 0)
opt_names = th.tree_to_list(opt_name, 0)
bboxes = th.tree_to_list(bbox, 0)

def zoom_to_geometry(rectangle, padding=10):
    """
    Creates a boundary around the provided point and zooms the Rhino view to this boundary.

    Args:
        rectangle: Bounding rectangle of view.
        padding: Additional space around the boundary to ensure the geometry is not clipped in the view.

    """
   
    # Calculate the bounding box of the rectangle, including padding if necessary
    bbox = rectangle.BoundingBox
    if padding > 0:
        # Expand the bounding box by the padding amount
        bbox.Inflate(padding)
    
    # Zoom to the bounding box of the rectangle
    sc.doc.Views.ActiveView.ActiveViewport.ZoomBoundingBox(bbox)
    sc.doc.Views.Redraw()


def captureViewportToFile(file_path, view=None, size=System.Drawing.Size(2500, 1000)):
    view = sc.doc.Views.ActiveView;
    if view:
        view_capture = rc.Display.ViewCapture()
        view_capture.Width = size.Width 
        view_capture.Height = size.Height 
        view_capture.ScaleScreenItems = False
        view_capture.DrawAxes = False
        view_capture.DrawGrid = False
        view_capture.DrawGridAxes = False
        view_capture.TransparentBackground = False
        bitmap = view_capture.CaptureToBitmap(view)
        if bitmap:
            folder = System.Environment.SpecialFolder.Desktop
            path = System.Environment.GetFolderPath(folder)
            bitmap.Save(file_path, System.Drawing.Imaging.ImageFormat.Png);




if run:
    for ind, pt in enumerate(opt_names):
        if ind == 0:
            for idx, bbox in enumerate(bboxes[ind]):
                if idx < 4:
                    zoom_to_geometry(bbox, padding)
                    print(file_names)
                    captureViewportToFile(directory + file_names[ind][idx] + ".png");

The ifs in the run loop are to limit the number of view captures for debugging. Right now, if I just capture the screen at the viewport resolution, it takes a few seconds per view. If I change the capture size, though, the capture can take anywhere between 15 to 60 seconds. I have approx. 40 views to capture, so I’m hoping this can be sped up. I’m not too familiar with the bitmap.Save function, so I’m hoping there is an approach to speed up the capture out there somewhere.

I added a timer to the loops, it looks like each loop takes longer and longer.

import Rhino as rc
import rhinoscriptsyntax as rs
import scriptcontext as sc
import ghpythonlib as gh
import ghpythonlib.treehelpers as th
import os
import System
import time

rhdoc = rc.RhinoDoc.ActiveDoc
sc.doc = rhdoc

cameraLocs = loc

file_names = th.tree_to_list(file_name, 0)
opt_names = th.tree_to_list(opt_name, 0)
bboxes = th.tree_to_list(bbox, 0)

def zoom_to_geometry(rectangle, padding=10):
    startTime = time.time()
    """
    Creates a boundary around the provided point and zooms the Rhino view to this boundary.

    Args:
        bbox: A bounding box.
        padding: Additional space around the boundary to ensure the geometry is not clipped in the view.

    """
   
    # Calculate the bounding box of the rectangle, including padding if necessary
    bbox = rectangle.BoundingBox
    if padding > 0:
        # Expand the bounding box by the padding amount
        bbox.Inflate(padding)
    
    # Zoom to the bounding box of the rectangle
    sc.doc.Views.ActiveView.ActiveViewport.ZoomBoundingBox(bbox)
    sc.doc.Views.Redraw()
    print("zoom fn: ", time.time() - startTime)


def captureViewportToFile(file_path, view=None, size=System.Drawing):
    startTime = time.time()
    size = size.Size(2150, 1400)
    bitmap = sc.doc.Views.ActiveView.CaptureToBitmap(size)
    if bitmap:
        try:
            folder = System.Environment.SpecialFolder.Desktop
            path = System.Environment.GetFolderPath(folder)
            bitmap.Save(file_path, System.Drawing.Imaging.ImageFormat.Png); 
        finally:
            bitmap.Dispose()
    print("capture fn: ", time.time() - startTime)

start = time.time()

if run:
    for ind, pt in enumerate(opt_names):
        if ind < 1:
            for idx, bbox in enumerate(bboxes[ind]):
                if idx < 3:
                    currentTime = time.time()
                    zoom_to_geometry(bbox, padding)
                    captureViewportToFile(directory + opt_names[ind][0] + file_names[ind][idx] + ".png");
                    print(ind, currentTime - start)
                    start = currentTime

sc.doc = ghdoc

Which gives these times:

zoom fn:  2.2118399143218994
capture fn:  46.72248673439026
main loop:  48.93432664871216
zoom fn:  7.965643882751465
capture fn:  44.652865171432495
main loop:  52.61850905418396
zoom fn:  11.594014167785645
capture fn:  70.62788462638855
main loop:  82.22289824485779

So maybe a caching issue? Memory Leak?