使用 C# 将 DirectX 3D 图形直接渲染到图像上
我正在进行的项目需要能够变换图像的 4 个角中的任何一个角。遗憾的是,GDI+ 不具备此功能,因此我们转而使用 DirectX 的 3D 图形。
虽然我有一个方形网格,其纹理在屏幕上成功显示,但我需要能够从该渲染中生成图像,并将背景设置为透明。有没有一种方法可以有效地实现这一目标?
最好,我想在不使用控件来初始化设备的情况下执行此操作。或者,我不介意创建一个自定义的、不可见的控件来为我生成图像。
编辑:
实际上,我刚刚意识到透明背景是严格没有必要的,但它会对我们项目的性能有所帮助。
无论如何,我很幸运地做了这样的事情,但速度太慢了。也许有更好的方法?
// Create a surface to render an image to
Surface mSurface = mDevice.GetRenderTarget(0);
// Render the visualization
mDevice.Clear(ClearFlags.Target, Color.Transparent, 1.0f, 0);
mDevice.BeginScene();
/* Do some amazing stuff */
// Exit rendering
mDevice.EndScene();
mDevice.Present();
// Render the bitmap images
GraphicsStream mGraphics = SurfaceLoader.SaveToStream(
ImageFileFormat.Bmp, mSurface);
Image mImage = new Bitmap(mGraphics, false);
The project I'm working on requires the ability to transform any of the 4 corners of an image. As GDI+ unfortunately doesn't have this capability, we're resorting into using DirectX's 3D graphics.
While I have a square mesh with a texture showing successfully on-screen, I need to be able to generate an image from this rendering, with the background set to transparent. Is there a way to efficiently achieve this?
Preferably, I'd like to do this without using a Control for initializing a device. Alternatively, I don't mind the option of creating a custom, invisible Control that will generates an image for me.
Edit:
Actually, I just realized a transparent background is strictly not necessary, but it would help the performance of our project a bit.
Anyway, I've had some luck doing something like this, but it is excessively slow. Perhaps there's a better method?
// Create a surface to render an image to
Surface mSurface = mDevice.GetRenderTarget(0);
// Render the visualization
mDevice.Clear(ClearFlags.Target, Color.Transparent, 1.0f, 0);
mDevice.BeginScene();
/* Do some amazing stuff */
// Exit rendering
mDevice.EndScene();
mDevice.Present();
// Render the bitmap images
GraphicsStream mGraphics = SurfaceLoader.SaveToStream(
ImageFileFormat.Bmp, mSurface);
Image mImage = new Bitmap(mGraphics, false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 D3D 进行最终渲染到屏幕,那么您可以使用渲染到纹理轻松完成您正在讨论的事情。
Well if you use D3D for doing the final rendering to screen then you can easily do the things you are talking about using render-to-texture.