Display Legend on viewport

Hi everyone,

I want to draw Legend on the viewport scripting C#.
Here is my image which is what I want to do.

I want to make Legend’s color depend on the value which my component calculates.
And Legends will not be moved even when I panned and rotate viewport.

I’ve already draw 3Dtext using DrawViewportMeshes.
I think what I want is something to do with DrawViewportmeshes, but I couldn’t find how to get viewport coordinete.

Would there be any way to do this?


I found Draw2dRectangle class.
I guess this will be useful for me to draw legend.

Hi @11194,

DisplayPipeline.DrawBitmap might be what you need.

– Dale

Hi Dale,
Thank you for the reply!
Sorry for my late.

I made component displaying legend on viewport with using Draw2d, but It’s location looks bad…

int start = 400;
int length = 400;
for(int j = 0;j < div;j++){
Rectangle rec = new Rectangle(0, length / div * j + start, 50, length / div);

I forcibly modified ‘start’ and ‘length’ to adjust rectangle location every single time.

Do you know how to get position of corner at lower left on viewport?


Hi @11194,

Use RhinoViewport.GetScreenPort.

– Dale

Hi Shimpei, I’m interesting in this topic. And can you show your codes about this display legend on viewport. very thanks!

private void RunScript(bool show, int width, int height)
  if (_conduit == null)
    _conduit = new DrawLegendConduit();
  var gradiant = Grasshopper.GUI.Gradient.GH_Gradient.Heat();
var legend = new Bitmap(width, height);
  for(var x = 1; x < width; x++)
      for(var y = 1; y < height; y++)
        legend.SetPixel(x, y, gradiant.ColourAt((double) y / height));

  _conduit.Legend = legend;
  _conduit.Enabled = show;

// <Custom additional code> 
private DrawLegendConduit _conduit;
private class DrawLegendConduit : DisplayConduit
  public Bitmap Legend;
  protected override void DrawForeground(DrawEventArgs e)
    e.Display.DrawBitmap(new DisplayBitmap(Legend), 50, 50);
// </Custom additional code> 

Conduits.gh (3.2 KB)

thank you very much!