从图像资源

时间:2018-04-26 14:44:34

标签: c# wpf imagesource

我有一个WPF登录屏幕,我想动态加载启动图像。大多数情况下,图像加载正常,但是如图所示,图像显示损坏的可能性为10次,如下所示。

在XAML中,我有一个简单的图像控件。

<Image x:Name="imgLogonSplash" Stretch="None" Width="800" Height="489"/>

我通过绑定在我的viewmodel中执行此操作,但为了简单起见,我现在在我的代码中有这一行。

imgLogonSplash.Source = new BitmapImage(new Uri($"pack://application:,,,/Company.myapp;component/{splashImagepath}"));

上面的SplashImagePath提供了要加载的图像的名称。图像是主程序集中的.png文件,并标记为“资源”。

我已经尝试了几种加载图像的方法,包括在我的viewmodel中。任何人都可以解释为什么这不能正常工作吗?

BitmapImage image = new BitmapImage();
image.BeginInit();

string imageUri = $"pack://application:,,,/Company.myapp;component/{splashImagePath}";

image.UriSource = new Uri(imageUri, UriKind.Absolute);

image.EndInit();
return image;

corrupted image example

1 个答案:

答案 0 :(得分:-1)

我会检查您正在使用的图像的分辨率(可能是图像文件的大小),如果它们非常大并且您运行此应用程序的系统没有要渲染的硬件内存(图形)图像。

尝试使用简单但尺寸较小的图像。

希望这会有所帮助。

相关问题