如何在 C# 中绘制简单的图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:54:32  来源:igfitidea点击:

How do I draw simple graphics in C#?

c#.netgraphicsdirectx2d

提问by msvcyc

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.

我只想在 C# 中绘制简单的 2D 对象,如圆、线、正方形等。我怎么做?回到 Turbo C++ 时代,我记得初始化一些图形库来做同样的事情。我需要在 .NET 中做类似的事情吗?3D 对象有什么不同吗?DirectX 之类的东西会让这更容易吗?任何指向教程或示例的链接都非常感谢。

采纳答案by John Rudy

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.

正如其他人所说,请查看System.Drawing。(我只是为了完整性而重复这一点。) System.Drawing向您的应用程序公开GDI+Windows 绘图库。

A good tutorial to get you jump-started with System.Drawing and GDI+ can be found at C# Corner.

可以在C# Corner找到一个很好的教程,让您快速开始使用 System.Drawing 和 GDI+ 。

Some important items to note:

一些需要注意的重要事项:

  1. Many GDI+ objects implement the IDisposable interface, and therefore should be wrapped in usingblocks. 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, XNAis 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.)
  1. 许多 GDI+ 对象实现 IDisposable 接口,因此应该包装在using块中。确保您遵循适当的处置惯例;未能处理 GDI+ 对象可能会给您的应用程序带来非常糟糕的副作用。(.NET 中的 GDI+ 对象对应于它们的底层 Windows API 等价物。)
  2. DirectX 等 API 非常复杂,这是有充分理由的。它们不是为简单的形状而设计的,而是为复杂的、高性能和高度交互的多媒体应用程序设计的。(换句话说,通常是游戏。)您可以通过托管 DirectX 接口访问 DirectX,但同样,对于您的直接目的来说,这可能有点过头了。
  3. 如果您对使用 DirectX 的更简单方法感兴趣,XNA是您的最佳选择。然而,这在很大程度上是一个特定于游戏的库,而且很可能是矫枉过正。我参加聚会有点晚了,但根据下面的评论,这根本不再受支持。(这是有道理的;我已经好几年没有听到任何关于它的消息了。)

回答by Keith

Check out the System.Drawing namespace: http://msdn.microsoft.com/en-us/library/system.drawing.aspx

查看 System.Drawing 命名空间:http: //msdn.microsoft.com/en-us/library/system.drawing.aspx

回答by Kevin LaBranche

Look at the System.Drawing Namespace

查看 System.Drawing 命名空间

回答by Max Schmeling

You need to use GDI+.

您需要使用 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.

你怎么做稍微取决于你想画什么。您可以在控件或窗体上绘图,也可以在图像对象上绘图。无论哪种方式,您都需要一个 System.Drawing.Graphics 对象,我认为它位于 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.

您可以实例化一个新的 Bitmap 类并调用 Graphics.FromImage(myImage),然后使用您刚刚创建的 Graphics 对象上的方法进行绘制。如果您想在窗体或控件上绘图,只需覆盖 OnPaint 方法并在 EventArgs 类上查找 Graphics 属性。

More information on System.Drawing namespace here: http://msdn.microsoft.com/en-us/library/system.drawing.aspx

有关 System.Drawing 命名空间的更多信息,请访问:http: //msdn.microsoft.com/en-us/library/system.drawing.aspx

回答by Marcin Deptu?a

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).

阅读 GDI、GDI+、System.Drawing 命名空间,例如这里
DirectX 不是你用来绘制简单形状的东西,而是渲染复杂的 3D 东西,而且,在 C# 下使用 DX Api 有点棘手(虽然不是那么难)。

回答by Myra

Look for Managed Direct3D graphics APIin .NET Source

在 .NET 源中查找托管 Direct3D 图形 API

回答by MusiGenesis

Here's a simple code sample that will get you started (assumes you have a PictureBox named pictureBox1):

下面是一个简单的代码示例,可以帮助您入门(假设您有一个名为 pictureBox1 的 PictureBox):

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.

图形对象有一堆其他的绘图方法,Intellisense 会告诉你如何调用它们。

回答by amy

GDI+ using System.Drawing

GDI+ 使用 System.Drawing