# CaptureToBitmap() with transparency

**URL:** <https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905>\
**Category:** Rhino Developer\
**Created:** [January 3, 2014, 7:31pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905 "2014-01-03T19:31:49Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaLo](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/halo/32/216615_2.png) [@HaLo](https://discourse.mcneel.com/u/HaLo)\
**Post date:** [January 3, 2014, 7:31pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/1 "2014-01-03T19:31:49Z")

</div>

After some experimentation, I finally give up.  
I tried every way I could find/come up with, to make the Viewport background transparent. The result of CaptureToBitmap() will always be a 24bpp bitmap without alpha.

Since ViewCaptureToFile is able to save transparent PNGs, is there any way to do so via RhinoCommon?

---

<div class="post-metadata">

**Author:** ![Alain](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/alain/32/512812_2.png) [@Alain](https://discourse.mcneel.com/u/Alain)\
**Post date:** [January 3, 2014, 8:19pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/2 "2014-01-03T20:19:42Z")

</div>

Hi,

After you capture did you try this?

var bitmap = doc.Views.ActiveView.CaptureToBitmap(true, true, true);  
bitmap.MakeTransparent();

---

<div class="post-metadata">

**Author:** ![HaLo](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/halo/32/216615_2.png) [@HaLo](https://discourse.mcneel.com/u/HaLo)\
**Post date:** [January 3, 2014, 8:56pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/3 "2014-01-03T20:56:18Z")

</div>

I looked into it, but it’s no use.

MakeTransparent() works like the magic wand tool in PS with zero tolerance, which is a pretty bad way to make an image transparent. This does neither account for aliasing nor for the occasional shading color that could get the same color as the background color.

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [January 6, 2014, 6:54pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/4 "2014-01-06T18:54:33Z")

</div>

I don’t see a way of doing this from RhinoCommon. I’ll add the wish to the pile…

---

<div class="post-metadata">

**Author:** ![HaLo](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/halo/32/216615_2.png) [@HaLo](https://discourse.mcneel.com/u/HaLo)\
**Post date:** [January 6, 2014, 7:16pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/5 "2014-01-06T19:16:34Z")

</div>

thank you very much!

---

<div class="post-metadata">

**Author:** ![velopl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/velopl/32/153438_2.png) [@velopl](https://discourse.mcneel.com/u/velopl)\
**Post date:** [October 27, 2016, 9:35am UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/6 "2016-10-27T09:35:40Z")

</div>

Hi @dale  
Just curious if this was implemented in RhinoCommon yet?

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [October 27, 2016, 3:11pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/7 "2016-10-27T15:11:52Z")

</div>

Yes it has. In the Rhino WIP, use `Rhino.Display.ViewCapture`.

– Dale

---

<div class="post-metadata">

**Author:** ![velopl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/velopl/32/153438_2.png) [@velopl](https://discourse.mcneel.com/u/velopl)\
**Post date:** [October 27, 2016, 4:06pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/8 "2016-10-27T16:06:10Z")

</div>

Oh that’s great, thank you

---

<div class="post-metadata">

**Author:** ![henry.langner](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/henry.langner/32/629978_2.png) [@henry.langner](https://discourse.mcneel.com/u/henry.langner)\
**Post date:** [May 7, 2018, 12:56pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/9 "2018-05-07T12:56:05Z")

</div>

Hi @dale,  
I have a problem to use that correctly, I would be glad if you or somebody else could help me. This is my code:

> //fp = filepath  
> //vpN = viewport name  
> void rCommonCap(string fp, string vpN, double scaleFac){  
> Rhino.Display.RhinoView view;  
> if(String.IsNullOrEmpty(vpN)){  
> view = RhinoDocument.Views.ActiveView;  
> } else {  
> view = RhinoDocument.Views.Find(vpN, false);  
> }  
> Rhino.Display.RhinoViewport vp = view.ActiveViewport;  
> Size size = new Size((int) (vp.Size.Width \* scaleFac), (int) (vp.Size.Height \* scaleFac));  
> //Bitmap capture = view.CaptureToBitmap(size);  
> Bitmap capture2 = Rhino.Display.ViewCapture.CaptureToBitmap(view);  
> if(System.IO.File.Exists(fp)){  
> System.IO.File.Delete(fp);  
> }  
> capture2.Save(fp);  
> }

However, it doesnt work. I get an CS0120 error, telling me, that an object reference is necessary. But as far as I understand, the Rhino.Display.ViewCapture.CaptureToBitmap wants a Rhino.Display.RhinoView. Where is my mistake? My programming skills make me feel nooby as hell…

To be honest, I didn’t write the code myself and had a hard time to understand what is happening there. Want I want to achive is a series of PNGs with a transparent background to make a video from.  
If the view.CaptureToBitmap(size) line is used, everything works fine, just with the background being opaque.

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [May 7, 2018, 3:50pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/10 "2018-05-07T15:50:14Z")

</div>

Hi @henry.langner,

Here is a simple example:

[SampleCsViewCapture.cs](https://github.com/mcneel/rhino-developer-samples/blob/6/rhinocommon/cs/SampleCsCommands/SampleCsViewCapture.cs)

Does this help?

– Dale

---

<div class="post-metadata">

**Author:** ![henry.langner](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/henry.langner/32/629978_2.png) [@henry.langner](https://discourse.mcneel.com/u/henry.langner)\
**Post date:** [May 8, 2018, 6:28am UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/11 "2018-05-08T06:28:45Z")

</div>

Yes it did, thank you very much! Love, peace and cotton candy for you!

---

<div class="post-metadata">

**Author:** ![psarras\_st](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/psarras_st/32/598486_2.png) [@psarras\_st](https://discourse.mcneel.com/u/psarras_st)\
**Post date:** [May 20, 2018, 8:39am UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/12 "2018-05-20T08:39:05Z")

</div>

Hi @dale ,  
In the past I was using: RhinoDoc.ActiveDoc.Views.ActiveView.CaptureToBitmap(…) to generate hundreds of images at a time. However, it was quite slow and was thinking if there was a way to speed it up. I tried your solution using the ViewCapture class but it seems like it takes about the same time for each frame. Is there a faster way? I was reading here: [http://developer.rhino3d.com/api/RhinoCommon/html/T\_Rhino\_Display\_DisplayBitmap.htm](http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Display_DisplayBitmap.htm)  
that you can potential use the Bitmap used by the display. Any ideas of how to do it?

I hope it is alright I am posting it here. Thanks!  
Stam

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [May 21, 2018, 3:23pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/13 "2018-05-21T15:23:15Z")

</div>

Hi @psarras_st,

I don’t know any way of speeding up view capturing. Perhaps more information on what you are doing and why might help us understand what you need?

– Dale

---

<div class="post-metadata">

**Author:** ![psarras\_st](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/psarras_st/32/598486_2.png) [@psarras\_st](https://discourse.mcneel.com/u/psarras_st)\
**Post date:** [May 21, 2018, 3:49pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/14 "2018-05-21T15:49:05Z")

</div>

Hi @dale

I basically have saved a few camera positions in GH and I want to batch save all their views to a file.  
What I am doing up untill know is move the viewport camera, and using RhinoDoc.ActiveDoc.Views.ActiveView.CaptureToBitmap(…) . I think this can be done faster, because Rhino’s viewport is doing it! When I am moving the camera using the mouse at around 20-30 fps per second, while when I use the previous command I might be getting 2fps.

I also stumbled on my search of Rhino Common to this class: DisplayBitmap Class, that states on its description:

“A bitmap resource that can be used by the display pipeline (currently only in OpenGL display). Reuse DisplayBitmaps for drawing if possible; it is much more expensive to construct new DisplayBitmaps than it is to reuse existing DisplayBitmaps.”

I thought it might have been the way to go, but I am not sure how to call it. Let me know if you need more information.

best,  
Stam

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [May 21, 2018, 9:41pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/15 "2018-05-21T21:41:15Z")

</div>

Hi @psarras_st,

The `DisplayBitmap` class is used for drawing bitmaps in a display conduit. It doesn’t have any to do with capturing a view.

– Dale

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [May 21, 2018, 10:03pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/16 "2018-05-21T22:03:40Z")

</div>

> [@psarras\_st](#):
>
> I think this can be done faster, because Rhino’s viewport is doing it!

Rhino’s viewport is not transferring data from the GPU to local memory in order to create a bitmap. That is an expensive operation which is why it takes longer to generate bitmaps than to just draw in the viewport.

---

<div class="post-metadata">

**Author:** ![psarras\_st](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/psarras_st/32/598486_2.png) [@psarras\_st](https://discourse.mcneel.com/u/psarras_st)\
**Post date:** [May 21, 2018, 10:28pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/17 "2018-05-21T22:28:54Z")

</div>

@stevebaer @dale Is there a way to optimise that then? Threading it maybe?

Thank you for your help

---

<div class="post-metadata">

**Author:** ![Jarek](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jarek/32/776329_2.png) [@Jarek](https://discourse.mcneel.com/u/Jarek)\
**Post date:** [December 13, 2018, 8:11pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/18 "2018-12-13T20:11:01Z")

</div>

hi @Dale,

I am trying to follow your sample:

> [@dale](#):
>
> [SampleCsViewCapture.cs](https://github.com/mcneel/rhino-developer-samples/blob/6/rhinocommon/cs/SampleCsCommands/SampleCsViewCapture.cs)

in Python.  
When trying to set the Width, I get this error:  
`Message: static property 'Width' of 'ViewCapture' can only be assigned to through a type, not an instance`

here is the code snippet:

```auto
    import Rhino.Display 
    import scriptcontext
    import System

    view = scriptcontext.doc.Views.ActiveView

    view_capture = Rhino.Display.ViewCapture
    view_capture.Width = 1000

    bitmap = view.CaptureToBitmap()

```

How can I define the capture properties? I am trying to capture current view with bitmap with custom view size and not to show any of the view widgets like title and world axes…

thanks,

–jarek

---

<div class="post-metadata">

**Author:** ![Jarek](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jarek/32/776329_2.png) [@Jarek](https://discourse.mcneel.com/u/Jarek)\
**Post date:** [December 13, 2018, 8:31pm UTC](https://discourse.mcneel.com/t/capturetobitmap-with-transparency/4905/19 "2018-12-13T20:31:33Z")

</div>

Nevermind! I found your other good sample here, it works now:

> [@ViewCapture + displayed lineweights bug](https://discourse.mcneel.com/t/viewcapture-displayed-lineweights-bug/67610/2):
>
> Hi @tom_svilans, Here is a simple example that might be helpful. import System import Rhino import scriptcontext as sc def SampleViewCaptureToFile(): 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 …
