简体   繁体   中英

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

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

This question gives an answer of how to display Stereoscopic images in c# using 3D but it utilises the Surface class. 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. When i encoutered this same problem i resorted to readying out the bytes from GDI to memory and then back to 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). Also, usage of *B*GRA texture format is intentional, otherwise colors are inverted.

If you need more performance, i suggest you look into 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.

I hope it helps others as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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