using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using System.Drawing.Imaging;
using Dynamsoft.DotNet.TWAIN;
namespace Neo.Afx.ActiveX.DocumentScanner
{
public delegate void FileAcquiredHandler(string directoryLocation, string fileName, string fileBytes);
public partial class DocumentScanner : UserControl
{
public event FileAcquiredHandler FileAcquired;
private int currentImageIndex = -1;
private DynamicDotNetTwain dynamicDotNetTwain = new DynamicDotNetTwain("5D153E4A16B8D972D35CB529D323972A;13A97F6E6087FAC39D8BDF53299FE729");
private Label infoLabel;
private delegate void CrossThreadOperationControl();
private bool isToCrop = false;
#region Constructor
public DocumentScanner()
{
InitializeComponent();
this.dynamicDotNetTwain.Location = new Point(0, 0);
this.dynamicDotNetTwain.Width = scanContainer.Panel2.Width - scanControlsPanel.Width;
this.dynamicDotNetTwain.Height = scanContainer.Panel2.Height;
this.dynamicDotNetTwain.AutoScroll = true;
this.dynamicDotNetTwain.VScrollBar = true;
this.dynamicDotNetTwain.AutoSize = false;
this.dynamicDotNetTwain.AutoSizeMode = AutoSizeMode.GrowOnly;
this.dynamicDotNetTwain.IfShowCancelDialogWhenImageTransfer = false;
this.dynamicDotNetTwain.IfShowPrintUI = false;
this.dynamicDotNetTwain.IfThrowException = false;
this.dynamicDotNetTwain.LogLevel = ((short)(0));
this.dynamicDotNetTwain.Name = "dynamicDotNetTwain";
this.dynamicDotNetTwain.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.dynamicDotNetTwain.Visible = true;
this.dynamicDotNetTwain.OnImageAreaDeselected += new Dynamsoft.DotNet.TWAIN.Delegate.OnImageAreaDeselectedHandler(this.dynamicDotNetTwain_OnImageAreaDeselected);
this.dynamicDotNetTwain.OnMouseClick += new Dynamsoft.DotNet.TWAIN.Delegate.OnMouseClickHandler(this.dynamicDotNetTwain_OnMouseClick);
this.dynamicDotNetTwain.OnMouseDoubleClick += new Dynamsoft.DotNet.TWAIN.Delegate.OnMouseDoubleClickHandler(this.dynamicDotNetTwain_OnMouseDoubleClick);
this.dynamicDotNetTwain.OnMouseRightClick += new Dynamsoft.DotNet.TWAIN.Delegate.OnMouseRightClickHandler(this.dynamicDotNetTwain_OnMouseRightClick);
this.dynamicDotNetTwain.OnPostAllTransfers += new Dynamsoft.DotNet.TWAIN.Delegate.OnPostAllTransfersHandler(this.dynamicDotNetTwain_OnPostAllTransfers);
this.dynamicDotNetTwain.OnSourceUIClose += new Dynamsoft.DotNet.TWAIN.Delegate.OnSourceUICloseHandler(this.dynamicDotNetTwain_OnSourceUIClose);
scanContainer.Panel2.Controls.Add(dynamicDotNetTwain);
InitUI();
InitDefaultValueForTWAIN();
}
#endregion
#region dynamicDotNetTwain_OnMouseClick
private void dynamicDotNetTwain_OnMouseClick(short sImageIndex)
{
if (dynamicDotNetTwain.CurrentImageIndexInBuffer != currentImageIndex)
checkImageCount();
}
#endregion
#region dynamicDotNetTwain_OnSourceUIClose
private void dynamicDotNetTwain_OnSourceUIClose()
{
EnableControls(picboxScan);
}
#endregion
#region dynamicDotNetTwain_OnPostAllTransfers
private void dynamicDotNetTwain_OnPostAllTransfers()
{
CrossThreadOperationControl crossDelegate = delegate ()
{
dynamicDotNetTwain.Visible = true;
checkImageCount();
EnableControls(picboxScan);
};
this.Invoke(crossDelegate);
}
#endregion
#region dynamicDotNetTwain_OnMouseDoubleClick
private void dynamicDotNetTwain_OnMouseDoubleClick(short sImageIndex)
{
try
{
Rectangle rc = dynamicDotNetTwain.GetSelectionRect(sImageIndex);
if (isToCrop && !rc.IsEmpty)
{
cropPicture(sImageIndex, rc);
}
isToCrop = false;
}
catch
{
}
EnableAllFunctionButtons();
}
#endregion
#region dynamicDotNetTwain_OnMouseRightClick
private void dynamicDotNetTwain_OnMouseRightClick(short sImageIndex)
{
if (isToCrop)
isToCrop = false;
dynamicDotNetTwain.ClearSelectionRect(sImageIndex);
EnableAllFunctionButtons();
}
#endregion
#region dynamicDotNetTwain_OnImageAreaDeselected
private void dynamicDotNetTwain_OnImageAreaDeselected(short sImageIndex)
{
if (isToCrop)
isToCrop = false;
EnableAllFunctionButtons();
}
#endregion
#region InitUI
private void InitUI()
{
DisableAllFunctionButtons();
#region View mode
this.cbxLayout.Items.Clear();
this.cbxLayout.Items.Insert(0, "1 x 1");
this.cbxLayout.Items.Insert(1, "2 x 2");
this.cbxLayout.Items.Insert(2, "3 x 3");
this.cbxLayout.Items.Insert(3, "4 x 4");
this.cbxLayout.Items.Insert(4, "5 x 5");
#endregion
#region Resolution
this.cbxResolution.Items.Clear();
this.cbxResolution.Items.Insert(0, "150");
this.cbxResolution.Items.Insert(1, "200");
this.cbxResolution.Items.Insert(2, "300");
this.cbxResolution.SelectedIndex = cbxResolution.FindStringExact("200");
#endregion
#region Info label for buttons
infoLabel = new Label();
infoLabel.Text = "";
infoLabel.Visible = false;
infoLabel.AutoSize = true;
infoLabel.Name = "Info";
infoLabel.BackColor = System.Drawing.Color.White;
infoLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
infoLabel.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
infoLabel.BringToFront();
this.Controls.Add(infoLabel);
#endregion
}
#endregion
#region InitDefaultValueForTWAIN
private void InitDefaultValueForTWAIN()
{
try
{
dynamicDotNetTwain.SupportedDeviceType = Dynamsoft.DotNet.TWAIN.Enums.EnumSupportedDeviceType.SDT_ALL;
dynamicDotNetTwain.ScanInNewProcess = true;
dynamicDotNetTwain.IfFitWindow = true;
dynamicDotNetTwain.MouseShape = false;
dynamicDotNetTwain.SetViewMode(-1, -1);
if (dynamicDotNetTwain.SourceCount > 0)
{
bool hasTwainSource = false;
bool hasWebcamSource = false;
cbxSource.Items.Clear();
for (int i = 0; i < dynamicDotNetTwain.SourceCount; ++i)
{
cbxSource.Items.Add(dynamicDotNetTwain.SourceNameItems((short)i));
Dynamsoft.DotNet.TWAIN.Enums.EnumDeviceType enumDeviceType = dynamicDotNetTwain.GetSourceType((short)i);
if (enumDeviceType == Dynamsoft.DotNet.TWAIN.Enums.EnumDeviceType.SDT_TWAIN)
hasTwainSource = true;
else if (enumDeviceType == Dynamsoft.DotNet.TWAIN.Enums.EnumDeviceType.SDT_WEBCAM)
hasWebcamSource = true;
}
if (hasTwainSource)
{
cbxSource.Enabled = true;
chkShowUI.Enabled = true;
chkADF.Enabled = true;
chkDuplex.Enabled = true;
cbxResolution.Enabled = true;
rdbtnBW.Checked = true;
EnableControls(this.picboxScan);
}
cbxSource.SelectedIndex = 0;
dynamicDotNetTwain.SelectSourceByIndex((short)cbxSource.SelectedIndex);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region ScanDocument
public void ScanDocument()
{
savingLabel.Visible = false;
dynamicDotNetTwain.RemoveAllImages();
checkImageCount();
}
#endregion
#region DocumentScanner_DoubleClick
private void DocumentScanner_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("Version 2.0.0.0");
}
#endregion
#region Picture manipulation buttons
#region picboxHand_Click
private void picboxHand_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.MouseShape = true;
dynamicDotNetTwain.AnnotationType = Dynamsoft.DotNet.TWAIN.Enums.DWTAnnotationType.enumNone;
}
#endregion
#region picboxPoint_Click
private void picboxPoint_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.MouseShape = false;
dynamicDotNetTwain.AnnotationType = Dynamsoft.DotNet.TWAIN.Enums.DWTAnnotationType.enumNone;
}
#endregion
#region picboxRotateRight_Click
private void picboxRotateRight_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.RotateRight(dynamicDotNetTwain.CurrentImageIndexInBuffer);
}
#endregion
#region picboxRotateLeft_Click
private void picboxRotateLeft_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.RotateLeft(dynamicDotNetTwain.CurrentImageIndexInBuffer);
}
#endregion
#region picboxCut_Click
private void picboxCut_Click(object sender, EventArgs e)
{
picboxPoint_Click(sender, null);
Rectangle rc = dynamicDotNetTwain.GetSelectionRect(dynamicDotNetTwain.CurrentImageIndexInBuffer);
if (rc.IsEmpty)
{
MessageBox.Show("Please select the rectangle area first!", "Warning Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
dynamicDotNetTwain.CutFrameToClipboard(dynamicDotNetTwain.CurrentImageIndexInBuffer, rc.Left, rc.Top, rc.Right, rc.Bottom);
}
}
#endregion
#region picboxCrop_Click
private void picboxCrop_Click(object sender, EventArgs e)
{
picboxPoint_Click(sender, null);
Rectangle rc = dynamicDotNetTwain.GetSelectionRect(dynamicDotNetTwain.CurrentImageIndexInBuffer);
if (rc.IsEmpty)
{
MessageBox.Show("Please select the rectangle area first!", "Warning Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
cropPicture(dynamicDotNetTwain.CurrentImageIndexInBuffer, rc);
}
}
#endregion
#region cropPicture
private void cropPicture(int imageIndex, Rectangle rc)
{
dynamicDotNetTwain.Crop((short)imageIndex, rc.X, rc.Y, rc.X + rc.Width, rc.Y + rc.Height);
}
#endregion
#region picboxDelete_Click
private void picboxDelete_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.RemoveImage(dynamicDotNetTwain.CurrentImageIndexInBuffer);
checkImageCount();
}
#endregion
#region picboxDeleteAll_Click
private void picboxDeleteAll_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.RemoveAllImages();
checkImageCount();
}
#endregion
#region picboxFirst_Click
private void picboxFirst_Click(object sender, EventArgs e)
{
if (dynamicDotNetTwain.HowManyImagesInBuffer > 0)
dynamicDotNetTwain.CurrentImageIndexInBuffer = (short)0;
checkImageCount();
}
#endregion
#region picboxPrevious_Click
private void picboxPrevious_Click(object sender, EventArgs e)
{
if (dynamicDotNetTwain.HowManyImagesInBuffer > 0 && dynamicDotNetTwain.CurrentImageIndexInBuffer > 0)
--dynamicDotNetTwain.CurrentImageIndexInBuffer;
checkImageCount();
}
#endregion
#region picboxNext_Click
private void picboxNext_Click(object sender, EventArgs e)
{
if (dynamicDotNetTwain.HowManyImagesInBuffer > 0 &&
dynamicDotNetTwain.CurrentImageIndexInBuffer < dynamicDotNetTwain.HowManyImagesInBuffer - 1)
++dynamicDotNetTwain.CurrentImageIndexInBuffer;
checkImageCount();
}
#endregion
#region picboxLast_Click
private void picboxLast_Click(object sender, EventArgs e)
{
if (dynamicDotNetTwain.HowManyImagesInBuffer > 0)
dynamicDotNetTwain.CurrentImageIndexInBuffer = (short)(dynamicDotNetTwain.HowManyImagesInBuffer - 1);
checkImageCount();
}
#endregion
#region picboxFit_Click
private void picboxFit_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.IfFitWindow = true;
checkZoom();
}
#endregion
#region picboxOriginalSize_Click
private void picboxOriginalSize_Click(object sender, EventArgs e)
{
dynamicDotNetTwain.SetViewMode(-1, -1);
dynamicDotNetTwain.IfFitWindow = false;
dynamicDotNetTwain.Zoom = 1;
checkZoom();
}
#endregion
#endregion
#region Helper Functions
#region checkImageCount
///
/// If the image count changed, some features should changed.
///
private void checkImageCount()
{
currentImageIndex = dynamicDotNetTwain.CurrentImageIndexInBuffer;
int currentIndex = currentImageIndex + 1;
int imageCount = dynamicDotNetTwain.HowManyImagesInBuffer;
if (imageCount == 0)
currentIndex = 0;
tbxCurrentImageIndex.Text = currentIndex.ToString();
tbxTotalImageNum.Text = imageCount.ToString();
if (imageCount > 0)
{
EnableAllFunctionButtons();
}
else
{
DisableAllFunctionButtons();
}
if (imageCount > 1)
{
EnableControls(picboxFirst);
EnableControls(picboxLast);
EnableControls(picboxPrevious);
EnableControls(picboxNext);
if (currentIndex == 1)
{
DisableControls(picboxPrevious);
DisableControls(picboxFirst);
}
if (currentIndex == imageCount)
{
DisableControls(picboxNext);
DisableControls(picboxLast);
}
}
else
{
DisableControls(picboxFirst);
DisableControls(picboxLast);
DisableControls(picboxPrevious);
DisableControls(picboxNext);
}
}
#endregion
#region DisableControls
private void DisableControls(object sender)
{
if (sender is PictureBox)
{
(sender as PictureBox).Image =
(Image)Afx.ActiveX.DocumentScanner.Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Disabled");
(sender as PictureBox).Enabled = false;
}
else
{
(sender as Control).Enabled = false;
}
}
#endregion
#region EnableControls
private void EnableControls(object sender)
{
if (sender is PictureBox)
{
(sender as PictureBox).Image =
(Image)Afx.ActiveX.DocumentScanner.Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Leave");
(sender as PictureBox).Enabled = true;
}
else
{
(sender as Control).Enabled = true;
}
}
#endregion
#region DisableAllFunctionButtons
///
/// Disable all the function buttons in the left and bottom panel
///
private void DisableAllFunctionButtons()
{
DisableControls(this.picboxHand);
DisableControls(this.picboxPoint);
DisableControls(this.picboxCrop);
DisableControls(this.picboxCut);
DisableControls(this.picboxRotateRight);
DisableControls(this.picboxRotateLeft);
DisableControls(this.picboxFit);
DisableControls(this.picboxOriginalSize);
DisableControls(this.picboxDelete);
DisableControls(this.picboxDeleteAll);
DisableControls(this.picboxFirst);
DisableControls(this.picboxPrevious);
DisableControls(this.picboxNext);
DisableControls(this.picboxLast);
}
#endregion
#region EnableAllFunctionButtons
///
/// Enable all the function buttons in the left and bottom panel
///
private void EnableAllFunctionButtons()
{
EnableControls(this.picboxHand);
EnableControls(this.picboxPoint);
EnableControls(this.picboxCrop);
EnableControls(this.picboxCut);
EnableControls(this.picboxRotateRight);
EnableControls(this.picboxRotateLeft);
EnableControls(this.picboxFit);
EnableControls(this.picboxOriginalSize);
EnableControls(this.picboxDelete);
EnableControls(this.picboxDeleteAll);
if (dynamicDotNetTwain.HowManyImagesInBuffer > 1)
{
EnableControls(this.picboxFirst);
EnableControls(this.picboxPrevious);
EnableControls(this.picboxNext);
EnableControls(this.picboxLast);
if (dynamicDotNetTwain.CurrentImageIndexInBuffer == 0)
{
DisableControls(picboxPrevious);
DisableControls(picboxFirst);
}
if (dynamicDotNetTwain.CurrentImageIndexInBuffer + 1 == dynamicDotNetTwain.HowManyImagesInBuffer)
{
DisableControls(picboxNext);
DisableControls(picboxLast);
}
}
}
#endregion
#region checkZoom
private void checkZoom()
{
////if (cbxLayout.SelectedIndex != 0 || dynamicDotNetTwain.HowManyImagesInBuffer == 0)
////// || cbxViewMode.SelectedIndex != 0)
////{
//// //DisableControls(picboxZoomIn);
//// //DisableControls(picboxZoomOut);
//// //DisableControls(picboxZoom);
//// DisableControls(picboxFit);
//// DisableControls(picboxOriginalSize);
//// return;
////}
if (picboxFit.Enabled == false)
EnableControls(picboxFit);
if (picboxOriginalSize.Enabled == false)
EnableControls(picboxOriginalSize);
//if (picboxZoom.Enabled == false)
// EnableControls(picboxZoom);
//// the valid range of zoom is between 0.02 to 65.0,
//if (dynamicDotNetTwain.Zoom <= 0.02F)
//{
// DisableControls(picboxZoomOut);
//}
//else
//{
// EnableControls(picboxZoomOut);
//}
//if (dynamicDotNetTwain.Zoom >= 65F)
//{
// DisableControls(picboxZoomIn);
//}
//else
//{
// EnableControls(picboxZoomIn);
//}
}
#endregion
#endregion
#region cbxSource_SelectedIndexChanged
private void cbxSource_SelectedIndexChanged(object sender, EventArgs e)
{
short sIndex = (short)((ComboBox)(sender)).SelectedIndex;
switch (dynamicDotNetTwain.GetSourceType(sIndex))
{
case Dynamsoft.DotNet.TWAIN.Enums.EnumDeviceType.SDT_TWAIN:
dynamicDotNetTwain.CloseSource();//when switching from webcam source to twain source, need to close webcam source.
break;
case Dynamsoft.DotNet.TWAIN.Enums.EnumDeviceType.SDT_WEBCAM:
//panelScan.Visible = false;
//panelGrab.Visible = true;
//lbUnknowSource.Visible = false;
////Initial media type list and webcam resolution list
//cbxMediaType.Items.Clear();
//cbxResolutionForWebcam.Items.Clear();
//dynamicDotNetTwain.IfDisableSourceAfterAcquire = false;//don't close video after grabbing an image.
//dynamicDotNetTwain.SelectSourceByIndex(sIndex);
//dynamicDotNetTwain.OpenSource(); //Open webcam source before getting the value of MediaTypeList and ResolutionForCamList
//List lstMediaTypes = dynamicDotNetTwain.MediaTypeList;
//List lstWebcamResolutions = dynamicDotNetTwain.ResolutionForCamList;
//if (lstMediaTypes != null)
// foreach (string strMediaType in lstMediaTypes)
// cbxMediaType.Items.Add(strMediaType);
//if (lstWebcamResolutions != null)
// foreach (Dynamsoft.DotNet.TWAIN.WebCamera.CamResolution camResolution in lstWebcamResolutions)
// cbxResolutionForWebcam.Items.Add(camResolution.Width + " X " + camResolution.Height);
//if (cbxMediaType.Items.Count > 0)
// cbxMediaType.SelectedIndex = 0;
//if (cbxResolutionForWebcam.Items.Count > 0)
// cbxResolutionForWebcam.SelectedIndex = 0;
////show error information
//if (dynamicDotNetTwain.ErrorCode != Dynamsoft.DotNet.TWAIN.Enums.ErrorCode.Succeed)
// MessageBox.Show(dynamicDotNetTwain.ErrorString, "Webcam error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
#endregion
#region Button hover etc. functions
#region picbox_MouseEnter
private void picbox_MouseEnter(object sender, EventArgs e)
{
if (sender is PictureBox)
{
if ((sender as PictureBox).Enabled == true)
{
(sender as PictureBox).Image =
(Image)Afx.ActiveX.DocumentScanner.Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Enter");
}
}
}
#endregion
#region picbox_MouseDown
private void picbox_MouseDown(object sender, MouseEventArgs e)
{
if (sender is PictureBox)
{
if ((sender as PictureBox).Enabled == true)
{
(sender as PictureBox).Image = (Image)Afx.ActiveX.DocumentScanner.Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Down");
}
}
}
#endregion
#region picbox_MouseLeave
private void picbox_MouseLeave(object sender, EventArgs e)
{
if (sender is PictureBox)
{
if ((sender as PictureBox).Enabled == true)
{
(sender as PictureBox).Image =
(Image)Afx.ActiveX.DocumentScanner.Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Leave");
infoLabel.Text = "";
infoLabel.Visible = false;
}
}
}
#endregion
#region picbox_MouseUp
private void picbox_MouseUp(object sender, MouseEventArgs e)
{
if (sender is PictureBox)
{
if ((sender as PictureBox).Enabled == true)
{
(sender as PictureBox).Image =
(Image)Properties.Resources.ResourceManager.GetObject((sender as PictureBox).Name + "_Enter");
}
}
}
#endregion
#region picbox_MouseHover
private void picbox_MouseHover(object sender, EventArgs e)
{
infoLabel.Text = (sender as PictureBox).Tag.ToString();
infoLabel.Location = new Point(this.PointToClient(MousePosition).X, this.PointToClient(MousePosition).Y + 20);
infoLabel.Visible = true;
infoLabel.BringToFront();
}
#endregion
#endregion
#region picboxScan_Click
public void picboxScan_Click(object sender, EventArgs e)
{
if (picboxScan.Enabled)
{
picboxScan.Focus();
if (this.cbxSource.SelectedIndex < 0)
{
MessageBox.Show(this, "There is no scanner detected!\n " +
"Please ensure that at least one (virtual) scanner is installed.", "Information");
}
else
{
DisableControls(picboxScan);
this.AcquireImage();
}
}
}
#endregion
#region AcquireImage
//
/// Acquire image from the selected source
///
private void AcquireImage()
{
try
{
dynamicDotNetTwain.SelectSourceByIndex((short)cbxSource.SelectedIndex);
dynamicDotNetTwain.OpenSource();
dynamicDotNetTwain.IfShowUI = chkShowUI.Checked;
dynamicDotNetTwain.IfAutoContrast = true;
dynamicDotNetTwain.IfFeederEnabled = chkADF.Checked;
dynamicDotNetTwain.IfDuplexEnabled = chkDuplex.Checked;
dynamicDotNetTwain.IfDisableSourceAfterAcquire = true;
if (rdbtnBW.Checked)
{
dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_BW;
dynamicDotNetTwain.BitDepth = 1;
}
else if (rdbtnGray.Checked)
{
dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_GRAY;
dynamicDotNetTwain.BitDepth = 8;
}
else
{
dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_RGB;
dynamicDotNetTwain.BitDepth = 24;
}
dynamicDotNetTwain.Resolution = int.Parse(cbxResolution.Text);
if (!dynamicDotNetTwain.AcquireImage())
{
MessageBox.Show(dynamicDotNetTwain.ErrorString, "Scan error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
EnableControls(picboxSave);
}
catch (System.Exception ex)
{
MessageBox.Show("An exception occurred: " + ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (dynamicDotNetTwain.ErrorCode != Dynamsoft.DotNet.TWAIN.Enums.ErrorCode.Succeed)
EnableControls(picboxScan);
}
}
#endregion
#region cbxLayout_SelectedIndexChanged
private void cbxLayout_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.cbxLayout.SelectedIndex)
{
case 0:
dynamicDotNetTwain.SetViewMode(-1, -1);
break;
case 1:
dynamicDotNetTwain.SetViewMode(2, 2);
break;
case 2:
dynamicDotNetTwain.SetViewMode(3, 3);
break;
case 3:
dynamicDotNetTwain.SetViewMode(4, 4);
break;
case 4:
dynamicDotNetTwain.SetViewMode(5, 5);
break;
default:
dynamicDotNetTwain.SetViewMode(-1, -1);
break;
}
checkZoom();
}
#endregion
#region saveButton_Click
private void saveButton_Click(object sender, EventArgs e)
{
try
{
picboxSave.Enabled = false;
savingLabel.Visible = true;
if (FileAcquired != null)
{
string myDocuments = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".pdf");
var indexList = new IndexList();
for (int x = 0; x <= dynamicDotNetTwain.HowManyImagesInBuffer - 1; x++)
{
indexList.Add(x);
}
var data = dynamicDotNetTwain.SaveAsMultiPagePDFToBytes(indexList);
FileAcquired("", fileName, ByteFunctions.ByteArrayToHexString(data));
}
else
{
MessageBox.Show("error saving image. FileAcquired handler has not been defined");
}
}
catch (Exception ex)
{
MessageBox.Show("error saving image: " + dynamicDotNetTwain.ErrorString + "." + ex.Message);
}
}
#endregion
}
}