possible to load WPF resources at runtime?

N

not_a_commie

I want to call a WPF method that requires a "resourceName" -- an image
that is embedded in the assembly as a resource. I want to send in an
image straight from disk rather than compiling it into the assembly.
Is there a way load my image into the resources of my current assembly
dynamically?
 
A

Andy O'Neill

not_a_commie said:
I want to call a WPF method that requires a "resourceName" -- an image
that is embedded in the assembly as a resource. I want to send in an
image straight from disk rather than compiling it into the assembly.
Is there a way load my image into the resources of my current assembly
dynamically?

Not sure I understand the question.

If you're calling a method then you can just load a file from somewhere.
The way you do that in wpf is kind of weird because you need to create a uri
StreamResourceInfo s = Application.GetResourceStream(new
Uri("yourpicturepath", UriKind.Absolute)):
Image i = new Image(s.Stream);
You can also use relative paths and pick em out other compiled resources
using the pack syntax.

I think that probably answers the question.
OTOH
Do you really mean that bit about resource literally.
You can just point an image source to an external URI and job's a good un.
<Image Source="your external uri" />
Or a binding expression.
You could have a piece of xaml pointing to this file and give it a name then
use it:
<Image Source="{DynamicResource thatname}"
and merge dictionaries.
You could build that xaml in code.
If two dictionaries have "thatname" in them then the latest one is used.
 
P

Peter Duniho

not_a_commie said:
I want to call a WPF method that requires a "resourceName" -- an image
that is embedded in the assembly as a resource. I want to send in an
image straight from disk rather than compiling it into the assembly.
Is there a way load my image into the resources of my current assembly
dynamically?

Instead of vaguely describing the method, why not tell us the exact name
of the method and why you're using it?

The way you've asked the question, the only answer is "no", you can't
dynamically add resources to your EXE. But if you provide specifics, it
is almost certain that there's a different, better way to approach the
problem.

Pete
 
N

not_a_commie

I was considering using the SplashScreen class. Look at its
constructor and tell me how to load a file from disk into that at
runtime.
 
P

Peter Duniho

not_a_commie said:
I was considering using the SplashScreen class. Look at its
constructor and tell me how to load a file from disk into that at
runtime.

You'll need to embed the image resource in a standalone DLL, which you
can then load dynamically as an Assembly instance. If you really need a
dynamically-created splash screen, you can use the compiler services in
..NET to do this on boot. Of course, creating a splash screen
dynamically on boot, using whatever technique, is IMHO not in keeping
with the whole point of a splash screen. But you can do it.

In general, you might have been able to use a file-based ResourceManager
(see ResourceManager.CreateFileBasedResourceManager()), but AFAIK
there's no way to tell the SplashScreen class to use an arbitrarily
chosen manager. I don't typically use the ResourceManager class
directly though, so it's possible I've overlooked some way of overriding
the default ResourceManager with a file-based one you've created explicitly.

Pete
 
A

Andy O'Neill

not_a_commie said:
I was considering using the SplashScreen class. Look at its
constructor and tell me how to load a file from disk into that at
runtime.

Fire up your web browser and head over to google.
Search on
splashscreen wpf.

"Stefan Olson's Blog : A better WPF splash screen" stook out for me.

You might well feel that creating a separate lightweight exe is overkill or
it might be exactly what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top