【问题标题】:Drawing a 2D rectangle with SharpDX使用 SharpDX 绘制二维矩形
【发布时间】:2015-07-05 12:07:35
【问题描述】:

如何用 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;
    }
    

【问题讨论】:

  • 您不需要 Direct3D11 GPU 也能使用它。 Direct3D 有一个叫做 Feature Levels 的东西,这意味着它将运行 D3D11,但只有 GPU 可用的功能。因此,除非您运行的是 Windows XP,否则您将能够运行它。如果需要,请打开 Run... 并输入 dxdiag。然后寻找DirectX version。如果是 DirectX 11 或更高版本,那么您就可以开始了。
  • @LHLaurini 问题是我有一个 SharpDX.Direct3D9.Device 可供我从库中使用,我该如何使用它? (我没有 SharpDX.Direct3D11.Device)
  • 对不起,我擅长将 DirectX 与本机代码一起使用。我不太喜欢管理方面。所以其他人将不得不回答你。您是否尝试运行该 DX11 代码?抱歉,说到 C#,我很笨。

标签: c# directx 2d directx-9 sharpdx


【解决方案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();

以这种方式加载纹理将创建一个方形纹理,如果设备功能支持它,那么您可以在加载纹理时指定尺寸,例如

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;

【讨论】:

  • 非常感谢!这段代码主要工作,除了有一个奇怪的问题,它把图像拉伸得更高(高度与宽度相似,即使源图像不是正方形) - 请参阅github.com/spazzarama/Direct3DHook/issues/27
  • 更新为包含缩放(假设图像在任何维度上都不大于 1024)
  • 大小的另一个选项,是在加载纹理时指定它(假设设备功能支持非方形/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);
  • 在一台设备上尝试了第二个选项,并且效果很好。再次感谢!
猜你喜欢
  • 2023-03-26
  • 2011-04-05
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 2011-02-17
相关资源
最近更新 更多