C# 如何在运行时将图像加载到 WPF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11880946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:37:21  来源:igfitidea点击:

How to load image to WPF in runtime?

c#wpf

提问by 3D-kreativ

It seems like it's quite complicated to load an image in runtime to a WPF window.

在运行时将图像加载到 WPF 窗口似乎非常复杂。

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

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:

我正在尝试此代码,但我需要一些帮助才能使其正常工作。我在代码下方看到了一些红线!我还想知道我是否需要在 XAML 代码中添加一些额外的代码,或者是否足够:

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

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

想知道,因为我已经看到了 XAML 标签内的图像的示例。

EDIT:

编辑:

I'm using this now:

我现在正在使用这个:

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

The XAML:

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>

EDIT 2: My issue is solved, this code works fine:

编辑 2:我的问题解决了,这段代码工作正常:

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

回答by WPF-it

Make sure that your sas.pngis marked as Build Action: Contentand Copy To Output Directory: Copy Alwaysin its Visual Studio Properties...

确保您在其 Visual Studio 中sas.png标记为Build Action: Content和...Copy To Output Directory: Copy AlwaysProperties

I think the C# source code goes like this...

我认为 C# 源代码是这样的......

Image image = new Image();
image.Source = (new ImageSourceConverter()).ConvertFromString("pack://application:,,,/Bilder/sas.png") as ImageSource;

and XAML should be

XAML 应该是

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

EDIT

编辑

Dynamically I think XAML would provide best way to load Images ...

动态地我认为 XAML 将提供加载图像的最佳方式......

<Image Source="{Binding Converter={StaticResource MyImageSourceConverter}}"
       x:Name="MyImage"/>

where image.DataContextis stringpath.

哪里image.DataContextstring路。

MyImage.DataContext = "pack://application:,,,/Bilder/sas.png";

public class MyImageSourceConverter : IValueConverter
{
    public object Convert(object value_, Type targetType_, 
    object parameter_, System.Globalization.CultureInfo culture_)
    {
        return (new ImageSourceConverter()).ConvertFromString (value.ToString());
    }

    public object ConvertBack(object value, Type targetType, 
    object parameter, CultureInfo culture)
    {
          throw new NotImplementedException();
    }
}

Now as you set a different data context, Imagewould be automatically loaded at runtime.

现在,当您设置不同的数据上下文时,Image将在运行时自动加载。

回答by Clemens

In WPF an image is typically loaded from a Streamor an Uri.

在 WPF 中,图像通常从StreamUri加载。

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

BitmapImage支持两者,甚至可以将 Uri 作为构造函数参数传递:

var uri = new Uri("http://...");
var bitmap = new BitmapImage(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:

如果图像文件位于本地文件夹中,则必须使用file://Uri。您可以从这样的路径创建这样的 Uri:

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 Urischeme:

如果图像文件是程序集中的资源,则 Uri 必须遵循Pack Uri方案:

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

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

在这种情况下,Visual Studio Build Actionsas.png必须是Resource.

Once you have created a BitmapImageand also have an Imagecontrol like in this XAML

一旦你创建了一个BitmapImage并且还有一个像这个 XAML 中的Image控件

<Image Name="image1" />

you would simply assign the BitmapImage to the Sourceproperty of that Image control:

您只需将 BitmapImage 分配给该SourceImage 控件的属性:

image1.Source = bitmap;