请先安装TwainDotNet
private static AreaSettings AreaSettings = new AreaSettings(Units.Centimeters, 0.1f, 5.7f, 0.1F + 2.6f, 5.7f + 2.6f);
Twain _twain;
ScanSettings _settings;
public ScanForm(string tName, string cNumber, List<string> names, CaseSelectForm form)
{
InitializeComponent();
try
{
SelectForm = form;
this.DoubleBuffered = true;//设置本窗体
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
myNames = names;
_typeName = tName;
_caseNumber = cNumber;
this.Text = "扫描文件 - " + tName + " - " + cNumber;
_twain = new Twain(new WinFormsWindowMessageHook(this));
_twain.TransferImage += delegate (Object sender, TransferImageEventArgs args)
{
if (args.Image != null)
{
Bitmap image = new Bitmap(args.Image);
string temp = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
//缓存图片到磁盘
string path = tempPath + temp;
image.Save(path, ImageFormat.Jpeg);
tempPicUrl.Add(path);
var resizeImg = ImageHelper.KiResizeImage(image, 0.2f);
//缓存图片到磁盘
string path2 = resizePath + temp;
resizeImg.Save(path2, ImageFormat.Jpeg);
//scanImages.Add(image);
imageList1.Images.Add(image);
image.Dispose();
}
args.Image.Dispose();
};
_twain.ScanningComplete += delegate
{
for (int i = LastScanCount; i < imageList1.Images.Count; i++)
{
ListViewItem li = new ListViewItem();
li.Text = i.ToString();
li.ImageIndex = i;
mlistview_Image.Items.Add(li);
ImageStateModels.Add(new ImageStateModel
{
Checked = false,
Person = "无",
tempPicUrl = tempPicUrl[i],
Name = i.ToString()
});
}
LastScanCount = ImageStateModels.Count;
mlbl_PageCount.Text = string.Format("本次扫描 {0} 页", ImageStateModels.Count);
Enabled = true;
this.Activate();
this.WindowState = FormWindowState.Maximized;
};
// ocr = new TesseractEngine("./tessdata", "chi_sim");
InitInfo();
InitCut();
InitSearch();
InitWords();
DeleteFileByDir();
InitMatchImg();
}
catch (Exception ex)
{
log4netHelper.Error("---报错方法--ScanForm");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error(ex.ToString());
log4netHelper.Error("---end---");
MessageBox.Show("请检查设备是否开启或者链接!");
}
}
private void Init()
{
try
{
mcb_userName.DataSource = myNames;
mtbPage_Set.SelectedIndex = 0;
string savetype = ConfigHelper.ReadConfigPath("savetype.txt");
if (savetype.Trim() == "1")
{
mcBox_savetype.Text = ".jpg";
}
else if (savetype.Trim() == "0")
{
mcBox_savetype.Text = ".PDF";
}
_saveType = savetype;
}
catch (Exception ex)
{
log4netHelper.Error("---报错方法--Init");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error(ex.ToString());
log4netHelper.Error("---end---");
}
}