0

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
{
   // ???
}
EindacorDS
  • 107
  • 1
  • 3
  • 11
  • 1
    SaveWICTextureToFile source code is here https://github.com/microsoft/DirectXTK/blob/8658fbf943d1217659554906693ce3fbfa8399d0/Src/ScreenGrab.cpp#L347 instead of using `stream->InitializeFromFilename` you can do `stream->InitializeFromIStream` with your own `IStream` created from `CreateStreamOnHGlobal` for example – Simon Mourier May 31 '19 at 15:54
  • Thanks @SimonMourier, that's actually what I was hinting at when I said I started to unpack `SaveWICTextureToFile`, but wondered if there was already a module or something that did that for me so I wouldn't have to copy/paste a bunch of MS code. If all else fails I'll give that a shot, thanks again. – EindacorDS May 31 '19 at 16:56

0 Answers0