{"id":13415266,"url":"https://github.com/praeclarum/NGraphics","last_synced_at":"2025-03-14T22:33:13.640Z","repository":{"id":26698224,"uuid":"30155248","full_name":"praeclarum/NGraphics","owner":"praeclarum","description":"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.","archived":false,"fork":false,"pushed_at":"2024-02-19T14:27:02.000Z","size":15343,"stargazers_count":706,"open_issues_count":62,"forks_count":133,"subscribers_count":49,"default_branch":"master","last_synced_at":"2024-09-18T05:07:25.619Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/praeclarum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"Contributing.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"praeclarum"}},"created_at":"2015-02-01T18:31:02.000Z","updated_at":"2024-09-08T01:37:51.000Z","dependencies_parsed_at":"2024-06-21T16:46:27.004Z","dependency_job_id":"9672165b-2ab0-4a81-923b-45ce316f18b2","html_url":"https://github.com/praeclarum/NGraphics","commit_stats":{"total_commits":369,"total_committers":18,"mean_commits":20.5,"dds":"0.19241192411924124","last_synced_commit":"8ae60d99076f3aabf9b533a30de5a6939d674eae"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praeclarum%2FNGraphics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praeclarum%2FNGraphics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praeclarum%2FNGraphics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praeclarum%2FNGraphics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/praeclarum","download_url":"https://codeload.github.com/praeclarum/NGraphics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243658057,"owners_count":20326459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T21:00:46.233Z","updated_at":"2025-03-14T22:33:13.633Z","avatar_url":"https://github.com/praeclarum.png","language":"C#","funding_links":["https://github.com/sponsors/praeclarum"],"categories":["Graphics","图形","Media"],"sub_categories":[],"readme":"# NGraphics\n\n[![NuGet Package](https://img.shields.io/nuget/v/NGraphics.svg)](https://www.nuget.org/packages/NGraphics) ![Build](https://github.com/praeclarum/NGraphics/workflows/Build/badge.svg?branch=master)\n\n\u003cimg src=\"TestResults/Icon-Mac.png\" width=\"64\" height=\"64\" /\u003e \n\nNGraphics 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.\n\nYou 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.\n\n\n\n\n\n## Installation\n\n[Install NGraphics from nuget](https://www.nuget.org/packages/NGraphics/).\n\n\n\n## Getting Started\n\nThe 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.\n\nWe can draw a little house easily enough:\n\n```csharp\nvar canvas = Platforms.Current.CreateImageCanvas (new Size (100), scale: 2);\n\nvar skyBrush = new LinearGradientBrush (Point.Zero, Point.OneY, Colors.Blue, Colors.White);\ncanvas.FillRectangle (new Rect (canvas.Size), skyBrush);\ncanvas.FillEllipse (10, 10, 30, 30, Colors.Yellow);\ncanvas.FillRectangle (50, 60, 60, 40, Colors.LightGray);\ncanvas.FillPath (new PathOp[] {\t\n\tnew MoveTo (40, 60),\n\tnew LineTo (120, 60),\n\tnew LineTo (80, 30),\n\tnew ClosePath ()\n}, Colors.Gray);\n\ncanvas.GetImage ().SaveAsPng (GetPath (\"Example1.png\"));\n```\n\n\u003cimg src=\"TestResults/Example1-Mac.png\" width=\"100\" height=\"100\" /\u003e\n\n`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.\n\nPaths are drawn using standard turtle graphics.\n\n\n\n## Pens and Brushes\n\nWhen drawing, you have a choice of pens to stroke the object with or brushes to fill it with.\n\nAnyway.\n\n`Pens` can be any *color* and any *width*.\n\n```csharp\nvar canvas = Platforms.Current.CreateImageCanvas (new Size (120*5, 120), scale: 2);\n\ncanvas.Translate (20, 20);\nfor (var i = 0; i \u003c 5; i++) {\n\tcanvas.DrawEllipse (\n\t\tnew Rect (new Size (80)),\n\t\tpen: Pens.DarkGray.WithWidth (1 \u003c\u003c i),\n\t\tbrush: Brushes.LightGray);\n\tcanvas.Translate (120, 0);\n}\n\ncanvas.GetImage ().SaveAsPng (GetPath (\"PenWidths.png\"));\n```\n\n\u003cimg src=\"TestResults/PenWidths-Mac.png\" width=\"600\" height=\"120\" /\u003e\n\n\n`Brushes` can be solid colors or trippy multi-color gradients (linear and radial!)\n\nThere is no multi-layering within elements, so you will have to draw them a few times with different brushes to get complex effects.\n\n\n## Colors\n\nWhat would a graphics library be without a `Color` class? Well, this one is a struct. Colors are light-weight, have fun with them.\n\nNormally you will use the RGBA constructor of color: `new Color (r, g, b, a)` where each value can range from 0 to 1.\n\nIf you're not normal, you might prefer the web notation: `Color.FromRGB (0xBEEFEE)`.\n\n\n\n## Retained Mode\n\nSometimes 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:\n\n* `Rectangles` are best used for drawing rectangles.\n* `Ellipses` can also be used to draw ovals and circles.\n* `Paths` can draw anything that you can imagine, and more. Lines, curves, turtles, they're all for the taking.\n\n\n```charp\nvar circle = new Ellipse (new Rectangle (Point.Zero, new Size (10)));\n\nICanvas canvas = ...;\ncircle.Draw (canvas);\n```\n\n## Platforms\n\n\n* Android (Xamarin) using Android.Graphics\n\t- `CanvasCanvas` wraps a `Android.Graphics.Canvas`\n* iOS (Xamarin) using CoreGraphics\n\t- `CGContextCanvas` wraps a `CoreGraphics.CGContext`\n* Mac (Xamarin) using CoreGraphics\n\t- `CGContextCanvas` wraps a `CoreGraphics.CGContext`\n* .NET 4.5 using System.Drawing\n\t- `GraphicsCanvas` wraps a `System.Drawing.Graphics`\n* Windows Store 8.1 using Direct2D\n\t- `RenderTargetCanvas` wraps a `SharpDX.Direct2D1.RenderTarget`\n* Windows Phone 8.1 using Direct2D\n\t- `RenderTargetCanvas` wraps a `SharpDX.Direct2D1.RenderTarget`\n* Universal Windows Platform (UWP) using Direct2D\n\t- `RenderTargetCanvas` wraps a `SharpDX.Direct2D1.RenderTarget`\n\n## Editor\n\nTo 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.](https://github.com/praeclarum/NGraphics/releases)\n\n\u003cimg src=\"Documentation/Editor.png\" width=\"640\" /\u003e\n\nAny 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.\n\nSimply compile and run the project [NGraphics.Editor](https://github.com/praeclarum/NGraphics/tree/master/NGraphics.Editor) or [download the editor](https://github.com/praeclarum/NGraphics/releases) to get started.\n\n\n## Examples\n\nFor more examples, check out the images in the [TestResults directory](https://github.com/praeclarum/NGraphics/tree/master/TestResults)\nand the [test code](https://github.com/praeclarum/NGraphics/tree/master/NGraphics.Test) that generated them.\n\n### Icon\n\nThe NGraphics icon can be rendered using a simple repeating path:\n\n```csharp\nvar size = new Size (64);\nvar canvas = Platforms.Current.CreateImageCanvas (size, scale: 2);\ncanvas.SaveState ();\ncanvas.Scale (size);\ncanvas.Translate (1 / 8.0, 0);\n\nvar p = new Path ();\np.MoveTo (0, 1);\np.LineTo (0, 0);\np.LineTo (0.5, 1);\np.LineTo (0.5, 0);\n\nvar colors = new [] {\n\t\"#DCDCDD\",\n\t\"#C5C3C6\",\n\t\"#46494C\",\n\t\"#4C5C68\",\n\t\"#68A5E2\",\n};\nforeach (var c in colors) {\n\tp.Pen = new Pen (c, 1 / 4.0);\n\tp.Draw (canvas);\n\tcanvas.Translate (1 / 16.0, 0);\n}\n\ncanvas.GetImage ().SaveAsPng (GetPath (\"Icon.png\"));\n```\n\n\u003cimg src=\"TestResults/Icon-Mac.png\" width=\"64\" height=\"64\" /\u003e\n\n\n### Cats\n\nNGraphics also supports scaling cats:\n\n```csharp\nvar img = GetResourceImage (\"cat.png\");\nvar canvas = Platform.CreateImageCanvas (new Size (100, 200), transparency: true);\ncanvas.DrawImage (img, new Rect (new Size (50)));\ncanvas.DrawImage (img, new Rect (new Point (50, 0), new Size (50)));\ncanvas.DrawImage (img, new Rect (new Point (0, 50), new Size (50, 150)));\ncanvas.DrawImage (img, new Rect (new Point (50, 50), new Size (50, 150)));\ncanvas.GetImage ().SaveAsPng (GetPath (\"ImageCanvas.Cats\"));\n```\n\n\u003cimg src=\"TestResults/ImageCanvas.Cats-Mac.png\" width=\"100\" height=\"200\" /\u003e\n\n\n## License\n\nThe MIT License (MIT)\n\nSee [LICENSE](LICENSE) for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraeclarum%2FNGraphics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpraeclarum%2FNGraphics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraeclarum%2FNGraphics/lists"}