• Stars
    star
    696
  • Rank 62,404 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

NGraphics is a cross platform library for rendering vector graphics on .NET. It provides a unified API for both immediate and retained mode graphics using high quality native renderers.

NGraphics

NuGet Package Build

NGraphics is a cross platform library for rendering vector graphics on .NET. It provides a unified API for both immediate and retained mode graphics using high quality native renderers.

You can use it for cross platform rendering of UI widgets. Or as the basis for graphically rich interactive views. Or maybe you just want an easy way to import and export SVG and PNG files. Either way, I'm sure you'll find something interesting here.

Installation

Install NGraphics from nuget.

Getting Started

The most important class is ICanvas. Uses canvases to render vector graphics (rectangles, ellipses, paths) to "something". Sometimes canvases are views on the screen, sometimes they are images -- you never really know.

We can draw a little house easily enough:

var canvas = Platforms.Current.CreateImageCanvas (new Size (100), scale: 2);

var skyBrush = new LinearGradientBrush (Point.Zero, Point.OneY, Colors.Blue, Colors.White);
canvas.FillRectangle (new Rect (canvas.Size), skyBrush);
canvas.FillEllipse (10, 10, 30, 30, Colors.Yellow);
canvas.FillRectangle (50, 60, 60, 40, Colors.LightGray);
canvas.FillPath (new PathOp[] {	
	new MoveTo (40, 60),
	new LineTo (120, 60),
	new LineTo (80, 30),
	new ClosePath ()
}, Colors.Gray);

canvas.GetImage ().SaveAsPng (GetPath ("Example1.png"));

Platforms.Current.CreateImageCanvas is just our tricky way to get a platform-specific ICanvas that we can rendered on. IImageCanvases are special because you can call GetImage to get an image of the drawing when you are done. We use a scale of 2 to render retina graphics and keep this README looking good.

Paths are drawn using standard turtle graphics.

Pens and Brushes

When drawing, you have a choice of pens to stroke the object with or brushes to fill it with.

Anyway.

Pens can be any color and any width.

var canvas = Platforms.Current.CreateImageCanvas (new Size (120*5, 120), scale: 2);

canvas.Translate (20, 20);
for (var i = 0; i < 5; i++) {
	canvas.DrawEllipse (
		new Rect (new Size (80)),
		pen: Pens.DarkGray.WithWidth (1 << i),
		brush: Brushes.LightGray);
	canvas.Translate (120, 0);
}

canvas.GetImage ().SaveAsPng (GetPath ("PenWidths.png"));

Brushes can be solid colors or trippy multi-color gradients (linear and radial!)

There is no multi-layering within elements, so you will have to draw them a few times with different brushes to get complex effects.

Colors

What would a graphics library be without a Color class? Well, this one is a struct. Colors are light-weight, have fun with them.

Normally you will use the RGBA constructor of color: new Color (r, g, b, a) where each value can range from 0 to 1.

If you're not normal, you might prefer the web notation: Color.FromRGB (0xBEEFEE).

Retained Mode

Sometimes it's nice to hang onto the graphical elements themselves so that you can change them later, or perhaps cache them from an expensive-to-compute draw operation, or maybe you just want to sing to them. Whatever your needs, NGraphics exposes the following graphical elements:

  • Rectangles are best used for drawing rectangles.
  • Ellipses can also be used to draw ovals and circles.
  • Paths can draw anything that you can imagine, and more. Lines, curves, turtles, they're all for the taking.
var circle = new Ellipse (new Rectangle (Point.Zero, new Size (10)));

ICanvas canvas = ...;
circle.Draw (canvas);

Platforms

  • Android (Xamarin) using Android.Graphics
    • CanvasCanvas wraps a Android.Graphics.Canvas
  • iOS (Xamarin) using CoreGraphics
    • CGContextCanvas wraps a CoreGraphics.CGContext
  • Mac (Xamarin) using CoreGraphics
    • CGContextCanvas wraps a CoreGraphics.CGContext
  • .NET 4.5 using System.Drawing
    • GraphicsCanvas wraps a System.Drawing.Graphics
  • Windows Store 8.1 using Direct2D
    • RenderTargetCanvas wraps a SharpDX.Direct2D1.RenderTarget
  • Windows Phone 8.1 using Direct2D
    • RenderTargetCanvas wraps a SharpDX.Direct2D1.RenderTarget
  • Universal Windows Platform (UWP) using Direct2D
    • RenderTargetCanvas wraps a SharpDX.Direct2D1.RenderTarget

