如何在 Picturebox 中自动显示扫描的图像?

How do I display a scanned Image automatically in a Picturebox?

我需要在扫描后自动将图片上传到 PictureBox

这是 PictureBox 的名称PictureBox ptbImgDocEmp

这是扫描仪class

Scanner.cs

 public class Scanner
 {
    Device oDevice;
    Item oItem;
    CommonDialogClass dlg;
    public Scanner()
    {
        dlg = new CommonDialogClass();
        try
        {
            oDevice = dlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
        }

        catch (Exception Exp)
        { MessageBox.Show("printer not detected");}

    }

这是一个扫描仪按钮

private void btnSca_Click(object sender, EventArgs e)
    {
        Scanner oScanner = new Scanner();
        oScanner.Scann();

        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.FileName = "test.jpg";
        saveFileDialog.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            ptbImgDocEmp.Image = Image.FromFile(saveFileDialog.FileName);
            ptbImgDocEmp.Refresh();
        }
    }

要获取最后创建的文件,假设他们的名字是升序的,你可以使用这个:

using System.IO;
..

string path = @"C:\Users\MyComputer\Documents\Scanned Documents";

var files = Directory.GetFiles(yourPath);
var filesSorted = files.OrderBy(x => x);
string lastFile = filesSorted.Last();

如果您的扫描仪软件创建的文件名称不是严格升序的,您可以使用此代码:

var files = Directory.GetFiles(yourPath);
List<FileInfo> filesInfoList = new List<FileInfo>();
foreach(string f in files ) filesInfoList.Add(new FileInfo(f));
var filesSorted = filesInfoList.OrderBy(x => x.CreationTime);
string lastFile = filesSorted.Last().FullName;

那你可以写

ptbImgDocEmp.Image = Image.FromFile(lastFile );