动态WPF图像加载问题

时间:2014-08-29 14:50:15

标签: c# wpf

我有一个问题,我认为其他WPF图像加载问题尚未涉及。我正在扫描几张图片并将它们传递给“预览页面”。预览页面采用图像缩略图,并通过生成的位图显示打印输出的内容。

对我来说奇怪的是,如果我第一次运行该程序,它将正常工作。到达流程结束并点击“重新开始”时,预览将返回空白。我在一个方法中创建BitmapImage,将位图保存为随机文件名,所以我不相信第二次锁定文件。此外,如果我去查看通过资源管理器创建的临时文件,它将被正确绘制,因此我知道适当的数据正在进行中。

最后,当我离开此页面时,我正在清除必要的数据。我真的很困惑,任何帮助都会受到赞赏。

//Constructor
public Receipt_Form() {
    InitializeComponent();

    printData = new List<Object>();
    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e) {
    // populates global variable fileName
    var task = System.Threading.Tasks.Task.Factory.StartNew(() => outputToBitmap());         task.ContinueWith(t => setImage(fileName),
    System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());

   // I started the image creation in a separate thread because I 
   // thought it may be blocking the UI thread, but it didn't matter
}

private void setImage(string imageURI) {
    BitmapImage image;

    using (FileStream stream = File.OpenRead(imageURI)) {
        image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.EndInit();
    }

    receiptPreview.Source = image;
    //this works the first iteration but not the second, though the temp file is created successfully
}

1 个答案:

答案 0 :(得分:-2)

发现问题 - 在转出页面时,现代UI容器被清除。

相关问题