我正在编写.NET 4.0 WPF应用程序,我需要从硬盘加载大图像(300 MB)。加载图像需要几秒钟,我想通过使用进度条显示它的进度。我使用带有BitmapImage的图像作为其源。 BitmapImage类提供了DownloadProgress事件,如果我从Web加载图像,它可以正常工作。但如果我从我的硬盘加载图像,它就不起作用。
这是MCVE: xaml文件:
<Window x:Class="progressbar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:progressbar"
Title="MainWindow" Height="Auto" Width="Auto">
<Window.DataContext>
<local:MainVM />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="{Binding BitmapImg}" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>
<TextBox Text="{Binding Url, Mode=TwoWay}" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1"/>
<Button Content="Load local image file" Grid.Column="0" Grid.Row="2" Click="Button_Click"/>
<Button Content="Load from url" Grid.Column="1" Grid.Row="2" Click="Button_Click_1"/>
<ProgressBar Value="{Binding Progress}" Grid.Column="2" Grid.Row="2"/>
</Grid>
</Window>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
namespace progressbar
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
var dc = DataContext as MainVM;
if (dc == null) return;
OpenFileDialog dialog = new OpenFileDialog();
var res = dialog.ShowDialog();
if (res == true) {
dc.Url = dialog.FileName;
dc.loadImage();
}
}
private void Button_Click_1(object sender, RoutedEventArgs e) {
var dc = DataContext as MainVM;
if (dc == null) return;
dc.loadImage();
}
}
}
ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using System.ComponentModel;
namespace progressbar
{
public class MainVM : INotifyPropertyChanged
{
private BitmapImage bmi = null;
private Int32 _progress = 0;
private String _url = "";
public event PropertyChangedEventHandler PropertyChanged;
public Int32 Progress {
get { return _progress; }
set {
_progress = value;
NotifyPropertyChanged("Progress");
}
}
public String Url {
get { return _url; }
set {
_url = value;
NotifyPropertyChanged("Url");
}
}
public BitmapImage BitmapImg {
get { return bmi; }
set { bmi = value; }
}
public MainVM() { }
private void NotifyPropertyChanged(string propertyName = "") {
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void loadImage() {
bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(_url);
bmi.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bmi_DownloadProgress);
bmi.EndInit();
NotifyPropertyChanged("BitmapImg");
}
void bmi_DownloadProgress(object sender, DownloadProgressEventArgs e) {
Progress = e.Progress;
}
}
}
如果您尝试从硬盘加载图像,则不会显示进度。 有没有办法做到这一点?
提前致谢