Blue Theme Orange Theme Green Theme Red Theme
 
ANTS Performance Profiler 6.0
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
ASP.Net 4 Hosting is here
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » VB.NET » Getting Images from Scanners and Webcams using Visual Basic 2005

Getting Images from Scanners and Webcams using Visual Basic 2005


In this article I will show how to acquire pictures from imaging peripherals like scanners, webcams and so on, by calling some API functions, provided by the EZTW32.DLL library, and Visual Basic 2005.

Total page views :  58468
Total downloads :  2681
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ads_EzTwainLibrary.zip
 
Become a Sponsor

Introduction.

In this article I will show how to acquire pictures from imaging peripherals like scanners, webcams and so on, by calling some API functions, provided by the EZTW32.DLL library, and Visual Basic 2005.

To perform this operations we're going to create a wrapper that will expose methods based on the above mentioned API functions. Another purpose of this article is to show how to create CLS-Compliant assemblies and classes covering some code writing guidelines and some executable settings. Don't worry if any of the words above sound new. In this article you will find the right explanation about everything.

But we won't stop at this. We will also see how to use an assembly analysis tool to validate our executable. We will moreover talk about XML comments, a new feature of VB IDE.

To continue reading this article, download the source code from the link above. The archive contains two projects: the class library and a Windows Forms sample, called Esempio2005 (in Italian Esempio means Sample).

If yet not, install the Microsoft .NET Framework 2.0. And obviously you need to check if the EZTW32.DLL is already present on your machine. You should find this library in the System directory (e.g.: "C:\Windows\System32"). If you have one or more imaging peripherals installed, you should already have this library. If this is not your case, you can download the library from the following URL: http://www.dosadi.com/eztwain1.htm.

On this web site you can find different versions of the library, but only the one linked above is freeware. Its last updated was in 1999 but for our purpose it is really good.

Acquiring pictures via Visual Basic 2005 code.

The best project type that fits our purpose is a Class Library, so that we will obtain a compiled Dll. Give the project the name you like more, I used EZTwainLibrary.

Just one code file is what we need. But before showing the code I think it's important to tell something more about the Framework and the Common Language Specifications (by now just CLS).

Everything that follows has the purpose to fit as more as possible the CLS. According to the CLS, API declarations, called pinvokes in .NET, must be declared in a separated class. This class must have friend accessibility and its name must be one of the following conventional identifiers: NativeMethods, SafeNativeMethods, UnsafeNativeMethods.

This means that we need to create two classes in the same file. First of all we have to declare a new namespace, called EZTwainLibrary as in the sample code. This namespace contains the main class, EZTwain, with public accessibility, which implements methods and properties for interfacing with imaging peripherals.

A second class is called NativeMethods and contains the API declarations for using the EZTW32.DLL.

Let's begin to write the code, line by line. First of all a pair of Imports directives:

Imports System

Imports System.Runtime.InteropServices

Next step is to mark the assembly as CLS-Compliant.

Some features of Common Language Specifications.

As mentioned before, one of this article's purposes is to show how to compile a CLS-Compliant assembly. This means that we have to write compliant code from the beginning. Next steps show what and how to do it.

  • According to the CLS an assembly should be marked as CLS-Compliant. This assures a full compatibility with the .NET platform and can be done by using two attributes: Assembly and CLSCompliant.

  • EZTwain class name: we could choose any other fantasy name but letters capitalization is not random. In fact, always according to the CLS, class names are pascal-cased; if composed of more than one word, the first letter of every word is upper case while the others are lower case, as in EZ and Twain. But there is an exception: when the first word is made of two characters, as in our case, they must be both upper case.

  • Why NativeMethods: as seen before, according to .NET specifications, all pinvokes must reside in a class identified with one of the conventional names mentioned above.

Now it's time to mark the assembly as CLS-Compliant. The following code must be put after Imports directives:

'Marks the assembly as CLS-Compliant
<Assembly: CLSCompliant(True)>

Then a new namespace, that will contain the two classes:

Namespace EZTwainLibrary

End Namespace

Namespace notation is the same as for classes.

Then comes the implementation of NativeMethods class, which declares EZTW32.DLL APIs. You can see the code inside the EzTwain.Vb file, in the downloadable archive. Please, check out the documentations link at the bottom of this article. By visiting these web pages you will find very detailed informations about what used APIs do.

As you can see in the source code, NativeMethods has Friend accessibility since it must be reached only from within the project and not outside, and the same applies to its methods. These are declared as shared because they will be used by a class that will implement all shared methods and properties.

CLS also state that a class made of shared methods only, must contain an empty and Private constructor.
We can now implement the EZTwain class, that makes the real job. You can observe the source code in the archive you downloaded before.

