将图像添加到 Bing 地图中

发布于 2024-12-23 15:31:22 字数 566 浏览 8 评论 0原文

我已尝试通过以下方式添加图像,但它仍然无法正常工作。图像类型是内容。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白龙吟 2024-12-30 15:31:22

确保您的图像是正确的资源类型并且以最佳方式加载(即,如果多次使用,则加载一次)。有多种方法可以加载 WPF 图像(与 WP7 相同),如下所述:WPF 图像资源

这篇文章在这里: Visual Studio:如何将图像资源存储为嵌入资源? 讨论了您应该/不应该使用的不同图像资源类型。

我认为您应该看看两者,因为理解它是一件好事,因为它可以帮助您避免将来可能出现的问题。

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.

二货你真萌 2024-12-30 15:31:22

我无法对您的问题添加评论,但是当您说内容时,我会在这里问,您是否将图像直接添加到包含代码的项目或单独的内容项目中?

假设您已直接添加它:

如果您已将“Build Action”设置为“Resource”,则应使用 GetResourceStream 方法:

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;

但是,如果您已将“Build Action”设置为“Content”,则应使用 GetContentStream 方法

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;

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;
鹤舞 2024-12-30 15:31:22

只是为了澄清这个问题的答案。问题不在于资源类型,问题与相对 Uri 的工作方式有关。就像任何结构良好的项目一样,ericlee 在他的项目中使用了不同的文件夹,如下所示(相对于项目根目录):
/pages - 包含实际页面,因此也包含包含上述代码的页面
/images - 包含必须引用的实际 PNG 图像

在原始代码中,将“myimage.png”作为相对 uri 进行引用。该应用程序现在将查看“/pages/myimage.png”,因此不会找到该图像。这里的技巧是使用正确的相对 URI。它可以构造如下:
1. 首先通过两个点进入项目根目录 -> ..(一个用于当前目录,一个额外用于上一级)
2. 现在参考/images -> ../图片
3. 现在添加实际的文件引用 -> ../images/myimage.png

如果您使用正确的 URI,问题就可以解决。

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.

吐个泡泡 2024-12-30 15:31:22

主要问题似乎是如何获得真实的uri。
对我来说,下表在这种情况下对我有帮助(我只有德语):
Uriformats WP7

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

示例:

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

示例 2:

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

或隐藏代码:

'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example

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
ヅ她的身影、若隐若现 2024-12-30 15:31:22
/projectname;component/images/menu/lost.png

是正确的方法,你的其余答案确实不起作用

/projectname;component/images/menu/lost.png

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文