Batch viewport capture to image

I am using the Rhino Inside Convert console app example to capture viewports to bitmaps and save them to disk as a sort of thumbnail preview for each file.
I put this code inside the obj conversion loop.

        var Views = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(true, false);
        foreach (var view in Views)
        {
          var img = view.CaptureToBitmap(true, true, true);
          img.Save(string.Format("C:\\Some\\Directory\\\\Rhino.Inside.Output\\{some-filename}.png", runCount.ToString(), view.ActiveViewport.Name), System.Drawing.Imaging.ImageFormat.Png);
          img.Dispose();
        }

I am getting
“External component has thrown an exception”
However, one image is being written correctly to disk. No OBJ conversion is happening.
What might be going wrong ? Any help appreciated.
Thanks

You should attach Visual Studio 2017 to the process, then capture all .NET exceptions prior to entering the loop. Then see if the callstack gives any clues.

Paste here if you can’t figure out from that.

Thanks for replying.
In my folder of 3 3dms, only one file is being converted and image is being saved. On attaching a debugger, the correct number of filepaths are read (in this case - 3) but it enters the loop only once.
There are no errors in the console. This is the only thing I found in the debug output.
Exception thrown: 'System.Runtime.InteropServices.SEHException' in RhinoCommon.dll

I have enabled breaking on all CLR exceptions.

@fraguada can you have a look, as this is your sample project?

1 Like

I’m seeing the same behavior over on my side and cannot detect the cause of the exception when calling view.CaptureToBitmap. I’ll keep investigating.

When looping through files, you could do something like this:

// Change the original extension to .png
var pngPath = System.IO.Path.ChangeExtension(file, ".png");

var imgScript = string.Format("_-ViewCaptureToFile \"{0}\" _Enter", pngPath);
RhinoApp.RunScript(imgScript, false);

This obviously doesn’t loop through the views in the file, and is a simple workaround, but we need to dig a bit more before finding the issue you’ve reported.

Thanks. This will do for now, however I was trying to send the bitmap data over a POST request. Now I will have to read from disk after writing.

Hi Luis, I am hitting the same exception when trying to capture a viewport from a hosted viewport and a Rhino.Inside app. It works once, then starts to throw. Any progress on this issue? The ViewCaptureToFile hack does not work in my case as the captured view does not match my viewport at all.