Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw simple graphics in C#?

Tags:

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.

like image 243
msvcyc Avatar asked Aug 19 '09 21:08

msvcyc


People also ask

Can you make graphics in C?

C Graphics programming is very easy and interesting. You can use graphics programming for developing your games, in making projects, for animation etc. It's not like traditional C programming in which you have to apply complex logic in your program and then you end up with a lot of errors and warnings in your program.

How do you draw a graphic circle in C?

The header file graphics. h contains circle() function which draws a circle with center at (x, y) and given radius. Syntax : circle(x, y, radius); where, (x, y) is center of the circle.


2 Answers

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.)
like image 85
John Rudy Avatar answered Oct 17 '22 01:10

John Rudy


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.

like image 41
MusiGenesis Avatar answered Oct 17 '22 01:10

MusiGenesis