【问题标题】:Dynamic Loading of Larger Images in WPF在 WPF 中动态加载较大的图像
【发布时间】:2018-09-04 04:57:05
【问题描述】:

我有一个 WPF 的奇怪问题,我在运行时从磁盘加载图像并将它们添加到 Canvas 容器中。我的图像大小超过 20MB。我需要在一个窗口中显示 20 到 30 张图像,并且我想以完全清晰的方式显示图像。我的问题是一些图像没有显示。下面是我的代码

for (var i = 0; i < Count; i++)
{
   BitmapImage bmp=new BitmapImage(new Uri(ImagePath, UriKind.RelativeOrAbsolute));
   Image imageControl = new Image();
   imageControl.Source = bmp;
   MyCanvas.Children.Add(imageControl);            
}

【问题讨论】:

  • “图像大小超过 20MB” - 这到底是什么意思?它是否是 JPG 文件的大小,表示图像超过 20 兆像素?请注意,您不应在后面的代码中创建 Image 元素。相反,在 ItemTemplate 中使用带有 Image 元素的 ItemsControl,并将其 ItemsSource 绑定到文件路径字符串的集合,例如像这样:stackoverflow.com/a/34557467/1136211

标签: wpf


【解决方案1】:
<ItemsControl x:Name="imageLists">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Width="500" Margin="15"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

像这样设置 ItemsControl 的 ItemsSource:

imageLists.ItemsSource = Directory.EnumerateFiles(FOLDERPATH, "*.*");

【讨论】:

    猜你喜欢
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多