如何加载图像WPF在运行? [英] How to load image to WPF in runtime?

查看:81
本文介绍了如何加载图像WPF在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番搜索,好像它是相当复杂的运行时加载图像到一个WPF窗口!?

After some searching, it seems like it's quite complicated to load an image in runtime to a WPF window!?

        Image image;
        image = new Uri("Bilder/sas.png", UriKind.Relative);
        ????.Source = new BitmapImage(image);

我想这code,但我需要一些帮助才能正常工作。我得到下面的code一些红线!我也想知道,如果我需要添加一些额外的code中的XAML code里面还是足够用这样的:

I'm trying this code, but I need some help to get it to work. I get some red lines below the code! I also wonder if I need to add some extra code inside the XAML code or is in enough with this:

 <Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="350" />

奇迹监守我见过的例子与sorces到XAML标记中的图像。

Wonder becuase I have seen examples with sorces to the images inside the XAML tags.

编辑:

我使用这个诀窍:

        var uri = new Uri("pack://application:,,,/sas.png");
        var bitmap = new BitmapImage(uri);
        image1.Source = bitmap;

在XAML:

    <Grid Width="374">
    <Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="350" />
    <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="12,226,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" />
    <Button Content="Land" Height="23" HorizontalAlignment="Left" Margin="287,226,0,0" Name="btnLand" VerticalAlignment="Top" Width="75" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" />

</Grid>

编辑2:
我的回答是解决,但堆栈溢出外索姆帮助。这code正常工作:

EDIT 2: My answer is "solved", but with som help outside Stack Overflow. This code works fine:

        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.UriSource = new Uri("pack://application:,,,/Resources/" + company + ".png");
        image.EndInit();
        image2.Source = image;


推荐答案

在WPF的图像通常由装流乌里

In WPF an image is typically loaded from a Stream or an Uri.

的BitmapImage 支持和开放的我们甚至可以作为构造参数传递:

BitmapImage supports both and an Uri can even be passed as constructor argument:

var uri = new Uri("http://...");
var bitmap = new BitmapImage(uri);

如果图像文件位于本地文件夹,你将不得不使用文件:// URI。你可以从这样的路径创建这样一个开放的:

If the image file is located in a local folder, you would have to use a file:// Uri. You could create such a Uri from a path like this:

var path = Path.Combine(Environment.CurrentDirectory, "Bilder", "sas.png");
var uri = new Uri(path);

如果图像文件是在装配中的资源,开放的我们必须遵循的包乌里方案:

If the image file is a resource in an assembly, the Uri must follow the the Pack Uri scheme:

var uri = new Uri("pack://application:,,,/Bilder/sas.png");

在这种情况下,为 sas.png Visual Studio的生成操作必须是资源

In this case the Visual Studio Build Action for sas.png would have to be Resource.

一旦你创建了一个的BitmapImage ,也有一个的图片在这个XAML控制像

Once you have created a BitmapImage and also have an Image control like in this XAML

<Image Name="image1" />

您只需指定的BitmapImage到Image控件的来源属性:

image1.Source = bitmap;

这篇关于如何加载图像WPF在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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