EindacorDS
EindacorDS

Reputation: 107

How do I create a PNG-formatted byte array from an ID3D11Texture2D without saving/loading a file?

I have an ID3D11Texture2D. The end goal is to take the image and embed it into html as a base64 encoded string (requirements are based on existing functionality of which I have no control). I can achieve this by saving the texture to file using DirectX::SaveWICTextureToFile, loading the texture as a byte array, then encoding the array in base64, but I'd like to do it without saving to a file if possible.

Ideally there would be some kind of SaveWICTextureToMemory function that can take the container format as a parameter, but so far I haven't found anything that deals with the format. I've checked out CreateWICTextureFromMemory and other functions in the WICTextureLoader package, but I didn't find exactly what I'm looking for (perhaps I missed it). Figuring SaveWICTextureToFile must generate a byte array to write to file, I tried to essentially unpack that function and strip away the file creation elements of it, but decided there was likely a simpler solution, which brought me here.

I don't have much code to look at, as I've only stubbed out many of the key functions, but this is generally where this stuff needs to live:

HRESULT ExportImages::GetAngleEncodedString(std::string& encoded, const DirectX::XMMATRIX& projectionMatrix, float radians) const
{
   // Draws geometry to m_Texture2D
   DrawAngle(projectionMatrix, radians);

   unsigned const char* pngBytes = GetPNGBytes(m_Texture2D);

   try
   {
      encoded = Base64Encode(pngBytes);
   }
   catch (...)
   {
      // TODO don't catch all, add resolution
      return HRESULT(-1);
   }

   return S_OK;
}

unsigned const char* ExportImages::GetPNGBytes(ID3D11Texture2D* texture) const
{
   // ???
}

Upvotes: 0

Views: 791

Answers (0)

Related Questions