How do I draw simple graphics in C#?
Asked Answered
L

9

20

I just want to draw simple 2D objects like circle, line, square etc in C#. How do I do that? Back in the Turbo C++ days I remember initializing some graphics library for doing the same. Do I need to do something similar in .NET? Is it any different for 3D objects? Will things like DirectX make this any easier? Any links to tutorials or samples much appreciated.

Langdon answered 19/8, 2009 at 21:32 Comment(0)
I
22

As others have said, check out System.Drawing. (I'm only repeating that for completeness.) System.Drawing exposes the GDI+ Windows drawing library to your application.

A good tutorial to get you jump-started with System.Drawing and GDI+ can be found at C# Corner.

Some important items to note:

  1. Many GDI+ objects implement the IDisposable interface, and therefore should be wrapped in using blocks. Be sure you follow the appropriate disposal conventions; failing to dispose GDI+ objects can result in really nasty side effects for your app. (GDI+ objects in .NET correspond to their underlying Windows API equivalents.)
  2. APIs such as DirectX are extremely complex, and for good reason. They're designed not for simple shapes, but rather for complex, highly-performant and highly-interactive multimedia applications. (In other words, games, typically.) You can access DirectX through the Managed DirectX interfaces, but again, it's probably overkill for your direct purposes.
  3. If you are interested in an easier way to work with DirectX, XNA is the way to go. However, this is very much a gaming-specific library, and again is likely to be overkill. I'm a bit late to the party, but according to the comments below, this is no longer supported at all. (This makes sense; I haven't heard anything about it in years.)
Irrepealable answered 19/8, 2009 at 21:41 Comment(4)
"According to an email sent on 31 January 2013, XNA is no longer actively being developed,and it is not supported under the new "Metro interface" layers of Windows 8 nor on the Windows RT platform." Quote from WikipediaWatchcase
Maybe look at unity3d it has good tutorials and a free indie/personal licence. Or check this redditWatchcase
I don't recommend GDI+ nor System.Drawing I am here because FillRectangle is too slow, and it slowly calls underlying SafeNativeMethods GDI+ (Gdip) FillRectangle waay too slow to be useful. this is 2017, and I am fighting against a simple FillRectangle standard function, are you kidding me.Solatium
github.com/mono/sysdrawing-coregraphics is a port of System.Drawing on top of CoreGraphics for macOS.Hominy
R
15

Here's a simple code sample that will get you started (assumes you have a PictureBox named pictureBox1):

Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.DrawLine(new Pen(Color.Red), 0, 0, 10, 10);
}
pictureBox1.Image = bmp;

The graphics object has a bunch of other drawing methods, and Intellisense will show you how to call them.

Raddle answered 19/8, 2009 at 21:42 Comment(0)
V
3

Read about GDI, GDI+, System.Drawing namespace, for example here.
DirectX is not something you would use to draw simple shapes, rather render complicated 3D stuff, also, using DX Api under C# is a bit trickier (although not that hard).

Vevay answered 19/8, 2009 at 21:37 Comment(0)
W
1

Check out the System.Drawing namespace: http://msdn.microsoft.com/en-us/library/system.drawing.aspx

Whish answered 19/8, 2009 at 21:34 Comment(0)
C
1

The best way to implement 2D Graphics in C# Windows Forms (also VB.Net) is using CefSharp and Canvas API via JavaScript language. Canvas is way better and faster than clunky GDI+

Couturier answered 6/10, 2020 at 22:59 Comment(0)
F
0

Look at the System.Drawing Namespace

Feminize answered 19/8, 2009 at 21:35 Comment(0)
M
0

You need to use GDI+.

How you do it depends slightly on what you want to draw on. You can draw on a control or a form, or you can draw on an image object. Either way, you need a System.Drawing.Graphics object which I believe is located in System.Drawing.dll.

You can instantiate a new Bitmap class and call Graphics.FromImage(myImage), and then draw using the methods on the Graphics object you just created. If you want to draw on a form or control just override the OnPaint method and look for the Graphics property on the EventArgs class.

More information on System.Drawing namespace here: http://msdn.microsoft.com/en-us/library/system.drawing.aspx

Masonmasonic answered 19/8, 2009 at 21:36 Comment(2)
You can't instantiate a new Image, but you can instantiate a new Bitmap.Raddle
Ah yes, thanks for the clarification. I knew that, but forgot.Masonmasonic
F
0

Look for Managed Direct3D graphics API in .NET Source

Fatimafatimah answered 19/8, 2009 at 21:38 Comment(0)
F
0

GDI+ using System.Drawing

Frame answered 16/9, 2009 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.