在 C# 中渲染图形

发布于 2024-07-04 08:07:15 字数 277 浏览 9 评论 0 原文

除了 GDI+XNA

(用于开发瓦片地图编辑器。)

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

(For the development of a tile map editor.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失与倦" 2024-07-11 08:07:15

SDL.NET 是我喜欢的解决方案。 如果您需要在其之上进行 3D,您可以使用Tao.OpenGL 在其内部进行渲染。 它速度快、符合行业标准(SDL)且跨平台。

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.

饮湿 2024-07-11 08:07:15

是的,我已经编写了一个 Windows 窗体控件,它封装了 DirectX 9.0 并提供了视频表面的直接像素级操作。

实际上,我在 Stack Overflow 上写了另一篇文章,询问是否还有其他更好的方法:不安全的 C# 和用于 2D 渲染的指针,好还是坏?

虽然它的性能相对较高,但它需要不安全的编译器选项,因为它使用指针来有效地访问内存。 这就是之前这篇文章的原因。

这是所需步骤的高级步骤:

  1. 下载 DirectX SDK。
  2. 创建一个新的 C# Windows 窗体 项目并引用已安装的
    微软 DirectX 程序集。
  3. 使用演示参数初始化新的 DirectX 设备对象
    (窗口、后台缓冲等)您需要的。
  4. 创建设备,注意记录表面“俯仰”和
    当前显示模式(每像素位数)。
  5. 当您需要显示某些内容时,锁定后台缓冲区
    Surface 并将返回的指针存储到 Surface 的开头
    记忆。
  6. 使用指针运算,计算出实际像素位置
    基于表面间距的数据,
    每像素位数和实际 x/y 像素坐标。
  7. 在我的例子中,为了简单起见,我坚持使用 32 bpp,这意味着设置像素就像这样简单: *(surfacePointer + (y *itch + x))=Color.FromARGB(255,0,0);
  8. 完成绘制后,解锁后台缓冲区表面。 呈现表面。
  9. 根据需要重复步骤 5。

请注意,采用这种方法时,您需要非常小心地检查目标表面的当前显示模式(每像素的间距和位数)。 此外,您还需要制定一个策略来处理程序运行时窗口大小调整或屏幕格式更改。

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.

离线来电— 2024-07-11 08:07:15
  • 托管 DirectX(Microsoft.DirectX 命名空间)可实现更快的 3D 图形。 它是 DirectX API 上的可靠 .NET 包装器,但在创建 .NET 对象和编组方面会造成一些性能损失。 除非您正在编写功能齐全的现代 3D 引擎,否则它将正常工作。

  • Window Presentation Foundation (WPF)(Windows.Media 命名空间)- 2D 图形的最佳选择。 3D 能力也有限。 旨在用矢量、硬件加速的独立于分辨率的框架取代 Windows 窗体。 非常方便,支持多种类型的自定义控件、资源、数据绑定、事件和命令...还有一些 WTF。 速度通常比 GDI 快,比 DirectX 慢,并且很大程度上取决于您的操作方式(以合理的方式重写后,某些东西的工作速度提高了 60 倍)。 我们成功地在一台(不是最好的)PC 上实现了 3 个 1280x1024 屏幕,其中充满了实时指示器、图表和绘图。

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

分開簡單 2024-07-11 08:07:15

您可以查看开罗图形库Mono 项目具有 C# 绑定。

You might look into the Cairo graphics library. The Mono project has bindings for C#.

り繁华旳梦境 2024-07-11 08:07:15

您可以尝试使用 Visual Studio 和/或 Expression Blend 查看 WPF。 我不确定你想要变得多么复杂,但它应该能够处理一个简单的编辑器。 请查看这篇 MSDN 文章了解更多信息。

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.

忆离笙 2024-07-11 08:07:15

开罗是一个选项。 我目前正在使用 GDI+ 和 Cairo 重写我的地图软件。 除其他功能外,它还具有瓷砖地图生成器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文