Question How to scan a document

momer79

New member
Joined
Sep 14, 2009
Messages
2
Programming Experience
3-5
Hi, I need to write a application which can scan a document and save it on hard drive.

How can i do it in VB.net?

Thanks
 
Code is as follows: (Note: This implementation of the code may need to call atalasoft.com / components)

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data

Imports Atalasoft.Imaging
Imports Atalasoft.Imaging.Codec
Imports Atalasoft.Imaging.Codec.Pdf
Imports Atalasoft.Twain

Namespace SimpleWinScan
Public Class Form1 : Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code"
Private WithEvents button1 As System.Windows.Forms.Button
''' <summary>
''' Required designer variable.
''' </summary>
Private components As System.ComponentModel.Container = Nothing

Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()

'
' TODO: Add any constructor code after InitializeComponent call
'
End Sub

''' <summary>
''' Clean up any resources being used.
''' </summary>
Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub


''' <summary>
''' Required method for Designer support - do not modify
''' the contents of this method with the code editor.
''' </summary>
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
' button1
'
Me.button1.Location = New System.Drawing.Point(56, 16)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(88, 23)
Me.button1.TabIndex = 0
Me.button1.Text = "Press To Scan"
' Me.button1.Click += New System.EventHandler(Me.button1_Click);
'
' Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(192, 62)
Me.Controls.Add(Me.button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub


''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
#End Region

Private _acquireCanceled As Boolean
Private WithEvents myAcquisition As Acquisition

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
' Delete the temp and output files if they exist
If File.Exists("outputTiff.tif") Then
File.Delete("outputTiff.tif")
End If
If File.Exists("outputPdf.pdf") Then
File.Delete("outputPdf.pdf")
End If

' reset for the current acquisition
myAcquisition = New Acquisition(Me)
_acquireCanceled = False

' Let the user select the device to use
Dim activeDevice As Device = myAcquisition.ShowSelectSource()

' acquire the Image
activeDevice.Acquire()
End Sub

Private Sub OnImageAcquired(ByVal sender As Object, ByVal e As AcquireEventArgs) Handles myAcquisition.ImageAcquired
' make sure the image exists
If e.Image Is Nothing Then
Return
End If

' append each image as a page onto an existing tiff file, or create the file if it doesn't exist
Dim enc As TiffEncoder = New TiffEncoder(TiffCompression.Default, True)
Dim fs As FileStream = New FileStream("outputTiff.tif", FileMode.OpenOrCreate, FileAccess.ReadWrite)
enc.Save(fs, AtalaImage.FromBitmap(e.Image), Nothing)
fs.Close()
End Sub

Private Sub OnAcquireCanceled(ByVal sender As Object, ByVal e As EventArgs) Handles myAcquisition.AcquireCanceled
_acquireCanceled = True
End Sub

Private Sub OnAcquireFinished(ByVal sender As Object, ByVal e As EventArgs) Handles myAcquisition.AcquireFinished
If _acquireCanceled Then
Return
End If

' convert the tiff image to PDF
Dim col As PdfImageCollection = New PdfImageCollection
Dim dec As TiffDecoder = New TiffDecoder
Dim fs As FileStream = New FileStream("outputTiff.tif", FileMode.Open, FileAccess.Read)
Dim frameCount As Integer = dec.GetFrameCount(fs)
fs.Close()
' Create a PdfImageCollection containing one PdfImage for each page of the tiff
Dim i As Integer
For i = 0 To (frameCount - 1)
col.Add(New PdfImage("outputTiff.tif", i, PdfCompressionType.Auto))
Next i

Dim outStream As FileStream = New FileStream("outputPdf.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim enc As PdfEncoder = New PdfEncoder
' save the final PDF file
enc.Save(outStream, col, Nothing)
outStream.Close()

' we're done!
MessageBox.Show("Finished Scanning")
End Sub
End Class
End Namespace

_____________________
Sabrina Gage
 
@Sabrina:
I don't wanna discourage or critic you, but if he does not want to pay some hundred bucks for TwainX, he most likely wouldn't want to spend some thousands on Atalasoft. ;)

Bobby
 
Back
Top