Pay attention to the following points:

  • The class has been marked as CLS-Compliant through the CLSCompliant attribute and declared as Public because it will expose methods and properties to the external world;

  • It has been also declared as NotInheritable, because CLS state that if a class is implementing only shared methods, it should not be inherited. The equivalent of NotInheritable in C# is the sealed keyword. It's useful to know this C# keyword because some analysis tools, like Microsoft FxCop, throw exceptions returning messages containing sealed rather than NotInheritable.

  • Methods and properties identifiers are pascal-cased; the first letter is upper case, the others are lower case as in AcquireToClipboard;

  • Parameters are camel-cased; as in outputFileName. The first letter of the first word is lower case, the first letter of the othe words is upper case;

  • There are some identifiers, called discrete, that are recognized by the compiler (since they are used in the base class library) when used by the developer in his own methods, e.g.: Filename. If we want to use a discrete identifier, we have to define it in a different way. Simply it's right to use FileName rather than Filename;

  • As in NativeMethods class, EZTwain implements an empty and Private constructor too, since it exposes only shared methods;

  • The Visual Basic compiler understands whether we create functions that make a job of Get/Set. In case of Get, properties are preferred to methods when properties implementation is not very complex and they do not use too many resources;

  • In VB 2005, methods should return values via the Return keyword rather than assigning a value to the function, even if this does not throw exceptions;

  • CLS state that we should avoid namespaces with less than five elements. In our case it was necessary to leave that namespace, because it is just a sample.

The following is a small example of how EZTwain could be used for acquiring images:

Imports EZTwainLibrary

 

Sub Scan()

 

        EZTWain.SelectTwainSource()

 

        EZTwain.AcquireToFilename(Me.Handle, "C:\MyImage.Bmp")

 

End Sub

Inside the downloadable source code you will find a sample Windows Forms application (called Esempio2005) which shows some of our assembly capabilities.

Writing methods and properties in a .NET-style.

Now it's the moment for a walkthrough inside two methods exposed by our EZTwain class: IsTwainInstalled and AcquireToNative methods.

IsTwainInstalled, which determines whether EZTW32.DLL is installed or not, calls a method implemented by the new My namespace. This is not the best place to talk about this revolutionary namespace but here I can say that My implements further classes which provide full access to many important situations, like current-user, file-system, network connections. To get the same functionalities in VB 6 we had to write hundreds lines of code, very often referring to Windows API. Now everything is easier.

AcquireNative is declared Overloads. This operator lets the developer define more instances of the same method, but with different parameters and implementations. In our case, the first overload acquires an image from a scanner, saves it to disk given the user-specified filename handling the calling window. Returns an integer value, the same returned by its related API. The second overload acquires an image from a scanner and saves to a new temporary file, handling the active window. Returns a string containing the full pathname of the new temporary file, so that the user can manage the new image file.

The Version property uses a System.Globalization.CultureInfo.InvariantCulture parameter during the conversion to string of the library version value. CLS state that when you convert a numeric value as an integer or a double to a string, calling the .ToString method, it's important to specify a valid string format depending on the client operanting system culture.

This can be done implementing the IFormatProvider interface, which provides methods for strings formatting. In our code sample we used the System.Globalization.CultureInfo.InvariantCulture  type to return a culture-indipendent string.

XML comments.

Being superior to its predecessors Visual Basic 2005 lets you add XML-formatted comments while writing code.  If you know Visual Basic .NET 2003, you can remember how this was possible only installing an IDE add-in called VB-Commenter, while this feature was always part of Visual C# development. Finally this feature has been implemented directly into Visual Basic 2005 IDE. Documenting source code through XML comments can be very useful if you develop Dlls instead of stand-alone applications. In fact there are some programs, like NDoc, which can create compiled help files starting from XML comments, building compiled files very similar to Visual Studio documentation, where every namespace or class is shown describing its methods, properties and so on.

To insert XML comments, just put the cursor on the line before the code you want to describe and write three apostrophes. The IDE will automatically add a collapsible XML block, where you can add comments about what the coming code is implementing as shown in the following picture:


 
Signing the assembly.

To complete assembly creation it's necessary to create a strong name. This means signing the assembly with a digital sign (password protected or not). This tells the .NET Framework that the assembly comes from a certified source. In this way the .dll will be definitively CLS-Compliant.

Do you know what the Global Assembly Cache (GAC) is?. It is a folder created by the .NET Framework. In this folder reside all the registered assemblies, depending on .NET version. If an assembly is in the GAC it does not need to be copied every time into the folder where resides the application that uses it as reference, since the Common Language Runtime already know where registered assemblies are located. If different applications reference the same assembly, you should consider to install that assembly into the GAC.

Whenever we would like to install our assembly into the GAC, it's necessary to sign it. Visual Studio 2005 lets you make this operations from within the IDE. In the previous versions of Visual Studio .NET this could be done adding some particular lines of code into the AssemblyInfo.Vb file and running certain command line utilities.

