在 C# 中渲染图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58230/
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
Rendering graphics in C#
提问by David McGraw
采纳答案by Cody Brocious
回答by Ash
Yes, I have written a Windows Forms control that wraps DirectX 9.0 and provides direct pixel level manipulation of the video surface.
是的,我编写了一个 Windows 窗体控件,它封装了 DirectX 9.0 并提供视频表面的直接像素级操作。
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?
实际上,我在 Stack Overflow 上写了另一篇文章,询问是否还有其他更好的方法:Unsafe C# 和用于 2D 渲染的指针,好还是坏?
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 Formsproject 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.
- 下载 DirectX SDK。
- 创建一个新的 C# Windows 窗体项目并引用已安装的 Microsoft DirectX 程序集。
- 使用您需要的演示参数(窗口化、后台缓冲等)初始化一个新的 DirectX 设备对象。
- 创建设备,注意记录表面“间距”和当前显示模式(每像素位数)。
- 当您需要显示某些内容时,
Lock
后台缓冲表面并将返回的指针存储到表面内存的开始处。 - 使用指针算法,根据表面间距、每像素位数和实际 x/y 像素坐标计算数据中的实际像素位置。
- 在我的情况下,为简单起见,我坚持 32 bpp,这意味着设置像素非常简单: *(surfacePointer + (y * pitch + x))=Color.FromARGB(255,0,0);
- 绘制完成后,
Unlock
后台缓冲面。呈现表面。 - 根据需要从第 5 步开始重复。
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.
请注意,采用这种方法时,您需要非常小心地检查目标表面的当前显示模式(间距和每像素位数)。此外,您还需要制定适当的策略来在程序运行时处理窗口大小调整或屏幕格式更改。
回答by tbreffni
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 Articlefor more info.
您可以尝试使用 Visual Studio 和/或 Expression Blend 来查看 WPF。我不确定你想变得多么复杂,但它应该能够处理一个简单的编辑器。查看此MSDN 文章了解更多信息。
回答by ima
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.
托管 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 屏幕,其中包含实时指标、图表和绘图。
回答by AngelBlaZe
You might look into the Cairo graphics library. The Monoproject has bindings for C#.