From my research I have seen that GDI+ (if i am wrong correct me please) is used by Graphics to draw various object, but also to my knowledge I am unsure if it utilizes the systems graphics card? If it does not could someone please lead me to a way that I can tell it to utilize the graphics cards many features, like the shader etc, or at-least how to code my own DirectX, OpenGL engine?
Thanks in advance.
P.S. The main thing I wish to complete is to draw a pixel by pixel representation of various object.
No, I'm sorry. GDI+ does is not hardware-accelerated, and hence does not use graphics cards (although GDI is hardware-accelerated in windows 7). I suggest you use OpenTK.
GDI+ and DirectX are two alternative libraries used to render graphics on your display on top of the windows operating system. Since, they are just interfaces to the OS, none of them can directly "talk" to your graphics card at the hardware level. All GDI+ functionality used to draw graphics such as your WinForms graphics, either directly or indirectly is just a call to the API in gdi32.dll system library. Similarly, DirectX functions too have their own API libraries to call.
It is often suggested that DirectX has much better performance in rendering 3D graphics, though in no way does it mean that GDI+ is lacking in any functionality or it doesn't make use your graphics card - they both do indirectly through windows. For comparison, all WinForms graphics in .NET are based on GDI+ library, whereas WPF ones are based on DirectX.
Related
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.
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:
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.)
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.
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.)
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.
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).
Check out the System.Drawing namespace: http://msdn.microsoft.com/en-us/library/system.drawing.aspx
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+
Look at the System.Drawing Namespace
Look for Managed Direct3D graphics API in .NET
Source
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
GDI+
using System.Drawing
How can I get the C# winform graphics system from iTextSharp?
So that I can use DrawString(....) to draw text directly onto the existed PDF.
Unfortunately this isn't possible right now. iText, the Java-based library, subclassed the system class java.awt.Graphics2D which is a further subclass of java.awt.Graphics. This allowed them to bridge Sun/Oracle's drawing paradigms with their own. iTextSharp was ported from Java to C# but for whatever reason the system Graphics bridging code was not ported.
If I were to guess Java and .Net's version of Graphics were too dissimilar and there just wasn't enough community desire to bother with it. Also, .Net's implementation has extra abstractions like Pen and Brush so this would have made the bridge code very different between the Java and .Net version. Not to mention methods like FillPie(), MeasureString(), etc.
Right now your two main options are to either just manually draw to the PdfContentByte object which, once you get used to the inverted axis (Adobe's fault, not iText), goes pretty easy, or you can draw to a .Net image and then just embed it. If you do the latter, I'd recommend at least tripling the image resolution and then embedding it at a third of the size, effectively kicking up the DPI.
You can also try something like PDFsharp which, according to their site, has a very native feel for drawing.
I’m looking for an advanced noncommercial drawing library for C# with WPF.
My goal is to create a drawing application. Not vector graphic support is needed, only bitmap.
The most advanced non-commercial API's for drawing are going to be DirectX and OpenGL. They are both complex, but you will be able to make them do almost anything you would want. You could also roll your own design that uses an Image and just changes the various pixels manually. You can then draw the image to your window when changes occur. Here is a code example below.
http://msdn.microsoft.com/en-us/library/0t3sakh9.aspx
Is there a specific reason why you can't utilize GDI+? GDI+ is an object-oriented vector-based graphics library built into the .NET Framework. It can perform most trivial drawing tasks, including loading, drawing, and saving bitmaps (BMP, PNG, GIF, JPEG, and others), transforms, color matrix manipulation, and blending. It can also render primitives such as lines, rectangles, circles, and n-sided polygons.
It also has support for render operations, and alpha transparency. You can also draw primitives and other graphical constructs using brushes and patterns, like with GDI.
You can find more information about the classes available to you by looking at the System.Drawing namespace, where most of the GDI+ classes are contained:
http://msdn.microsoft.com/en-us/library/system.drawing.aspx
Your question is not specific, however Graphic Classes in C# has many methods and features to accomplish any Trivial and some non trivial task.. if you need any thing more than that then you have DirectX.
I have developed a quite large application using MFC. Naturaly, I used GDI for drawing, CCmdTarget for event routing, and the document-view architecture.
It was a convenient development path.
Now, the client is interested in converting this application to .Net.
I would prefer (and they too) writing the new product in C#.
The application displays and interacts with thousands of graphic objects, so
I figured going with GDI+, although seems natuaral, can cause performance issues,
So I am thinking of using OpenGL, specifically - OpenTK - as the graphics library (it's 2D).
I know that OpenGL works differently that these Windows APIs, which rely on Invalidation of portion of the screen. OpenGL has a rendering loop which constantly draws to the screen.
My question is:
Is this an acceptable way to go, thinking of:
performance - will the users need special graphics cards (hardware?). It is graphics intensive, but it's not a high-end game
printing and print preview - are these things complex to achienve?
multiple selection and context menus
Is this library goes well inside windows forms?
I don't think so. Use WPF if you can or DirectX if you can't.
I know it might not be fair but if I'm programming on .NET (microsoft) on windows (microsoft) I'd rather use DirectX ... which is also from microsoft.
As a side note: don't reinvent the wheel. Recoding user controls in open-gl can be very time consuming, if you do make sure you have a good reason.
In my experience developing CAD-like software, the benefits of OpenGL and DirectX are fast depth testing, smooth rotation and panning, lighting and powerful texture capabilities. Obviously there are other benefits but, despite what most tutorials would lead you to believe, implementing a rendering system using either of these APIs is a significant undertaking and should not be taken lightly.
Specifically:
If it is a 2D app and you already have it implemented in GDI then switching to GDI+ will be much easier. Additionally, on modern hardware, 2D GDI or GDI+ can be about as fast as 2D OpenGL or DirectX. And ultimately, the end-user probably won't notice the difference, especially with double buffered support in GDI+.
You do not need (and probably don't want) a continuous rendering loop for your app. In OpenGL and DirectX you can explicitly invalidate the window when your scene changes.
If you go with OpenGL or DirectX you will need to consider putting your objects into display lists or vertex arrays (buffers) for fast drawing. This is not difficult but managing objects in this way adds complexity to the system and will most likely significantly change the architecture of your rendering system.
Printing in either OpenGL or DirectX can also be tedious. On the one hand you can render to a bitmap and print that out. However, for high quality images you may want vectorized images instead, which are difficult to produce with either of these rendering frameworks.
I would also stay away from writing GUIs in OpenGL or DirectX...unless you're really looking for a challenge ;~)
Finally, and this is just an annoyance from an install perspective, the Managed DirectX run-time library that must be installed on the user's machine is around 100 MB.
I have no experience with C#, but I have once built a layer system for a drawing program that used openGL for rendering.
To make this layer I asked openGL for the current framebuffer and converted it to an image to use as a texture under the current canvas. So I guess from there its pretty easy to go to printing and print preview.
Direct X and Open GL much faster than GDI+.
You can also use an TAO framework as an alternative to OpenTK.
Is there another way to render graphics in C# beyond GDI+ and XNA?
(For the development of a tile map editor.)
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.
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:
Download the DirectX SDK.
Create a new C# Windows Forms project and reference the installed
Microsoft DirectX assembly.
Initialize a new DirectX Device object with Presentation Parameters
(windowed, back buffering, etc.) you require.
Create the Device, taking care to record the surface "Pitch" and
current display mode (bits per pixel).
When you need to display something, Lock the backbuffer
surface and store the returned pointer to the start of surface
memory.
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.
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);
When finished drawing, Unlock the back buffer surface. Present the surface.
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.
Managed DirectX (Microsoft.DirectX namespace) for faster 3D graphics. It's a solid .NET wrapper over DirectX API, which comes with a bit of performance hit for creating .NET objects and marshalling. Unless you are writing a full featured modern 3D engine, it will work fine.
Window Presentation Foundation (WPF) (Windows.Media namespace) - best choice for 2D graphics. Also has limited 3D abilities. Aimed to replace Windows Forms with vector, hardware accelerated resolution-independent framework. Very convenient, supports several flavours of custom controls, resources, data binding, events and commands... also has a few WTFs. Speed is usually faster than GDI and slower than DirectX, and depends greatly on how you do things (seen something to work 60 times faster after rewriting in a sensible way). We had a success implementing 3 1280x1024 screens full of real-time indicators, graphs and plots on a single (and not the best) PC.
You could try looking into WPF, using Visual Studio and/or Expression Blend. I'm not sure how sophisticated you're trying to get, but it should be able to handle a simple editor. Check out this MSDN Article for more info.
You might look into the Cairo graphics library. The Mono project has bindings for C#.
Cairo is an option. I'm currently rewriting my mapping software using both GDI+ and Cairo. It has a tile map generator, among other features.