Capture Image Inside Task

I have this method that captures an image and saves it in a location:

    public void SaveImage()
    {
        string ImageName = @"C:\";
        var doc = Rhino.RhinoDoc.ActiveDoc;
        var view = doc.Views.ActiveView;
        var view_capture = new ViewCapture
        {
            Width = view.ActiveViewport.Size.Width,
            Height = view.ActiveViewport.Size.Height,
            ScaleScreenItems = false,
            DrawAxes = false,
            DrawGrid = false,
            DrawGridAxes = false,
            TransparentBackground = true
        };
        var bitmap = view_capture.CaptureToBitmap(view);
        if (null != bitmap)
        {
            bitmap.Save(ImageName, System.Drawing.Imaging.ImageFormat.Png);
            bitmap.Dispose();
        }
    }

This method works beautifully, but it also takes a little time to execute this method, it is only a little bit but it affects the following functions.

My solution is to run this method within a task: Task.Run(() => saveMat.SaveImage());

Runing with Task, popup “Exception Error”:

I’m not able to find out where this error comes from and why, any ideas?

Hi @MatrixRatrix,

Image capturing must happen on Rhino’s main thread, as the document must be iterated.

– Dale