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();
}
}
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 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.