[英]Displaying/implementing a System.Graphics.DrawEllipse as a DirectX 3D Surface?! c#
目前,我已經開發了一個使用System.Graphics.DrawEllipse繪制多個橢圓的應用程序,該程序在c#中效果很好。
現在,我想將其集成起來,以便通過為每只眼睛提供不同的圖像來使用立體成像(3D)將某些橢圓顯示給不同的眼睛。我安裝了DirectX SDK和SharpDX,並希望使用生成的ellipse(2D)進行顯示使用NVIDIA 3D和快門眼鏡以立體聲/ 3D方式播放。
這個問題給出了如何使用3D在c#中顯示立體圖像的答案,但它利用了Surface類。 我在互聯網上搜索了很多內容,但是找不到繪制形狀的方法或使用已經繪制的形狀而不是圖像(位圖)。
任何幫助表示贊賞。 謝謝。
在DirectX和GDI之間沒有直接的交互方式。 當我遇到同樣的問題時,我不得不將字節從GDI准備到內存,然后再回到direct3D。 我在下面添加了我的代碼,因為我認為我的項目中正在使用它,所以它應該可以工作:)
請注意,這是針對directx11的(應該易於轉換)。 另外,* B * GRA紋理格式的使用是有意的,否則顏色會反轉。
如果您需要更高的性能,我建議您研究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);
}
我的工作方式是將每個創建的橢圓(通過圖形)保存在位圖中,將每個位圖添加到位圖列表中,然后將這些位圖加載到Direct3D曲面列表中,然后再通過索引訪問我想要的任何曲面。
我希望它也對其他人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.