使用PNG的DirectX11 CreateWICTextureFromMemory [英] DirectX11 CreateWICTextureFromMemory Using PNG

查看:473
本文介绍了使用PNG的DirectX11 CreateWICTextureFromMemory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用CreateWICTextureFromFile加载纹理,但是我想对其进行更多控制,并且我想以字节形式将图像存储在资源加载器中.以下是两套返回两个单独结果的测试代码,我正在寻找对可能解决方案的任何见解.

I've currently got textures loading using CreateWICTextureFromFile however I'd like a little more control over it, and I'd like to store images in their byte form in a resource loader. Below is just two sets of test code that return two separate results and I'm looking for any insight into a possible solution.

ID3D11ShaderResourceView* srv;

std::basic_ifstream<unsigned char> file("image.png", std::ios::binary);

file.seekg(0,std::ios::end);
int length = file.tellg();
file.seekg(0,std::ios::beg);

unsigned char* buffer = new unsigned char[length];
file.read(&buffer[0],length);

file.close();

HRESULT hr;
hr = DirectX::CreateWICTextureFromMemory(_D3D->GetDevice(), _D3D->GetDeviceContext(), &buffer[0], sizeof(buffer), nullptr, &srv, NULL);

作为上述代码的返回,我得到了找不到组件.

As a return for the above code I get Component not found.

std::ifstream file;
ID3D11ShaderResourceView* srv;

file.open("../Assets/Textures/osg.png", std::ios::binary);

file.seekg(0,std::ios::end);
int length = file.tellg();
file.seekg(0,std::ios::beg);

std::vector<char> buffer(length);
file.read(&buffer[0],length);

file.close();

HRESULT hr;
hr = DirectX::CreateWICTextureFromMemory(_D3D->GetDevice(), _D3D->GetDeviceContext(), (const uint8_t*)&buffer[0], sizeof(buffer), nullptr, &srv, NULL);

上面的代码返回图像格式未知.

The above code returns that the image format is unknown.

我在这里显然做错了,非常感谢您的帮助.试图在stackoverflow上找到任何甚至相似的内容,但Google都无济于事.

I'm clearly doing something wrong here, any help is greatly appreciated. Tried finding anything even similar on stackoverflow, and google to no avail.

推荐答案

希望有人尝试做同样的事情会找到这种解决方案.

Hopefully someone trying to do the same thing will find this solution.

下面是我用来解决此问题的代码.

Below is the code I used to solve this problem.

std::basic_ifstream<unsigned char> file("image.png", std::ios::binary);

if (file.is_open())
{
    file.seekg(0,std::ios::end);
    int length = file.tellg();
    file.seekg(0,std::ios::beg);

    unsigned char* buffer = new unsigned char[length];
    file.read(&buffer[0],length);
    file.close();

    HRESULT hr;
    hr = DirectX::CreateWICTextureFromMemory(_D3D->GetDevice(), _D3D->GetDeviceContext(), &buffer[0], (size_t)length, nullptr, &srv, NULL);
}

重要的更改是CreateWICTextureFromMemory

这确实是一个愚蠢的错误.

It was indeed a stupid error.

这篇关于使用PNG的DirectX11 CreateWICTextureFromMemory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