-2

I've tried adding a image via the following however it is still not working. The image type is a content.

Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));

//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;

// Center the image around the location specified


//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);

mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);
ctacke
  • 66,480
  • 18
  • 94
  • 155
ericlee
  • 2,703
  • 11
  • 43
  • 68

5 Answers5

1

Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources

This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.

I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.

Community
  • 1
  • 1
Dessus
  • 2,147
  • 1
  • 14
  • 24
0

I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?

Assuming that you have added it directly:

If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:

Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;

However if you have set the "Build Action" to "Content" you should use the GetContentStream method

Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
David Thompson
  • 2,902
  • 2
  • 18
  • 21
0

Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root): /pages - Contains the actual pages and therefore also the page containing the above code /images - Contains the actual PNG images that have to be referenced

In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows: 1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level) 2. Now reference /images -> ../images 3. Now add the actual file reference -> ../images/myimage.png

If you use the correct URI the problem is solved.

Tom Verhoeff
  • 1,270
  • 6
  • 12
-1

The main question seems to be how to get true uri. For me, the following table helps me in this case (I only have it in German): Uriformats WP7

http://msdn.microsoft.com/de-de/library/aa970069.aspx

example:

// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);

example 2:

Uri uri = new Uri("pack://application:,,,/File.xaml");

or Codebehind:

'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
-3
/projectname;component/images/menu/lost.png

Is the correct way, the rest of your answer is really not working

ericlee
  • 2,703
  • 11
  • 43
  • 68
  • Just to clarify, how is your xaml.cs file related to the image file? I presume the xaml.cs file is in a separate folder and therefore would require a relative uri. For example if the xaml.cs file is in /views your relative uri would be "../images/menu/lost.png" – Tom Verhoeff Jan 08 '12 at 15:36
  • my xaml is on /pages while images is on /images? could this be the prob? – ericlee Jan 09 '12 at 08:04
  • It is not a problem, but you should be aware of the consequences for your path. Every extra . means you go up another level. The link is relative! In your situation it is like this. "/images/file.png" -> "/pages/images/file.png" "./images/file.png" -> "/pages/images/file.png" "../images/file.png" -> "/images/file.png" – Tom Verhoeff Jan 10 '12 at 19:58