Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SharpDX代码中Texture2D.FromMemory()的异常_C#_Directx_Sharpdx - Fatal编程技术网

C# SharpDX代码中Texture2D.FromMemory()的异常

C# SharpDX代码中Texture2D.FromMemory()的异常,c#,directx,sharpdx,C#,Directx,Sharpdx,我正在学习DirectX和SharpDX(使用版本2.6.2) 现在,我尝试使用Texture2D.FromMemory()方法从字节数组创建纹理。 我的示例代码如下 using System; using System.Diagnostics; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using SharpDX;

我正在学习DirectX和SharpDX(使用版本2.6.2)

现在,我尝试使用Texture2D.FromMemory()方法从字节数组创建纹理。 我的示例代码如下

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
using Buffer = SharpDX.Direct3D11.Buffer;
using Device = SharpDX.Direct3D11.Device;
using MapFlags = SharpDX.Direct3D11.MapFlags;

namespace HLSLTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Form1 form = new Form1();
            form.Text = "D3DRendering - Test";
            form.Width = 640;
            form.Height = 480;

            Device device;
            SwapChain swapChain;

            var desc = new SwapChainDescription()
            {
                BufferCount = 1,
                ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed = true,
                OutputHandle = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };

            Device.CreateWithSwapChain(
                SharpDX.Direct3D.DriverType.Hardware,
                DeviceCreationFlags.None,
                new[] {
                    SharpDX.Direct3D.FeatureLevel.Level_11_0,
                    SharpDX.Direct3D.FeatureLevel.Level_10_1,
                    SharpDX.Direct3D.FeatureLevel.Level_10_0,
                },
                desc,
                out device,
                out swapChain
            );

            // It's Ok, no error
            //var texture = Texture2D.FromFile<Texture2D>(device, "GeneticaMortarlessBlocks.jpg");

            // "An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll"
            // "Additional information: HRESULT: [0x80004005], Module: [General], ApiCode: [E_FAIL/Unspecified error]"
            byte[] texArray = new byte[8];
            var texture = Texture2D.FromMemory(device, texArray);

            var textureView = new ShaderResourceView(device, texture);
        }
    }
}
我在网上搜索相同的问题或解决方案,但什么也找不到

请给我一些建议


谢谢,

这样的内存块无法创建纹理。
Texture2D.FromMemory
方法需要与
Texture2D.FromFile
支持的纹理相同的纹理,唯一的区别是它可以从内存读取而不是从磁盘读取

从原始内存块创建纹理需要通过
DataRectangle
结构从纹理的
texture2dddescription
和数据的内存区域创建新的
Texture2D()。此说明包括宽度、高度、像素格式、MIP数量、阵列数量等。等效的本机方法是


最后,像
Texture2D.FromFile/FromMemory
这样的D3DX函数使用相同的
ID3D11Device::CreateTexture2D
创建纹理。

我感谢您的帮助。我尝试用Texture2DDescription修改代码。
An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll
Additional information: HRESULT: [0x80004005], Module: [General], ApiCode:[E_FAIL/Unspecified error], Message: エラーを特定できません