从Exe文件夹中的C#WPF加载图像 [英] C# WPF Load images from the exe folder

查看:367
本文介绍了从Exe文件夹中的C#WPF加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的程序从PC转移到另一个,但问题是图像不加载任何其他电脑(图片来源问题)。所以我想知道如果我能创造一个exe文件被放在一个文件夹并将其命名为资源,每一个形象从那里加载。

  image2.Source =新的BitmapImage(新的URI(@RES \\ startoh.png));


解决方案

您可能只是将图像作为资源添加到您的Visual Studio项目。然后,他们将被打包成可执行文件的组装和你不需要单独复制它们。

在项目中创建一个文件夹(假设名为图片)和你的图像添加到该文件夹​​。

请确保该图像生成操作设置为资源

现在您可以通过适当的包URI

  VAR URI =新的URI(包://应用:,,, /图片/ SomeImage.png);
image.Source =新的BitmapImage(URI);

i want to move my program from a pc to another but the problem is the images are not loaded on any other pc (Source problem) . So i was wondering if i could just create a folder where the exe is placed and name it Resources and to load every image from there.

image2.Source = new BitmapImage(new Uri(@"Res\startoh.png"));

解决方案

You may just add the images as resources to your Visual Studio project. Then they will be packed into the assembly of the executable and you don't need to copy them separately.

Create a folder in your project (let's say called Images) and add your images to that folder.

Make sure that the Build Action for the images is set to Resource.

Now you can simply create a BitmapImage from such a resource by an appropriate Pack URI:

var uri = new Uri("pack://application:,,,/Images/SomeImage.png");
image.Source = new BitmapImage(uri);

这篇关于从Exe文件夹中的C#WPF加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