Editor

To speed up the process of drawing with code, NGraphics ships with a code editor and live preview for OS X. Download the editor from the Releases page.

Any C# file that can be independently compiled can be used. The advantage of this editor over Xamarin Studio is that you can work on your drawings without having to wait for your whole project to compile and run.

Simply compile and run the project NGraphics.Editor or download the editor to get started.

Examples

For more examples, check out the images in the TestResults directory and the test code that generated them.

Icon

The NGraphics icon can be rendered using a simple repeating path:

var size = new Size (64);
var canvas = Platforms.Current.CreateImageCanvas (size, scale: 2);
canvas.SaveState ();
canvas.Scale (size);
canvas.Translate (1 / 8.0, 0);

var p = new Path ();
p.MoveTo (0, 1);
p.LineTo (0, 0);
p.LineTo (0.5, 1);
p.LineTo (0.5, 0);

var colors = new [] {
	"#DCDCDD",
	"#C5C3C6",
	"#46494C",
	"#4C5C68",
	"#68A5E2",
};
foreach (var c in colors) {
	p.Pen = new Pen (c, 1 / 4.0);
	p.Draw (canvas);
	canvas.Translate (1 / 16.0, 0);
}

canvas.GetImage ().SaveAsPng (GetPath ("Icon.png"));

Cats

NGraphics also supports scaling cats:

var img = GetResourceImage ("cat.png");
var canvas = Platform.CreateImageCanvas (new Size (100, 200), transparency: true);
canvas.DrawImage (img, new Rect (new Size (50)));
canvas.DrawImage (img, new Rect (new Point (50, 0), new Size (50)));
canvas.DrawImage (img, new Rect (new Point (0, 50), new Size (50, 150)));
canvas.DrawImage (img, new Rect (new Point (50, 50), new Size (50, 150)));
canvas.GetImage ().SaveAsPng (GetPath ("ImageCanvas.Cats"));

License

The MIT License (MIT)

See LICENSE for details.

More Repositories

1

sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
C#
3,753
star
2

Ooui

A small cross-platform UI library that brings the simplicity of native UI development to the web
C#
1,616
star
3

Netjs

Compile .NET assemblies to TypeScript and JavaScript
C#
955
star
4

FuGetGallery

An alternative web UI for browsing nuget packages
C#
672
star
5

webgpu-torch

Tensor computation with WebGPU acceleration
TypeScript
488
star
6

Continuous

Continuous IDE Addin enables live coding from Xamarin Studio and Visual Studio
C#
268
star
7

CLanguage

C parser, compiler, and interpreter for .NET
C#
252
star
8

Bind

A small but powerful C# library for data binding
C#
205
star
9

CrossGraphics

Cross-platform Graphics Library for .NET
C#
194
star
10

transformers-js

Browser-compatible JS library for running language models
JavaScript
178
star
11

ImmutableUI

Immutable objects that mirror popular object oriented UIs
C#
115
star
12

ListDiff

C#
113
star
13

Iril

Compiles LLVM IR (bytecode) or C to .NET Assemblies
LLVM
94
star
14

web-transformers

Transformer neural networks in the browser
TypeScript
89
star
15

Praeclarum.MacCatalyst

Convert your Xamarin.iOS apps to Mac Catalyst apps by adding this nuget.
C#
63
star
16

Csg

Solid modeling library for .NET
C#
62
star
17

AskGPT

Ask ChatGPT questions from the command line.
C#
58
star
18

SwiftSharp

Swift compiler for .NET
HTML
52
star
19

Gone

GO compiler for .NET
F#
42
star
20

HotDogOrNot

A minimal Xamarin CoreML app to detect hotdogs
C#
40
star
21

lcars

LCARS Reader - the coolest iPad news reader not on the App Store
C#
36
star
22

