Sharing Graphics between WPF and Winforms - wpf

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"/>

Related

What is the purpose of "Design" folder and "Skins" folder in MVVM WPF application created with MVVM Light Toolkit V 4.0?

I bag your pardon. I'm very beginner in MVVM. I installed MVVM Light Toolkit V 4.0 and tryed to create a WPF MVVM application project with it. A project was created successfully. There are following folders in the project: "Design", "Model", "Skins" and "ViewModel". I clearly understand the necessity of "Model" and "ViewModel" folders. But what is the purpose of "Design" and "Skins" folders? A folder "Design" contains a file DesignDataService.cs with the following contents:
using System;
using MvvmLight1.Model;
namespace MvvmLight1.Design
{
public class DesignDataService : IDataService
{
public void GetData(Action<DataItem, Exception> callback)
{
// Use this to create design time data
var item = new DataItem("Welcome to MVVM Light [design]");
callback(item, null);
}
}
}
The folder Skins contains a file MainSkin.xaml with the following contents:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
Explain me please: what is the purpose of "Design" folder (and it's contents) and "Skins" folder (and it's contents) in MVVM WPF application? And where should I place xaml-file that represents the View in MVVM-application?
Design Folder provides Design-Time data. Data that helps you design your UI in VS or expression-blend with dummy data that will not be applied to your project at run-time.
Skins is just a collection of resources in your project. Style's, ControlTemplates and such can go into that folder.
MVVM-light at codeplex have a look through the 2 videos in that page where the author of the library shows how to use Design time data.
The WPF designer in Visual Studio or in Blend is able to run the application during the design time - In this context, design teams means actually during that time you design the GUI of your application. Most of the application designs work only if the design is filled with data, but it makes not sense to call a database or somethings else. In conclusion the folder design contains all class which provide the data for the design time. This behaviour if often called blendability, because of the tool Blend. This links might help for further information:
http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/
http://www.robfe.com/2009/12/design-time-data-in-expression-blend-3-revisited/
The folder skins normally contains custom designs for your WPF controls. For more information, this url might help: http://www.c-sharpcorner.com/uploadfile/raj1979/skins-in-wpf/

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

App.XAML where are you?

I am new in WPF. Created a new WPF UserControl. See that some people uses an app.xaml file in order to set inside application level ressources.
My solution consists of a WinForm and a WPF UserControl. I don't see somewhere any app.xaml file.
How to proceed?
App.xaml is associated with a WPF application. If you've only got a UserControl, there's no application for it to be part of, is there?
Create a WPF application and you'll have an App.xaml to put application-level resources in.
Out of interest, why do you have WinForm if you're using a WPF user control?
EDIT: To repeat my comment: you're not going be provided with WPF Application resources smoothly when you're not creating a WPF application.
EDIT: As noted in Anthony Brien's answer, it seems you can hack it around - but I would strongly recommend against this sort of thing if you can possibly help it. Fundamentally, you're working against the expectations of the platform - and that's never a nice situation to be in.
It is possible to have application wide WPF resources in a WinForms application. Look at http://www.wpftutorial.net/AppLevelResourcesWinForms.html
You need to create a WPF or Silverlight application to get that file
You are actually hosting a WPF control in a Winforms application
If you created a WinForm project, it will not have a app.xaml.
Create a WPF project instead.
This article explains in great detail how to have application wide resources in a hosted/interop scenario:
http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/
The solution path I chose (from the article) is to include App.xaml as a page element
Modify project file:
<Page Include="App.xaml" />
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
Add code:
public App()
{
InitializeComponent();
}
public static void EnsureApplicationResources()
{
if (Application.Current == null)
{
// create the Application object
new App();
}
}
Full details are in the article.

wpf Image resources and visual studio 2010 resource editor

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));

Will WPF process an App.xaml file if the hosting application isn't WPF?

First I just want to say I am new to WPF, so please excuse my ignorance...
I am creating a .Net plug-in for Rhino 4.0. With the plugin I am developing a UI using WPF.
The Rhino 4.0 CAD engine is an MFC/Win32 application. The plugin will execute after the application is run, and it creates the WPF Window and then "sucks" the MFC Window into it.
So my question is, does WPF look for an App.xaml file to get to Application level resources if the hosting application isn't a WPF app?
If not, what is the best way to store application level resources?
Thanks,
Jason
App.xaml is used as a part of a partial class App : Application.
If your application does not have a WPF based Application class,
you can manually load dictionaries and merge with the application and create a main window and show it (access via static methods of Application class).
Code goes kind of like this.
var reader = new XamlReader();
var dictionary = reader.read("path to xaml file") as ResourceDictionary;
if (dictionary != null)
Application.MergedDictionaries.Merge(dictionary);
var mainWindow = new MyMainWindow();
mainWindow.Show();
WPF projects will - by default - generate an entry point for your application. This entry point constructs and initializes your Application-derived class for you. If you need, you can always create your instance manually, and store application-level resources in it:
App app = new App();
app.InitializeComponent();
app.Run();
Have you tried storing your resources at what MSDN refers to as the 'theme level'?
Within a folder called "<root>\Themes" have a file called generic.xaml.
I haven't tried this for a project that wasn't a WPF application, but the approach might work for you.
my guess is it has to do with how does rhino run your plugin does it run it as a seperate process or does it just call some thing you have defined?
If it does call a function you defined then you could just put the code there that will start the window?

Resources