In Visual Studio 2005 open the "My Project" window. When you get the project properties, select the "Signing" tab. Apply a flag onto the "Sign the assembly" checkbox, create a new strong name or choose an existing one, with even a password. The IDE will make the rest of the job and you will be able to see how the new file appears in the Solution Explorer.

The source code that comes with this article has already a strong name file, called EZTwain.Pfx. It's password is strongname.

Build the solution.

After a long job we are ready to build the solution. Our assembly is now ready to use in our applications and can be also installed into the Global Assembly Cache.

Assembly analysis.

The .NET Framework SDK 2.0 complains a very useful tool, called Microsoft FxCop. This tool implements analysis tools to understand if assemblies are CLS Compliant or not. FxCop is not automatically installed together with the SDK, but a link to its setup can be find in the Start menu.

Now let's try to analyze our assembly via the Add targets command of the Project menu. Then just click on Analyze. Though we have tried to be the most sharp as possible, you we'll see how some exceptions are thrown. But this is normal since: parameters names are made of single words, but FxCop tells that this can happen even if the developer is sure to have correctly written the code. So this can be ignored.

FxCop shows warnings about unused locals, but you can see that some of these locals are added by the IDE into some hidden files so to prevent user modifies. In the other cases it is due to particular situations concerning API functions.

Since we got a few warnings, for the reasons I just talked about, we can say that our job is good. If you want to better understand how analysis results can change, just remove CLSCompliant attributes or the NotInheritable keyword or change properties to methods or put all pinvokes into EZTwain class removing the NativeMethods one. If you repeat the analysis now, you will get many more exceptions.

Red-marked errors are more dangerous, while blue errors can be corrected in a second time...but don't forget it! For further details about every error just click on each message. If you are connected to the Internet you will be able to use hyperlinks to be redirected to the application's web site.

Microsoft FxCop is hosted on workspace onto the GotDotNet web site, at the following URL: http://www.gotdotnet.com/team/fxcop/ and can be downloaded stand-alone too, outside the Framework SDK.

Documentation.

Here is a small list of web sites where I found further informations about EZTW32.DLL API:

It's really important to visit the PowerBasic Support Forum above because inside the provided .bas module you can find very detailed comments about what EZTW32.DLL APIs do, so to better understand also my source code.

Conclusions.

In this article we learnt some important features about .NET Framework, Common Language Specifications and we saw how to analyze an assembly to get it .NET compliant.


Login to add your contents and source code to this article
 About the author
 
Alessandro Del Sole
Alessandro Del Sole is just an hobbiest developer. He is interested in .NET Framework and Visual Basic technologies. You can visit him at http://community.visual-basic.it/Alessandro
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
ASP.Net 4 Hosting is here
Become a Sponsor
 Comments
help me by thie On February 23, 2007
please help me to code my project. about how to scan a documents and drawing.. if you know this please help me.. email to me the code.. thank you very much..
Reply | Email | Delete | Modify | 
please help me too by dovan On April 7, 2007
my name dovan sorry my english not perfect. please if you know how to scan a documents and image using VB.Net 2005 email me the code to dovan_fatra@yahoo.co.id and if you know what the password of EZTwain library. Thanks a lot
Reply | Email | Delete | Modify | 
Re: please help me too by Alessandro On May 6, 2007

I think you need an OCR library.  Free version of EzTw32.dll is not that case... sorry.

The password for EzTwainLibrary's strong-name should be vbt&t but if it's not that, remove the .pfx file and add a new one with a password of yours.

Alessandro

Reply | Email | Delete | Modify | 
using your ezTwainlibrary by Rennen On February 16, 2010
thanx about your ezTwainLibrary. its goood and its working

how about ill save it to the specific folder??
what is the code??

thanks again

Reply | Email | Delete | Modify | 
Getting multiple images from scanner, Please help by Juan On April 5, 2010
Alessandro,

I found your article "Getting Images from Scanners and Webcams using Visual Basic 2005" at http://www.vbdotnetheaven.com/uploadfile/progalex/ads_acquirepicturesvb200502032006170523pm/ads_acquirepicturesvb2005.aspx?login=true&user=jdmira

Great article, well explained and very easy to integrate. I downloaded all the files and the project works perfect, I just have one question: when I scan more than one page from the scanner's feeder, all the pages are scanned successfully but the program only saves the first page that was scanned, so I end up with one image file only. So my question is, how can I scan more than one page and be able to save all those pages. I would greatly appreciate if you could help me with this issue.


Thank you very much for your time.


Best regards,


Juan Mira
Reply | Email | Delete | Modify | 
vb.net by subin On July 2, 2010
how to get data from scanned image(data means numbers that specifiy the length,width,etc of that image)in vb.net
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.