PdfContentByte.createGraphicsShapes(...) in iTextSharp? - c#

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.

Related

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

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

Program architecture for Windows 8 C# & XAML app featuring graphics

If I'm looking to create a game that doesn't necessarily run full screen, but simply needs to feature 2D/3D graphics somewhere in a portion of the screen, what's my best approach?
Some specific questions could be:
What component would the rendered area use?
Are there any game libraries I could leverage for the rendered area?
What would be the most "pure" or "canonical" stack according to Microsoft to use here?
Omega --
Visual Studio 2010 and 2012 are Both WPF apps. WinRT is for Tablets / Mobile. WPF is certainly NOT outdated.
If I were you I wouldn't render everything out the way canvas forces you to, it might be a better approach to have the center item be a UI element named Frame, which is the base element for all UI related content in WPF.
In this way you would be able to leverage all of the possible types of controls in the Frame whether you decided that An ImageSourceType or Canvas is more applicable to a particular features of the game.
Depending on how you want to draw graphics, you could use (but are by no means limited to):
Canvas - which would be totally appropriate for slow moving games. This way you get the benefit of the various WPF layout routines and can define objects inside the scene in XAML/vectors as well.
WPF supports 3D graphics (using Direct3D on the backend) so you could probably set up an orthogonal projection matrix and treat it like a Direct3D context (with the WPF API). I don't have enough experience to know how slow this is compared to D3D, but it's certainly easier (built-in "scene graph" like support from the XAML architecture, for instance).
If you want to go whole-hog with Direct3D you could use SlimDX, which has a WPF shim that I've used in the past, as well as another third party control. There may be other libraries available as well.
Direct blitting to/from a Bitmap using WriteableBitmap (see WriteableBitmapEx for a third-party version with a much friendlier API) or similar.
There are probably a lot of other options too. My preference would be for using Canvas initially if it's a slow-paced game that doesn't need super-fast frame rates (the layout work does incur a fairly substantial overhead, but it's less work and may be easier to get looking exactly the way you want).
If you want absolute control and speed, use D3D through SlimDX, but this is a pretty hefty learning curve if you're new to it.

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.

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