我看了下,还是不知道怎么设置缩略图显示大小,是哪个字段控制的吗?我自己重新写了一个第二次扫描的时候就会卡死,我想要实现扫描后将图片保存本地,并将扫描的图片展示在窗口控件上。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Leadtools;
- using Leadtools.Twain;
- using Leadtools.Codecs;
- using Leadtools.WinForms;
- using Leadtools.Drawing;
- using System.IO;
- namespace AcquiringImage
- {
- public partial class Form1 : Form
- {
- private TwainSession twnSession;
- private RasterCodecs rasterCodecs = new RasterCodecs();
- public TwainTransferMechanism _transferMode = TwainTransferMechanism.Native;
- public string dossierFilePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\');
- public string fileOrgExtension = "jpg";
- public Form1()
- {
- InitializeComponent();
- InitMyControls();
- }
- private void InitMyControls()
- {
- _browser = new RasterThumbnailBrowser();
- _browser.Codecs = new RasterCodecs();
- _browser.Dock = DockStyle.Fill;
- this.panel1.Controls.Add(_browser);
- _browser.BringToFront();
- _browser.ItemSpacingSize = new Size(10, 10);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- TwainStart();
- //InitMyControls();
-
- }
- private void TwainStart()
- {
- if (twnSession == null)
- {
- twnSession = new TwainSession();
- if (TwainSession.IsAvailable(this))
- {
- twnSession.Startup(this, string.Empty, "文档扫描", string.Empty, string.Empty, TwainStartupFlags.InitializeMultithreaded);
- twnSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(twnSession_AcquirePage);
- }
- }
- }
- private void buttonSelectSource_Click(object sender, EventArgs e)
- {
- try
- {
- //TwainStart();
- twnSession.SelectSource(string.Empty);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message);
- }
- }
- private void setParam_Click(object sender, EventArgs e)
- {
- this.twnSession.EnableShowUserInterfaceOnly = true;
- this.twnSession.ShowTemplateDialog(AppDomain.CurrentDomain.BaseDirectory + @"\Twain.nko");
- this.Activate();
- }
- private void buttonAcquire_Click(object sender, EventArgs e)
- {
- try
- {
- //SetTransferMode();
- // Acquire one or more images from a TWAIN source.
- twnSession.Acquire(TwainUserInterfaceFlags.None);
- Application.DoEvents();
- //twnSession.Acquire(TwainUserInterfaceFlags.Show | TwainUserInterfaceFlags.Modal);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message);
- }
- }
- private void SetTransferMode()
- {
- using (TwainCapability twnCap = new TwainCapability())
- {
- try
- {
- twnCap.Information.Type = TwainCapabilityType.ImageTransferMechanism;
- twnCap.Information.ContainerType = TwainContainerType.OneValue;
- twnCap.OneValueCapability.ItemType = TwainItemType.Uint16;
- twnCap.OneValueCapability.Value = (UInt16)_transferMode;
- // Set the value of ICAP_XFERMECH (Image Transfer Mechanism) capability
- twnSession.SetCapability(twnCap, TwainSetCapabilityMode.Set);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message);
- }
- }
- }
- private void InitClass()
- {
- InitMyControls();
- _browser.LoadThumbnails(Path.Combine(dossierFilePath, string.Empty, "Temp", "OriginalFile"), "*.*", RasterThumbnailBrowserLoadFlags.Block);
- _browser.Refresh();
- _browser.Items.Add(new RasterImageListItem());
- _browser.EnableBroswerWatcher = true;
- }
-
- private void twnSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
- {
- Application.DoEvents();
- GC.Collect();
- string rootPath = Path.Combine(dossierFilePath, string.Empty, "Temp", "OriginalFile");
- string path = string.Format(rootPath + "\\{0}.{1}", Guid.NewGuid().ToString().ToUpper().Replace("-", string.Empty), fileOrgExtension);
- rasterCodecs.Save(e.Image, path, RasterImageFormat.Jpeg, 24);
- Application.DoEvents();
- InsertImage(path);
- }
- private void InsertImage(string fileFullName)
- {
- RasterImageListItem imageItem = null;
- Image img = null;
- RasterImage image = null;
- StreamReader fs = new StreamReader(fileFullName);
- img = Image.FromStream(fs.BaseStream);
- img = img.GetThumbnailImage(183, 240, null, System.IntPtr.Zero);
- image = RasterImageConverter.ConvertFromImage(img, ConvertFromImageOptions.None);
- img.Dispose();
- fs.Close();
- fs.Dispose();
- imageItem = new RasterImageListItem() { Image = image.CloneAll(), Tag = 1, FileName = fileFullName, Text = string.Empty };
- image.Dispose();
- _browser.Items.Add(imageItem);
- _browser.BeginUpdate();
- _browser.EndUpdate();
- _browser.ScrollItems(_browser.Items.Count - 1);
-
- GC.Collect();
- }
- #region 获取当前页码
- /// <summary>
- /// 获取当前页码
- /// </summary>
- private int CurrentPageIndex
- {
- get
- {
- for (int i = 0; i < _browser.Items.Count; i++)
- {
- if (_browser.Items[i].Selected)
- return i;
- }
- return 0;
- }
- }
- #endregion
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- try
- {
- twnSession.Shutdown();
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message);
- }
- }
- }
- }
复制代码 |