Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering graphics in C#

Is there another way to render graphics in C# beyond GDI+ and XNA?

(For the development of a tile map editor.)

like image 524
David McGraw Avatar asked Sep 12 '08 02:09

David McGraw


People also ask

What is rendering in programming?

Rendering or image synthesis is the process of generating a photorealistic or non-photorealistic image from a 2D or 3D model by means of a computer program. The resulting image is referred to as the render.

How do I get graphics in C++?

Graphics in C++ is defined to create a graphic model like creating different shapes and adding colors to it. It can be done in the C++ console by importing graphics. h library to GCC compiler. We can draw the circle, line, eclipse, and other geometric shapes too.

What is rendering for?

Rendering refers to the process of applying a coat of cement on the external walls of a property to make them smooth or textured as desired. The difference between rendering and plastering is that rendering involves the exterior walls while plastering involves the interior ones.

What is OpenGL programming?

OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardware-accelerated rendering.


2 Answers

SDL.NET is the solution I've come to love. If you need 3D on top of it, you can use Tao.OpenGL to render inside it. It's fast, industry standard (SDL, that is), and cross-platform.

like image 193
Serafina Brocious Avatar answered Nov 07 '22 14:11

Serafina Brocious


Yes, I have written a Windows Forms control that wraps DirectX 9.0 and provides direct pixel level manipulation of the video surface.

I actually wrote another post on Stack Overflow asking if there are other better approaches: Unsafe C# and pointers for 2D rendering, good or bad?

While it is relatively high performance, it requires the unsafe compiler option as it uses pointers to access the memory efficiently. Hence the reason for this earlier post.

This is a high level of the required steps:

  1. Download the DirectX SDK.
  2. Create a new C# Windows Forms project and reference the installed Microsoft DirectX assembly.
  3. Initialize a new DirectX Device object with Presentation Parameters (windowed, back buffering, etc.) you require.
  4. Create the Device, taking care to record the surface "Pitch" and current display mode (bits per pixel).
  5. When you need to display something, Lock the backbuffer surface and store the returned pointer to the start of surface memory.
  6. Use pointer arithmetic, calculate the actual pixel position in the data based on the surface pitch, bits per pixel and the actual x/y pixel coordinate.
  7. In my case for simplicity I am sticking to 32 bpp, meaning setting a pixel is as simple as: *(surfacePointer + (y * pitch + x))=Color.FromARGB(255,0,0);
  8. When finished drawing, Unlock the back buffer surface. Present the surface.
  9. Repeat from step 5 as required.

Be aware that taking this approach you need to be very careful about checking the current display mode (pitch and bits per pxiel) of the target surface. Also you will need to have a strategy in place to deal with window resizing or changes of screen format while your program is running.

like image 30
Ash Avatar answered Nov 07 '22 14:11

Ash