2015-06-28 275 views
0

如何用SharpDX繪製純紅色的2D矩形?用SharpDX繪製2D矩形

  • 我有一個SharpDX.Direct3D9.Device available from a library爲我設置了一個Direct3D v9設備,所以我希望能夠使用它?
  • Found a tutorial on how to use Direct2D1 to draw a basic rectangle,但代碼似乎是依賴於Direct3D11設備,這是我沒有 - 我需要能夠把工作沒有Direct3D11,沒有Direct3D10

    unsafe int PresentHook(IntPtr devicePtr, SharpDX.Rectangle* pSourceRect, SharpDX.Rectangle* pDestRect, IntPtr hDestWindowOverride, IntPtr pDirtyRegion) 
    { 
        _isUsingPresent = true; 
    
        SharpDX.Direct3D9.Device device = (SharpDX.Direct3D9.Device)devicePtr; 
    
        // How to draw rectangle here? 
    
        if (pSourceRect == null || *pSourceRect == SharpDX.Rectangle.Empty) 
         device.Present(); 
        else 
        { 
         if (hDestWindowOverride != IntPtr.Zero) 
          device.Present(*pSourceRect, *pDestRect, hDestWindowOverride); 
         else 
          device.Present(*pSourceRect, *pDestRect); 
        } 
        return SharpDX.Result.Ok.Code; 
    } 
    
+1

你並不需要一個Direct3D11 GPU才能夠使用它。 Direct3D具有一些稱爲「功能級別」的功能,這意味着它將運行D3D11,但只具有可用於GPU的功能。所以,除非你運行Windows XP,否則你可以運行它。如果你想,打開'Run ...'並輸入'dxdiag'。然後查找「DirectX版本」。如果它是DirectX 11或更多,那麼你很好。 – LHLaurini

+0

@LHLaurini的事情是我有一個SharpDX.Direct3D9.Device可供我從庫中使用,我該如何使用它? (我沒有SharpDX.Direct3D11.Device) – Cel

+0

對不起,我擅長使用DirectX和本地代碼。我對管理方面沒有太多的瞭解。所以別人就必須回答你。您是否嘗試運行該DX11代碼?對不起,當談到C#時,我很愚蠢。 – LHLaurini

回答

1

完成可以使用類似繪製一個精靈如下:

// Be sure to only initialise these only once (or as needed) 
// not every frame. 

//use relative path 
string dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
string filename = dir + @"\image.bmp"; 
var _mytext = SharpDX.Direct3D9.Texture.FromFile(device, filename); 
var _sprite = new SharpDX.Direct3D9.Sprite(device); 

float posLeft = 10f; 
float posTop = 10f; 
var pos = new SharpDX.Vector3(posLeft, posTop, 0); 
var color = new SharpDX.ColorBGRA(0xffffffff); 
_sprite.Begin(SharpDX.Direct3D9.SpriteFlags.AlphaBlend); 
_sprite.Draw(_myText, color, null, null, pos); 
_sprite.End(); 

加載紋理這種方式將創建一個方形紋理,如果設備的功能支持,那麼你可以指定dimensio ns加載紋理時,例如

var _myText = SharpDX.Direct3D9.Texture.FromFile(device, filename, imageSize.Width, imageSize.Height, 0, Usage.None, Format.A8B8G8R8, Pool.Default, Filter.Default, Filter.Default, 0); 

否則,你可以在變換適當縮放X和Y:

// Get image dimensions 
Size imageSize; 
using (var img = Image.FromFile(filename)) 
{ 
    imageSize = img.Size; 
} 
// Calculate scale to get correct image size 
var transform = SharpDX.Matrix.AffineTransformation2D(1f, 0f, Vector2.Zero); 
// Calculate width scale 
if (imageSize.Width <= 128) 
{ 
    transform.M11 = (float)imageSize.Width/128f; // scale x 
} 
else if (imageSize.Width <= 256) 
{ 
    transform.M11 = (float)imageSize.Width/256f; // scale x 
} 
else if (imageSize.Width <= 512) 
{ 
    transform.M11 = (float)imageSize.Width/512f; // scale x 
} 
else if (imageSize.Width <= 1024) 
{ 
    transform.M11 = (float)imageSize.Width/1024f; // scale x 
} 
// Calculate height scale 
if (imageSize.Height <= 128) 
{ 
    transform.M22 = (float)imageSize.Height/128f; // scale y 
} 
else if (imageSize.Height <= 256) 
{ 
    transform.M22 = (float)imageSize.Height/256f; // scale y 
} 
else if (imageSize.Height <= 512) 
{ 
    transform.M22 = (float)imageSize.Height/512f; // scale y 
} 
else if (imageSize.Height <= 1024) 
{ 
    transform.M22 = (float)imageSize.Height/1024f; // scale y 
} 

_sprite.Transform = transform; 
+0

非常感謝!此代碼主要工作,除了有一個奇怪的問題,因爲它拉伸圖像更高(與寬度相似的高度,即使源圖像不是一個正方形) - 請參閱https://github.com/spazzarama/Direct3DHook/issues/27 – Cel

+1

已更新爲包含縮放(假設任何維度上的圖像不大於1024) –

+1

尺寸的另一種選擇是在加載紋理時指定它(假設設備功能支持非方形/ pow2紋理): var _myText = SharpDX.Direct3D9.Texture.FromFile(device,filename,imageSize.Width,imageSize.Height,0,Usage.None,Format.A8B8G8R8,Pool.Default,Filter.Default,Filter.Default, 0); –