Create High-rise Image without using Instances.ActiveCanvas

I want to create a high-rise image for a document, but I don’t want to use Instances.ActiveCanvas. Because I don’t want to open it on the active canvas.

I tried to do it like this.

        private static Bitmap CreateImage(GH_Document doc)
        {
            const float zoom = 1;

            var canvas = new GH_Canvas();
            canvas.Document = doc;

            var rec = canvas.Document.BoundingBox();
            rec.X *= zoom;
            rec.Y *= zoom;
            rec.Width *= zoom;
            rec.Height *= zoom;
            var rectangle = GH_Convert.ToRectangle(rec);
            rectangle.Inflate(100, 100);

            GH_Viewport gH_Viewport = new GH_Viewport(canvas.Viewport);
            gH_Viewport.Width = rectangle.Width;
            gH_Viewport.Height = rectangle.Height;
            gH_Viewport.Zoom = zoom;
            gH_Viewport.Tx = -rectangle.X;
            gH_Viewport.Ty = -rectangle.Y;
            gH_Viewport.ComputeProjection();
           
            var bitmap = canvas.GenerateHiResImageTile(gH_Viewport, Color.Transparent);

            canvas.Dispose();

            return bitmap;
        }

But it seems to get a null Graphic. And I got a canvas error.

What should I do?