Automatic map generating using data (C#) [duplicate] - c#

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

Related

PdfContentByte.createGraphicsShapes(...) in iTextSharp?

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.

GDI+ and system graphics cards

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.

How would one go about creating one’s own graphics effect in WPF?

I have an Image object in my application which the user can drag around. The object displays an image which is partly transparent, so the window background (which is itself a bitmap) can be seen through it.
I want to add a graphics effect to this object. Assume that I already have an algorithm for this effect — that’s not the issue. The issue is how to get this algorithm into WPF.
So I tried to look at how DropShadowEffect works, but the implementation displayed in Reflector is empty. I also tried to look at what methods from the abstract classes Effect and ShaderEffect I should override and there doesn’t seem to be anything related to actually rendering an effect.
So how do I create my own effect?
The best and fastest way is to use pixel shaders (supported starting with WPF 3.5 SP1 I think) . It will require some shader language (HLSL) knowledge, though :-)
Here is a tutorial: How Do I: Create Custom Pixel Shader Effects for WPF
a library on codeplex: Windows Presentation Foundation Pixel Shader Effects Library
an article with .NET 4 information (including Sliverlight support which has it too): SilverShader – Introduction to Silverlight and WPF Pixel Shaders
A very cool tool (and resource) is Shazzam it will help you to create the effects and it contains a nice tutorial.

WPF equivalent to GDI+'s ColorMatrix

I noticed that WPF has the System.Windows.Media(.Imaging) namespaces that contain a lot of the same functionality as System.Drawing(.Imagine), but I don't see an equivalent to the ColorMatrix in GDI+. I actually don't see a way of doing color transformations in general. Does one exist? I'm happy using GDI+ but was just curious.
WPF allows you to write custom pixel shaders. These are more versatile than a ColorMatrix which you could replicate as a pixel shader, plus they are executed on the GPU. Shazzam ships with some samples and simplifies the process of writing them.
Just to note: WPF is more suited for creating user interfaces than image editing, there's not much more than the members of System.Windows.Media.Imaging like WriteableBitmap and RenderTargetBitmap. If you are after bitmap image editing features gdi+ and Direct2D might be more appropriate.
Maybe http://msdn2.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap.aspx?
Edit: http://msdn.microsoft.com/en-us/library/system.windows.media.effects.pixelshader.aspx, but i think it is not best answer because you must know about HLSL.

Advanced drawing library for C#

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.

Categories