0

我在代码中使用事件来通知图像是否被扫描

在我的视图模型中:

public BitmapImage ImageSelected
{
   get{return _imageSelected;}
   set
   {
        if (_imageSelected == value)
        {
            return;
        }
        _imageSelected = value;
        RaisePropertyChanged(() => ImageSelected);
   }
}

public ICommand CmdScan
{
   get
   {
       return new DelegateCommand(Scan);
   }
}
WpfTwain twainInterface = null;
private BitmapImage _imageSelected = null;

private void Scan()
{

    twainInterface = new WpfTwain();
    twainInterface.TwainTransfer += new TwainTransferReadyHandler(TwainWin_TwainTransfer);
    twainInterface.Acquire(true);
}


private void TwainWin_TwainTransfer(BitmapImage imageSources)
{
    ImageSelected = imageSources;
}

在我的 XAML 中

<Image Margin="151,12,356,12" Stretch="Fill" Source="{Binding ImageSelected}" />

扫描多个图像时,Windows 中的图像在所有扫描完成之前都不会改变,即使我在 TwainWin_TwainTransfer 中设置了一个断点,并且在扫描的每个图像和 RaisePropertyChanged 中都会调用它,当扫描完成时它只显示最后一个图像

欢迎任何建议,我是 MVVM 的新手

更新

TwainLib 类中的扫描代码(由 调用twainInterface.Acquire(true);

do
{
   pxfr.Count = 0;
   hbitmap = IntPtr.Zero;

   TwImageInfo  iinf = new TwImageInfo();
   rc = DSiinf( appid, srcds, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, iinf );
   if( rc != TwRC.Success )
   {
      CloseSrc();
      return pics;
   }

   rc = DSixfer( appid, srcds, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, ref hbitmap );
   if( rc != TwRC.XferDone )
   {
      CloseSrc();
      return pics;
   }

   rc = DSpxfer( appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr );
   if( rc != TwRC.Success )
   {
      CloseSrc();
      return pics;
   }
   pics.Add(hbitmap);
   BitmapImage bitmapImage = /*converting hbitmap to BitmapImage  */
   TwainTransferReady(bitmapImage); 
}
while( pxfr.Count != 0 );

我也尝试完成 twainInterface.Acquire(true);一项任务,但结果相同,

4

1 回答 1

0

我换了

private void TwainWin_TwainTransfer(BitmapImage imageSources)
{
ImageSelected = imageSources;
}

App.Current.MainWindow.Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => ImageSelected = imageSources));
于 2013-05-14T14:29:50.137 回答