CaptureToBitmap(settings) + TransparentBackground

Hi,

The script below use ViewCapture.CaptureToBitmap(settings) to save bitmap from viewport.
However the TransparentBackground property is at ViewCapture Class.
I couldn’t find the Transparent property in ViewCaptureSettings Class.
Hope if there’s any suggestion.

def Capture():
    
    selectview = Rhino.RhinoDoc.ActiveDoc.Views.Find(i, False)
    size = System.Drawing.Size(int(width*fector), int(height*fector)) 

    #viewcap = rd.ViewCapture()
    #viewcap.TransparentBackground = True

    settings = rd.ViewCaptureSettings(selectview, size, 300) #view, size, dpi
    settings.SetWindowRect(pt0, pt2)
    settings.RasterMode = True
    settings.DrawGrid = False
    settings.DrawAxis = False
    settings.DrawWallpaper = False
        
    bitmap = rd.ViewCapture.CaptureToBitmap(settings)
    bitmap.Save(path+filename+".png")
1 Like

Hi @ctu6,

Try this:

import Rhino
import scriptcontext as sc
import System

def test_view_capture():
    view = sc.doc.Views.ActiveView
    if view:
        view_capture = Rhino.Display.ViewCapture()
        view_capture.Width = view.ActiveViewport.Size.Width
        view_capture.Height = view.ActiveViewport.Size.Height
        view_capture.ScaleScreenItems = False
        view_capture.DrawAxes = False
        view_capture.DrawGrid = False
        view_capture.DrawGridAxes = False
        view_capture.TransparentBackground = True
        
        bitmap = view_capture.CaptureToBitmap(view)
        if bitmap:
            path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            filename = System.IO.Path.Combine(path, "test_view_capture.png")
            bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)

if __name__=="__main__": 
    test_view_capture()

– Dale

1 Like

Hi @dale , thanks for the reply.

The reason I use ViewCaptureSettings Class is because it has SetWindowRect(Point3d, Point3d) method so that it can do batch captures based on input rectangles. I couldn’t find similar method under ViewCaptrue Class.

The batch capturing seem work now, but the problem is how to make the background transparent…

@dale
Other way I think of is, could ViewCapture Class have SetWindowRect Method which is under ViewCaptureSettings Class?

Hey @ctu6 It’s been a little while since I’ve used this class but if memory serves me correctly I think the DrawBackground bool on ViewCaptureSettings might help you here.

@ctu6 - to output a bitmap with a transparent background, you’ll need to use ViewCapture. Before capturing to bitmap, zoom to the extends that you want to capture.

– Dale

Hi @dale

Thanks for the previous script.
I’ve tried with ViewCapture and ZoomToExtend to the desired image frame, and set the width and height 1:1 to the frame for testing.

As picture shown below, while 400x300(horizontal) the output was fine.
But when 300x400(vertical) there were white border (red area) leak out and the frame got scaled down.
Any ideas about it?

ViewCapture_test.gh (5.5 KB)
image


import Rhino
import scriptcontext as sc
import System

def test_view_capture():
    view = sc.doc.Views.ActiveView
    if view:
        view_capture = Rhino.Display.ViewCapture()
        view_capture.Width = x
        view_capture.Height = y
        view_capture.ScaleScreenItems = False
        view_capture.DrawAxes = False
        view_capture.DrawGrid = False
        view_capture.DrawGridAxes = False
        view_capture.TransparentBackground = True
        
        bitmap = view_capture.CaptureToBitmap(view)
        if bitmap:
            path = mypath
            filename = System.IO.Path.Combine(path, name + ".png")
            bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png)

if __name__=="__main__": 
    test_view_capture()