简体   繁体   English

将System.Graphics.DrawEllipse显示/实现为DirectX 3D曲面? C#

[英]Displaying/implementing a System.Graphics.DrawEllipse as a DirectX 3D Surface?! c#

I have developed currently an application that draws several ellipses using System.Graphics.DrawEllipse which works fine in c#. 目前,我已经开发了一个使用System.Graphics.DrawEllipse绘制多个椭圆的应用程序,该程序在c#中效果很好。

Now I want to integrate this in order to display certain ellipses to different eyes using stereo imagining (3D) by providing each eye with a different image.I installed DirectX SDK and SharpDX and I want to use the produced ellipse(2D) and display it in a stereo/3D way using NVIDIA 3D and shutter glasses.. 现在,我想将其集成起来,以便通过为每只眼睛提供不同的图像来使用立体成像(3D)将某些椭圆显示给不同的眼睛。我安装了DirectX SDK和SharpDX,并希望使用生成的ellipse(2D)进行显示使用NVIDIA 3D和快门眼镜以立体声/ 3D方式播放。

This question gives an answer of how to display Stereoscopic images in c# using 3D but it utilises the Surface class. 这个问题给出了如何使用3D在c#中显示立体图像的答案,但它利用了Surface类。 I search a lot on the internet but couldn't find a way of drawing a shape or use the already drawn shape instead of an image (bitmap). 我在互联网上搜索了很多内容,但是找不到绘制形状的方法或使用已经绘制的形状而不是图像(位图)。

Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

There is no direct way to interact between directx and GDI. 在DirectX和GDI之间没有直接的交互方式。 When i encoutered this same problem i resorted to readying out the bytes from GDI to memory and then back to direct3D. 当我遇到同样的问题时,我不得不将字节从GDI准备到内存,然后再回到direct3D。 I added my code below, it should work since i think it is being used in my project :) 我在下面添加了我的代码,因为我认为我的项目中正在使用它,所以它应该可以工作:)

Note this is for directx11 (should be easily converted). 请注意,这是针对directx11的(应该易于转换)。 Also, usage of *B*GRA texture format is intentional, otherwise colors are inverted. 另外,* B * GRA纹理格式的使用是有意的,否则颜色会反转。

If you need more performance, i suggest you look into DirectDraw. 如果您需要更高的性能,我建议您研究DirectDraw。

    private byte[] getBitmapRawBytes(Bitmap bmp)
{
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData =
        bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

    // Get the address of the first line.
    IntPtr ptr = bmpData.Scan0;

    // Declare an array to hold the bytes of the bitmap.
    int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
    byte[] rgbValues = new byte[bytes];

    // Copy the RGB values into the array.
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

    // Unlock the bits.
    bmp.UnlockBits(bmpData);
    return rgbValues;
}


/// <summary>
/// The bitmap and the texture should be same size.
/// The Texture format should be B8G8R8A8_UNorm
/// Bitmap pixelformat is read as PixelFormat.Format32bppArgb, so if this is the native format maybe speed is higher?
/// </summary>
/// <param name="bmp"></param>
/// <param name="tex"></param>
public void WriteBitmapToTexture(Bitmap bmp, GPUTexture tex)
{
    System.Diagnostics.Debug.Assert(tex.Resource.Description.Format == Format.B8G8R8A8_UNorm);

    var bytes = getBitmapRawBytes(bmp);
    tex.SetTextureRawData(bytes);

}

The way I made it work was to save each created Ellipse ( through Graphics) in a bitmap, add each bitmap in a Bitmap List and load those bitmaps in a Direct3D Surface List and afterwards access whichever surface I want by index. 我的工作方式是将每个创建的椭圆(通过图形)保存在位图中,将每个位图添加到位图列表中,然后将这些位图加载到Direct3D曲面列表中,然后再通过索引访问我想要的任何曲面。

I hope it helps others as well. 我希望它也对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM