Table of Contents

Getting started

Install

dotnet add package NetVtk

One AnyCPU package for .NET Framework 3.5 and 4.5+, .NET 6, 8 and 10, and netstandard2.0; the natives come with it, in a NetVtk.runtime.<rid> package per platform.

Important

Windows needs the Visual C++ 2015-2022 runtime; rendering needs OpenGL 3.2. See Platforms.

Tip

Call VtkOutput.Install() at startup. Otherwise VTK shows its errors and warnings in a window of its own on Windows, on the console elsewhere. See Observers and messages.

A source's output

The namespace is Vtk:

using Vtk;
using var cone = new vtkConeSource();
cone.SetResolution(32);
cone.Update();
using var output = cone.GetOutput();
double[] bounds = output.GetBounds();
  • new vtkConeSource() calls VTK's vtkConeSource::New().
  • Update() runs the source; GetOutput() is its vtkPolyData.
  • GetBounds() returns a double* VTK marks as six long: a C# double[6], here -0.5 to 0.5 on each axis.
  • using drops the proxy's reference at the end of the scope. See Object lifetimes.

A picture

Source, mapper, actor, renderer and render window, drawn off the screen and read back as pixels:

// source -> mapper -> actor -> renderer -> render window
using var cone = new vtkConeSource();
using var mapper = new vtkPolyDataMapper();
using var port = cone.GetOutputPort();
mapper.SetInputConnection(port);
using var actor = new vtkActor();
actor.SetMapper(mapper);
using var renderer = new vtkRenderer();
renderer.AddActor(actor);
renderer.SetBackground(0.1, 0.2, 0.4);

// off the screen: no window, the picture stays in the render window's buffer
using var window = new vtkRenderWindow();
window.SetOffScreenRendering(1);
window.SetSize(300, 300);
window.AddRenderer(renderer);
window.Render();

// its pixels: three bytes (RGB) each, row by row
using var grab = new vtkWindowToImageFilter();
grab.SetInput(window);
grab.Update();
using var image = grab.GetOutput();
using var pixelData = image.GetPointData();
using var scalars = pixelData.GetScalars();
using var colors = vtkUnsignedCharArray.SafeDownCast(scalars);
byte[] pixels = colors.ToArray();

To draw into an application's window instead, see Rendering and hosting.

Where next

To Read
connect filters Pipelines
build a mesh with fields from C# arrays Data and arrays
show a simulation result Results
show VTK in WPF or Avalonia Rendering and hosting
translate VTK C++ code From C++ to C#