I've been trying to get my cursor to be a custom cursor from a .cur file located in a folder in my project as a Resource. Here's my solution layout:
Solution
WPF Application
WPF Class Library
SampleControl.xaml (this is hosted in the WPF Application in the solution)
Images
test_cursor.cur
test_cur.cur's build action is set to Resource. In my class library is a UserControl at the top level which is what I want to change the cursor for. Here's the relative code:
public partial class SampleControl: UserControl
{
InitializeComponent();
StreamResourceInfo streamResource = Application.GetResourceStream(new Uri("test_cursor.cur", UriKind.Relative));
Cursor = new Cursor(streamResource.Stream);
}
I've tried a few different variations of the Uri but I always get an error Cannot locate resource 'test_cursor.cur'.
The file path is wrong.
StreamResourceInfo streamResource = Application.GetResourceStream(new Uri("Images\\test_cursor.cur", UriKind.Relative));
Cursor = new Cursor(streamResource.Stream);
I got it working doing the following:
Uri uri = new Uri("pack://application:,,,/SampleClassLibrary;Component/Images/test_cursor.cur");
StreamResourceInfo streamResource = Application.GetResourceStream(uri);
SampleClassLibrary is the name of the class library project under the solution.
I had to make the Uri the way it was because the .cur file is a Resource under a class library and is being hosted in a different project under the same solution
Related
I have the following code for showing an image:
Image im = new Image();
im.Source = new BitmapImage(new Uri("Icons.login.jpg", UriKind.RelativeOrAbsolute));
im.Height = 200;
im.Width = 200;
icons.Add(new IconImagesForDummies() { Name = str, Image = im });
The Image class belongs to System.Windows.Controls.Image. I have added a folder to store images separately from Resources folder (named Icons). But I have set the build action property of all images to Embed Resource and they have been set to Copy Always. I do not know why I can not see the images. I'm using WPF. During debugging I found out that the image path was fine and it founded that, but I do not know the reason, the image was not showed. I have read here but was useless.
Is there something wrong with the Uri?
EDIT: maybe its just this simple
im.Source = new BitmapImage(new Uri(#"Icons\login.jpg", UriKind.Relative));
Edit: changing build action to content works.
I'm looking for a method to make all my resource files work properly after I have written my project in a cd.
For example I have saved my images in Project Name\Project Name\obj\Debug\Images and I want them to be usable both in xaml and regular c# code, when I insert that cd in another machine.
Thanks for your time!
What worked for me was, using the Image class for both backgrounds and overlapping images.
In xaml the code is <Image x:Name="Image1" Source="..\obj\Debug\Images\image.jpg"/>
and you initialise it in c# using
public PageName()
{
InitializeComponent();
Image1.Source = new BitmapImage(new Uri(#"../Images/cafeteria.jpg", UriKind.Relative));
}
It's a shame because (atleast in my case) the ImageBrush Class didn't support relative uris for some reason..
I want to access some images in my Windows Phone class library, but how can I access these images?
I have a method which will set the image, but
new Uri("/Assets/Images/MyImage.png", UriKind.Relative);
doesn't work. I've tried setting the image to Resource instead of Content, but it also doesn't work.
So how can I provice images in my class library?
From what I gather it looks like you are trying to set the source of an image from the XAML code behind file.
If so you can do so as follows:
MyImage.Source = new BitmapImage(new Uri("/Assets/Images/MyImage.png", UriKind.Relative));
Hope this helps.
I'm trying to display splash screen for my WPF application. Based on some condition I have to load one of two images I have created. So i have to use SplashScreen class instead of setting a static image as flashscreen. Which is easy and works in my case.
I'm doing something like following,(I have set the both images as Embedded Resource)
string splashImage = string.Empty;
if (Condition)
{
splashImage = "ApplicationType1.png";
}
else
{
splashImage = "ApplicationType2.png";
}
SplashScreen screen = new SplashScreen(splashImage);
screen.Show(true);
Which gives me exeception,
Cannot locate resource ApplicationType1.png
Finally i figured it out,
Problem was with the .png image i have added. Still i have no clue why it couldn't load that specific image. I just got the .jpg image and it works fine.
And it works with both build actions Embedded Resource or Resource
I develop an IOC framework in the form of a DLL that creates the application's main window and displays various WPF Pages in it. There is a folder of PNG images with their Build Action set to Resource. I use code like this to set the sources of WPF Image elements...
MyImage.Source = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png", UriKind.Relative));
This works great. However, there is also a feature where the user can choose to display one of these WPF Pages in a separate window. Here is the mystery: If the user navigates to the page in the main window before showing it in a separate window, all is well. However, if the user shows the page in a separate window before it is shown in the main window, I get a DirectoryNotFoundException saying " Could not find a part of the path 'C:\MyAssembly;component\Images\MyImage.png'."
I discovered the OnLoad cache option and tried the following code...
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri("/MyAssembly;component/Images/MyImage.png", UriKind.Relative);
image.EndInit();
MyImage.Source = image;
However, I then get the DirectoryNotFoundException on the very first image that I attempt to create.
I also tried an absolute URI...
MyImage.Source = new BitmapImage(new Uri("pack://application:,,,/MyAssembly;component/Images/MyImage.png", UriKind.Absolute));
I then get a UriFormatException saying "Invalid URI: Invalid port specified."
I solved the problem by using the 'pack' URI format as shown in the question. However, I needed to make a call to the Application class before I did so. This has the effect of executing the Application's static constructor which registers the 'pack' URI scheme. I found my answer here. The call I used was simply this:
var app = Application.Current;