I am trying to adapt the Direct 2D SVG sample at https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/D2DSvgImage to draw SVG string instead of from file. All I did was replacing the code to open a stream from an Assets file. Here is the relevant excerpt:
void D2DSvgImageRenderer::CreateDeviceDependentResources()
{
auto d2dContext = m_deviceResources->GetD2DDeviceContext();
StorageFolder^ packageFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
// Retrieve the SVG file from the app package.
// ORIGINAL create_task(packageFolder->GetFileAsync("Assets\\drawing.svg")).then([=](StorageFile^ file)
create_task([]()
{
// Open the SVG file for reading.
// ORIGINAL return file->OpenAsync(FileAccessMode::Read);
char *svg = "<svg><circle r=\"300\" cy=\"509\" cx=\"370\" style=\"fill:#ffff00;stroke:#000000;stroke-width:5\"/></svg>";
auto stream = ref new Windows::Storage::Streams::InMemoryRandomAccessStream();
auto writer = ref new Windows::Storage::Streams::DataWriter(stream);
auto p = svg;
while (*p != '\0')
{
writer->WriteByte((unsigned char)*p);
p++;
}
create_task(writer->StoreAsync()).get();
create_task(writer->FlushAsync()).get();
return stream;
}).then([=](IRandomAccessStream^ stream)
{
// Wrap the WinRT stream with a COM stream.
ComPtr<IStream> iStream;
DX::ThrowIfFailed(
CreateStreamOverRandomAccessStream(
stream,
IID_PPV_ARGS(&iStream)
)
);
// Parse the file stream into an SVG document.
DX::ThrowIfFailed(
d2dContext->CreateSvgDocument(
iStream.Get(),
D2D1::SizeF(sc_svgSize, sc_svgSize), // Create the document at a size of 500x500 DIPs.
&m_svgDocument
)
);
});
}
But I am always getting the inexplicable The parameter is incorrect exception and there is no way to debug as to why it is the case. (DirectX does NOT come with any way to debug or provide any hint why and which parameter is incorrect!?)
My educated guess is that the API never tell us the hidden fact that using InMemoryRandomAccessStream in CreateSvgDocument is strictly prohibited. How can I fix this?
writer.StoreAsync()and thenwriter.FlushAsync()as described inInMemoryRandomAccessStreamClass usage example. - user7860670iStreamcreated before theCreateSvgDocumentperfectly: the content is actually there. Just to be sure, I have just tested that even with thoseStoreAsyncandFlushAsync, it still crashes. - An Hoa