Relative Resource Uri in WPF Forms - c#

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..

Related

Changing cursor to custom cursor image as a resource

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

Windows 8 C#: Windows.Ui.Xaml.Media.ImageSource change Image

WARNING There are two different ImageSources in Windows API
System.Windows.Media.ImageSource = The one of Windows Forms
Windows.Ui.Xaml.Media.ImageSource = The one of Windows Store Apps
I'm a beginner in C# and Windows 8 Metro Style App programming stuff.
But if you want to edit the image of a Image, the "old" method with BitmapImage won't work:
XAML:
<Image Source="http://image.source.de" x:Name="Image1" />
Code-Behind C#:
Image1.Source = new BitmapImage...
won't work. The compiler will say something like
"System.Windows.Media.Imaging.BitmapImage" can't be converted to "Windows.Ui.Xaml.Media.ImageSource"
(Yes, BitmapImage can usually be converted to System.Windows.Media.ImageSource)
(System.Windows.Media.Imaging isn't anymore in the default import list of Visual Studio for Windows (StoreApps), you need to bind PresentationCore.dll first.)
So - is there any solution to edit Image1.Source than using Bindings?
This should work.
Image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new System.Uri("http://image.source.de"))

Access images in Windows Phone Class Library

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.

How to use gif animated image in WP 7

I have seen this post: Display GIF in a WP7 application with Silverlight
But in my case? for animating I am using a popup. So when application starts it shows a popup for 5 seconds. In this popup I want to show some .gif image, but it doesn't work.
Here is the code which I implement:
public partial class AnimatedSplashScreen : UserControl
{
protected Uri ImageSource
{
get;
set;
}
public AnimatedSplashScreen()
{
InitializeComponent();
ImageSource =
new Uri(
"http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Sunflower_as_GIF.gif/200px-Sunflower_as_GIF.gif",
UriKind.Absolute);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
}
And xaml code is:
<UserControl.Resources>
<imagetools:ImageConverter x:Key="ImageConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Width="480"
Height="800"
Background="White">
<imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />
But in result it does't work, it shows an empty background.
Updated:
ImageTools.IO.Decoders.AddDecoder();
ImageSource = new Uri("http://a3.twimg.com/profile_images/1136683647/hisoka_normal.gif", UriKind.Absolute);
It still doesn't work
Finally working... Talk about events conspiring against you... You need to fix all these things first!
(note there is a following problem with only the first 2 frames being animated but that is for another question):
Part 6 (getting sleepy now)
Lastly relative image URLs starting with "/" are not supported by the ImageTools.Controls.ImageConverter, so you need to use a relative URL without the leading "/". I found that once every other problem was fixed I was getting an unsupported exception with the path.
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
InitializeComponent();
this.ImageSource = new Uri("layer1.gif", UriKind.Relative);
this.DataContext = this;
Part 5
You need to set the binding DataContext somewhere.
You do not connect the XAML page DataContext to the code behind object. I could not see where you had done this. A very simple/quick way is to set this.DataContext = this; in the page's constructor.
Part 4
You can only bind to public properties!
Your ImageSource property is currently protected. Change it to Public
public Uri ImageSource
{
get;
set;
}
Part 3
I also note your ImageSource property is not an INotifyPropertyChange type property. So setting it after InitializeComponent will not work.
Try it this way round (or change it to use a notify property):
public AnimatedSplashScreen()
{
ImageSource =
new Uri(
"/200px-Sunflower_as_GIF.gif",
UriKind.Relative);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
InitializeComponent();
}
Part 2 (actually not support by ImageTools.Controls.ImageConverter)
The cross domain file was apparently only one problem. Based on the comments you also need to store your images on your own website and reference them with an appropriate URI format.
If you put your files in a folder called images under ClientBin you use this format:
"/images/imagename.jpg"
This is the best option as the images also use Browser caching!
For your example it would be like this:
ImageSource =
new Uri(
"/images/200px-Sunflower_as_GIF.gif",
UriKind.Relative);
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
and put the example file in your client bin folder under images.
If you do not use the leading "/" Silverlight assumes the files are resources in the current module instead e.g.
"images/imagename.jpg"
Part 1
This is actually a copyright issue, to stop people deep-linking files from other people's sites without permission.
The Wikimedia.org site does not have any cross domain access files e.g.:
http://upload.wikimedia.org/crossdomain.xml
http://upload.wikimedia.org/crossdomainpolicy.xml
wikimedia.org/crossdomain.xml
wikimedia.org/crossdomainpolicy.xml
... presumably as they do not want others to use the files they host there outside of their own website.
That means Silverlight will not allow access to files on those sites as it is a good Internet citizen. Try hosting the files on your own site (where your Silverlight app resides), then it will not need any cross domain access file at all.
Side note: If you do ever need a cross domain file on a website, for use by Silverlight, use a crossdomainpolicy.xml as the other one is not as useful (designed for older flash use)

Load Images in WPF application

I am not familiar with WPF, and I just feel quite confusing.
I am trying to create a little computer game, and there are elements I want to display dynamically. I use Image class and add the images to a canvas. But I'm not sure whether it's a good practice. I feel that adding controls to canvas seem to be a little wired. And I'm a little concerned about the performance, because I may need many images.
The real problem is, I can't load the images from the image files. I see an example in a text book like this (XMAL):
<Image Panel.ZIndex="0" Margin="0,0,0,0" Name ="image1">
<Image.Source>
<BitmapImage UriSource="Bell.gif" />
</Image.Source>
</Image>
Where Bell.gif is added into the project.
And I tried to copy this in code to create an image.
new Image { Source = new BitmapImage(new Uri("Blockade.bmp"))}
But I got invalid Uri exception. After some search in the Internet, I found that loading resources dynamically seems to be difficult. Can I load the files added to the project dynamically?
If I use absolute path then it's OK. But I can't expect every computer will put the files in the same location. Can I use relative path?
I tried
new Image { Source = new BitmapImage(new Uri(#"Pictures\Blank Land.bmp", UriKind.Relative)) }
But it doesn't work. (The image is blank)
Thanks.
I deleted the code. I just found the solution in "Pack URIs in WPF" in MSDN.
I can either use "pack://application:,,,/Blockade.bmp" (absolute) or "/Blockade.bmp" (Relative) for a resource in the local assembly.
(I didn't use '/' at first)
"pack://application:,,,/ReferencedAssembly;component/File.xaml" is for referenced assembly. (I guess the problem with the answer is that the authority part is missing)
The following works:
new BitmapImage(new Uri("/MyProject;component/Resources/next.png", UriKind.Relative));
Just replace "MyProject" with the name of your project, and adjust the path to your image.
Make sure the image is added to the project with the "BuildAction" set to "Resource".

Categories