The .Net "pack URIs" feature seems ideally suited to my C#
application, but I cannot make it work. I hope someone can help.
I have some images (GIF and JPG), which I have built into my library's
DLL, by setting Build Action to Embedded Resource. Using
Assembly.GetManifestResourceNames(), I have verified that they're
there, at run-time.
I have a string containing some generated XML text, representing a
XAML FlowDocument. Within the FlowDocument, there are some <Image>
elements, with Source attributes. These attributes need to point to
the images that I embedded in the DLL.
It seems to me that it should be easy to use pack URIs to do this. I
have verified that Assembly.GetExecutingAssembly() gives the assembly
that contains the images, so I should be able to use either a simple
relative URI (e.g. "/mySubdir/myImage.gif") or a not-too-hard absolute
URI (e.g. "pack://application:,,,/mySubdir/myImage.gif").
If I try either of the above, my program crashes with Access
Violation. As it's a C# managed application, that shouldn't be
possible, should it?
If I try explicitly asking for my particular assembly (e.g. "pack://
application:,,,/MyAssembly;component/mySubdir/myImage.gif"), it
doesn't crash, but it doesn't load the image either. When the
FlowDocument is rendered, there's a blank space where the image should
be.
The problem is definitely to do with the pack URIs, as I can get a
FlowDocument with images, simply by using "file:" URIs. That type of
URI isn't any good for the production system, though, because it ties
the FlowDocument to some arbitrary file location on my PC.