wpf Image resources and visual studio 2010 resource editor - wpf

My motivation for this question is really just to specify an image to be used in a user control via a dependency property for ImageSource. I'm hitting some pain points involving the management, access, and unit testing for this.
Is the resource editor a good tool to use to maintain images for the application?
What is the best way to translate the Bitmap from the editor to an ImageSource?
How can I grab the resource Filename from the editor?

No, the resource editor is not a good tool for this.
In a WPF application the best way is to put all of your images in an "Images" directory and mark each one as a "Resource". Then you can reference them directly in Image controls and elsewhere.
Here are the precise steps:
Crop and otherwise adjust your images using your favorite bitmap editing program (Paint.NET, Photoshop, etc)
Save them as .png files (or .jpg or .gif if you prefer)
Create an "Images" folder inside your Visual Studio solution (or multiple folders, however you want to organize it)
Drag the images from your hard disk into your "Images" folder (or right-click the project, select New -> Existing Item and select the images)
Now you can reference your images easily in XAML:
<Image Source="Images/MyImage.png" />
Or in code:
var source = (BitmapSource)Application.LoadComponent(
new Uri("Images/MyImage.png", UriKind.Relative));
You can also reference images in external assemblies:
<Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" />
Which in code would be:
var source = (BitmapSource)Application.LoadComponent(
new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png",
UriKind.Relative));

Related

Set default xaml window background image in dll

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

How do I share an image among WPF projects?

I have a solution with a number of WPF projects and UserControls in those. Among these is a Core project which contains resources shared by all the other projects and all. One of these is an image file, which I have had to copy to all the other projects to use a relative file path for the Source property of various ImageEdit controls.
How can I set things up so that the image file only exists in the Core project, and I can refer to it in XAML in all the other projects?
Reference the project from the project that you intend to consume the image, then use pack URI to access it.
Something like this:
<UserControl x:Class="MyProduct.MyModule.MyView"
.....
<Image Source="pack://application:,,,/MyProduct.Core;component/Images/MyImage.png"/>
Build action for images should be "Resource".
Add the image to your Core project, then add as linked item to your other projects (Right Click Project -> Add Existing Item -> Select the file from your Core project then instead of "Add" you need to click the triangle next to "Add" and say "Add As Link"). You can then reference it in the usual way from within your specific project...

Load content from a dynamically loaded module with MEF

I'm working on a modular Silverlight application and in one of the modules I'd like to display an image on a view. I've added the image file under the module project in the /Resources/Images directory. And than I've changed the image file's Build Action property to Content. I think it's nicer not to embed the image into the dll file. The xap file after build contains the image.
In the View xaml I've inserted the image <Image Source="/Resources/Images/Yoda.jpg" /> and design time it displays correctly but runtime the image is missing.
My question is how to solve this problem? I don't want to embed in the dll (Build Action: Resource).
I've made a small test solution if you want to play with it:
Solution.zip
Thanks in advance
Design time the image displayed correctly:
But runtime the image is missing:
Solution structure:
You could set the Copy to Output Directory property of the image to either:
Copy always, or
Copy if newer
Then at runtime it will be available.

image file resources to image source in silverlight app

I want a Listbox that lists all the images I have as resources in my app, then on selection changed the Image Source in my app is changed.
Need to:
Enumerate all the image files,
Load them into an imagesource
In WPF, I can just System.IO to grab the files from a directory, but I can't do this in silverlight.
how do I do this?
Don't think you can do that for files set as Resource or Content. It is possible for Embedded Resource (although Embedded Resource is not recommended for Silverlight. Don't think that an Image control can access them directly). See related discussion It suggests creating such a list during build time using T4

Sharing Graphics between WPF and Winforms

I have a number of png, bitmaps and icons which I want to use in both Winforms projects and WPF projects.
I'm comfortable with using resources in winforms, and happy with resources in WPF.
What are people doing where they have common resources shared by both? My current feeling is to have a WPF resource assembly and a Winforms resource assembly, but none of the approaches I've looked at seem fluid.
thanks
I have a project with both WPF and Winforms controls. This project have resource folder, which contains all the images. Than I simply load them from the disk using relative path.
Assuming that your project name is "MyProject", and the images path is a folder named "MyImages", inside the projects folder, and your image name is "MyImage.bmp" you can load them as follows:
In Winforms:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
In WPF
<Image Source="pack://application:,,,/MyProject;Component/MyImages/MyImage.bmp">
Hope it helps...
Answer by Amittai Shapira works if build action is set to "Embedded resource". If you set build action to "Resource", then you could use:
WinForms:
Bitmap photo;
var picUri = new Uri(#"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
WPF:
<Image Source="MyImages/MyImage.bmp"/>

Resources