0

I need upload image into PictureBox automatically after scan.

This is PictureBox's name PictureBox ptbImgDocEmp

This is the class of scanner

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");}

    }

This is a Scanner Button

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();
        }
    }
TaW
  • 53,122
  • 8
  • 69
  • 111
Moataz
  • 3
  • 3
  • So, where do you think the `oScanner.Scann();` puts the Image? I know that I have no idea? What do the docs say?? – TaW Apr 22 '15 at 18:10
  • In default path: C:\Users\MyComputer\Documents\Scanned Documents – Moataz Apr 22 '15 at 18:44
  • Um, what I meant is where in your application. But if the images really get stored in that path, where is the problem in loading them? Do not use a SaveFileDialog to open them, though; use a OpenFiledialog. And if they get saved automatically, they probably have a default name. You can read in all filenames from the directory and load the last one, no? – TaW Apr 22 '15 at 20:14
  • how i can read in all filenames from the directory and load the last one without use OpenFiledialog like Example below – Moataz Apr 22 '15 at 20:56
  • See my updated answer (I removed a few typos)..! – TaW Apr 22 '15 at 21:40

1 Answers1

0

To get the last file created, assuming their names are ascending you can use this:

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();

If your scanner software creates the files with names in non-strictly ascending order you can use this code:

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;

Then you can write

ptbImgDocEmp.Image = Image.FromFile(lastFile );
TaW
  • 53,122
  • 8
  • 69
  • 111