Last Updated on 2018-10-10
We talked about TWAIN and how to use TWAIN in .NET application in a previous article. Now, let’s make a step further to see how to implement webcam image capture into a .NET application by using Dynamic .NET TWAIN SDK specifically.
If you don’t have Dynamic .NET TWAIN on your machine, you can first download the 30-day free trial and install the component.
1. Launch Visual Studio and create a simple Windows Forms Application. We will create a C# project here as an example.
2. Add Dynamic .NET TWAIN component to the Visual Studio Toolbox. Learn more
3. Add MenuStrip, ContextMenuStrip, Button, PictureBox, Dynamic .NET TWAIN component from Toolbox. The PictureBox is placed to display preview data from Webcam, and the Dynamic .NET TWAIN Component is placed to display acquired image from Webcam or Scanner.
4. Double click MenuStrip to initialize a context menu for displaying the imaging devices.
[javascript]dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_ALL; // list both TWAIN devices and webcams
short count = dynamicDotNetTwain1.SourceCount; // check the count of available imaging devices
string name = null;
ToolStripMenuItem item = null;
contextMenu.Items.Clear();
for (short i = 0; i < count; i++) { name = dynamicDotNetTwain1.SourceNameItems(i); // get source name item = new ToolStripMenuItem(name); // create a menu item contextMenu.Items.Add(item); // add menu item to context menu item.Click += new System.EventHandler(toolStripMenuItem1_DropDownItemClicked); // add item listener } // show context menu if (count > 0)[/javascript]
5. Run the application, and take a look at how many devices detected by .NET TWAIN API.
6. If there is no imaging device connected, you may download and install a virtual scanner – “TWAIN2 FreeImage Software Scanner” – for testing. After configuring parameters, click “scan” to capture images using Dynamic .NET TWAIN Component from the selected scanner or webcam.
[javascript]void toolStripMenuItem1_DropDownItemClicked(object sender, System.EventArgs e)
{
btnAcquireSource.Enabled = true;
contextMenu.Hide();
int index = contextMenu.Items.IndexOf((ToolStripDropDownItem)sender); // get source index
EnumDeviceType type = dynamicDotNetTwain1.GetSourceType((short)index); // get source type
dynamicDotNetTwain1.SelectSourceByIndex(index); // select source by index
dynamicDotNetTwain1.CloseSource();
dynamicDotNetTwain1.IfThrowException = true;
dynamicDotNetTwain1.IfShowUI = true; // show the user interface of the device
if (type == EnumDeviceType.SDT_WEBCAM)
{
dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM;
dynamicDotNetTwain1.SetVideoContainer(this.pictureBox1); // display preview data from Webcam
}
else if (type == EnumDeviceType.SDT_TWAIN)
{
dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_TWAIN;
}
else if (type == EnumDeviceType.SDT_UNKNOW)
{
dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_ALL;
}
dynamicDotNetTwain1.OpenSource(); // open source
}[/javascript]
[javascript]private void btnAcquireSource_Click(object sender, EventArgs e)
{
try
{
dynamicDotNetTwain1.EnableSource();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}[/javascript]
7. Alternatively, you can select a Webcam to see the preview data on PictureBox. Then click “Acquire” to get image.
8. Click “Remove all” to clear component data.
[javascript]private void btnRemoveAllImages_Click(object sender, EventArgs e)
{
dynamicDotNetTwain1.RemoveAllImages();
}[/javascript]
9. Now we can try to edit the captured image by adding “Crop”.
Here are the basic steps needed:
1) Add a button named “Crop”.
2) Add a mouse event handler to Dynamic .NET TWAIN Component.
[javascript]this.dynamicDotNetTwain1.OnImageAreaDeselected += new Dynamsoft.DotNet.TWAIN.Delegate.OnImageAreaDeselectedHandler(this.dynamicDotNetTwainView_OnImageAreaDeselected);
this.dynamicDotNetTwain1.OnImageAreaSelected += new Dynamsoft.DotNet.TWAIN.Delegate.OnImageAreaSelectedHandler(this.dynamicDotNetTwainView_OnImageAreaSelected);[/javascript]
3) Acquire an image, and select an area you need for crop.
4) Click “Crop” to get the selected part.
[javascript]private void button_crop_Click(object sender, EventArgs e)
{
if (0 == iLeft && 0 == iTop && 0 == iRight && 0 == iBottom)
{
return;
}
else {
dynamicDotNetTwain1.Crop(0, iLeft, iTop, iRight, iBottom);
iLeft = 0;
iTop = 0;
iRight = 0;
iBottom = 0;
}
}[/javascript]
Download Sample&Free Trial
Download .NET Webcam sample
Download 30-day free trial for Dynamic .NET TWAIN
Let us know in the comments section below about your experience in creating your .NET-based image capture applications.