在WPF中动态加载图像 [英] Dynamic loading of images in WPF

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

问题描述

我有一个与WPF有一个奇怪的问题,我正在运行时从磁盘加载图像,并将它们添加到StackView容器。但是,图像未显示。经过一些调试,我发现了这个伎俩,但实际上并没有任何意义。我已经做了一个小的演示程序来识别问题:



创建一个新的WPF项目,并粘贴代码如下:



xaml:

 < Window x:Class =wpfBug.Window1
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题= Window1Height =300Width =300Loaded =Window_Loaded>
< StackPanel Name =sp>
< / StackPanel>
< / Window>

xaml.cs,粘贴到默认使用下方:

 命名空间wpfBug 
{
///< summary>
/// Window1.xaml的交互逻辑
///< / summary>
public partial class Window1:Window
{
public Window1()
{
InitializeComponent();
}

private void Window_Loaded(object sender,RoutedEventArgs e)
{
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(picture.jpg,UriKind.Relative);
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
// int q = src.PixelHeight; // Image load here
sp.Children.Add(i);
}
}
}

将图像复制到bin / Debug文件夹,并将其称为'picture.jpg'



此程序不显示任何内容,除非已注释的行被取消注释。



任何人都可以解释我在做错什么,为什么会发生这种情况?如果删除图像并运行该程序,则会在int q = ...行上生成异常。如果该行被注释,即使没有图像,程序运行也不例外。只有在nessesary有意义的情况下才能加载图像,但是当我将Image控件添加到StackPanel中时,应该加载图像。



任何ides?



编辑:顺便说一句,如果将图像添加为资源,则不需要'int q = ..'行。

解决方案

这是因为创作被推迟了。
如果您想要立即加载图片,您可以简单地将此代码添加到初始化阶段。



src.CacheOption = BitmapCacheOption.OnLoad ;



像这样:

  src.BeginInit (); 
src.UriSource = new Uri(picture.jpg,UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();


I have a strange issue with WPF, I was loading images from the disk at runtime and adding them to a StackView container. However, the images were not displayed. After some debugging I found the trick, but it really doesn't make any sense. I've made a small demo app to identify the problem:

Create a new WPF project, and paste code as follows:

xaml:

<Window x:Class="wpfBug.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <StackPanel Name="sp">
    </StackPanel>
</Window>

xaml.cs, paste below default usings:

namespace wpfBug
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Image i = new Image();
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri("picture.jpg", UriKind.Relative);
            src.EndInit();
            i.Source = src;
            i.Stretch = Stretch.Uniform;
            //int q = src.PixelHeight;        // Image loads here
            sp.Children.Add(i);
        }
    }
}

Copy a image to the bin/Debug folder and call it 'picture.jpg'

This program doesn't display anything, unless the commented line is uncommented.

Can anyone explain what I'm doing wrong, or why this happens? If you remove the image and run the program it generates an exception on the 'int q= ...' line. If that line is commented the program runs without exceptions even if no image is present. Loading an image only if nessesary makes sense, but then the image should be loaded when I add the Image control to the StackPanel.

Any ides ?

Edit: By the way, if you add the image as a resource, the 'int q = ..' line is not needed.

解决方案

It is because the Creation was delayed. If you want the picture to be loaded immediately, you can simply add this code into the init phase.

src.CacheOption = BitmapCacheOption.OnLoad;

like this:

src.BeginInit();
src.UriSource = new Uri("picture.jpg", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();

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

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