CSharpInteractive

C# Interactive is a REPL for C# in Xamarin Studio
C#
30
star
23

QuickTest

C#
28
star
24

Praeclarum

My general purpose library
C#
28
star
25

GooglePlus

GooglePlus API Documentation and .NET Implementation
C#
27
star
26

MetalTensors

.NET Neural Network training library using Apple's Metal Performance Shaders
C#
26
star
27

runcs

The C# compiler hosted in a web app
C#
26
star
28

odata

OData Browser for the iPhone
C#
26
star
29

CSharpToSwift

Converts your C# projects to Swift
C#
23
star
30

StopCrashing

Script to detect possible sources of crashes in Xamarin apps
F#
21
star
31

FunctionalMeetup

Example code for Frank Krueger's Functional Mobile Apps talk at NDC Oslo 2016
F#
19
star
32

EasyLayout

EasyLayout makes writing auto layout code in Xamarin.iOS easier.
C#
18
star
33

SdfKit

.NET library to convert signed distance functions (SDFs) into triangle meshes and 3D renders
C#
17
star
34

OouiChat

Chat room web app built using Xamarin.Forms, ASP.NET, and Ooui
C#
16
star
35

MarriageClock

An IoT thing in the shape of heart that tells you how long you've been married
C++
13
star
36

ThreeDO

C#
13
star
37

Ur

Game of Ur using Blazor
C#
13
star
38

CircuitTranslations

Language support for iCircuit
F#
13
star
39

ARDemo

Xamarin Evolve 2014 Augmented Reality Demo
C#
12
star
40

fuget

C#
10
star
41

caulker

3D Map for MonoTouch
C#
10
star
42

praeclarum.github.io

Frank A. Krueger's blog
HTML
10
star
43

WebGlobe

3D Globe Screensaver Written in WebGPU
JavaScript
9
star
44

BuildLight

Show IDE build status using an IoT device
C#
8
star
45

NMusic

Library and demo app for composing music and playing it on iOS
C#
8
star
46

Demo11

iOS 11 Demo Code Presented at the Seattle Mobile .NET Meetup
C#
8
star
47

JavaScriptLanguage

Resharper add-in that decompiles to JavaScript.
C#
8
star
48

circuitpython-net

CircuitPython compiled to run on .NET
C#
8
star
49

CuneiformTranslators

Neural network trained to translate from ancient languages to modern languages
Jupyter Notebook
8
star
50

ukf

Unscented Kalman Filter Library
7
star
51

ClrTools

Tools for .NET Developers
C#
7
star
52

ImageRecognizer

Uses Metal Performance Shaders on iOS 13+ to train a neural network to recognize shapes
C#
7
star
53

rt

My Pet Raytracer in C#
C#
6
star
54

Monospace11

Demo code from Frank Krueger's talk at Monospace
C#
6
star
55

rouse

Declarative RESTful server
C#
5
star
56

NEcho

Amazon Echo ("Alexa") ASP.NET Core Web Service
C#
5
star
57

html5

C#
5
star
58

NeuralScanner

C++
4
star
59

Fom

F#
4
star
60

VectorPerf

Measures the performance of different vector types in .NET
C#
4
star
61

Knossus

Quick and easy web apps
C#
4
star
62

WasmAloneLab

HTML
4
star
63

ColmapSharp

COLMAP builds and bindings for .NET
C++
4
star
64

UIDays

Cataloging the release dates of various UI frameworks
4
star
65

HomeAI

C#
4
star
66

SubMark1Sim

F# Code to visualize a submarine control simulation
F#
3
star
67

AppleNative.Templates

F#
3
star
68

dotnet-tree-sitter

.NET bindings to the Tree-sitter parsing library
Makefile
3
star
69

Lilui

The lilest cross platform UI library for .NET
C#
3
star
70

BuildBoard

Display build status on an LED matrix
C++
2
star
71

nerf-py

Python
1
star
72

VacuumMold

C#
1
star
73

PythonRepl

Python REPL for iOS using Xamarin and IronPython
C#
1
star
74

Armina

Programming language cross compilation tools
C#
1
star