File dallo scanner – Codice VB.NET
File dallo scanner in VB.NET: ovvero come settare lo scanner e salvare le scansioni automaticamente su file, in formato JPEG,BMP,GIF,PNG o TIFF.
Lo scanner è ormai un dispositivo presente in tutte le postazioni di pc domestici e professionali: scansionare foto, documenti, fare fotocopie è qualcosa ormai all’ordine del giorno. Come fare ricavare dei file dallo scanner?
Ci viene in aiuto la funzione, ideata da IngAC: IngAC_SCAN_TO_FILE(NOME_FILE As String, Formato_File As Byte, Qualita As Byte, DPI As Integer, Prof_Colore As Byte, Dim_X_mm As Integer, Dim_Y_mm As Integer, Pos_X_mm As Integer, Pos_Y_mm As Integer, Luminosita As Integer, Contrasto As Integer ) As String.
Grazie ad essa saremo in grado di ricavare un file dallo scanner con pochi passaggi, salvabile in vari formati, tra cui JPEG o BMP.
Come ricavare file dallo scanner: parametri e listato
La funzione IngAC_SCAN_TO_FILE ha come parametri di input:
- NOME_FILE che rappresenta il percorso del file e il nome desiderato per il file.
- Formato_File rappresenta il formato di file desiderato: 0 =JPG; 1=BMP; 2=GIF;3=PNG; 4=TIF.
- Qualita è la qualità del jpg variabile tra 1 e 100.
- DPI che rappresenta i dpi (attenzione a usare un valore supportato dal vostro scanner: un valore standard è 300).
- Prof_Colore che rappresenta la profondità di colore: 1 full color; 2 grayscale; 4 black & white.
- Dim_X_mm è la dimensione orizzontale dell’area in millimetri che si vuole scansionare (se fosse un formato A4 sarebbe 210).
- Dim_Y_mm è la dimensione verticale dell’area in millimetri che si vuole scansionare (se fosse un formato A4 sarebbe 297).
- Pos_X_mm è la posizione orizzontale (offset) in millimetri da cui si vuole iniziare a scansionare (ad esempio potremmo voler scansionare solo un quadratino 50x50mm al centro del foglio A4).
- Pos_Y_mm è la posizione verticale (offset) in millimetri da cui si vuole iniziare a scansionare (ad esempio potremmo voler scansionare solo un quadratino 50x50mm al centro del foglio A4).
- Luminosita: il valore standard è zero e varia tra – 100 e + 100.
- Contrasto: il valore standard è zero e varia tra – 100 e + 100.
In output avremo il file dallo scanner salvato nel percorso e col nome che abbiamo indicato, mentre la funzione restituisce il percorso in modo da poterlo usare in qualche picturebox direttamente.
Public Function IngAC_SCAN_TO_FILE( NOME_FILE As String, Formato_File As Byte, Qualita As Byte, DPI As Integer, Prof_Colore As Byte, Dim_X_mm As Integer, Dim_Y_mm As Integer, Pos_X_mm As Integer, Pos_Y_mm As Integer, Luminosita As Integer, Contrasto As Integer ) As String IngAC_SCAN_TO_FILE = Nothing Dim Seleziona_Dispositivo As WIA.Device = Nothing Dim Dispositivo_Scanner As WIA.Device = Nothing Dim Finestra As New WIA.CommonDialog Try Dispositivo_Scanner = Finestra.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, False, False) If Not Dispositivo_Scanner Is Nothing Then Seleziona_Dispositivo = Dispositivo_Scanner End If Catch ex As Exception MessageBox.Show(ex.Message, "Avviso!", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try 'profondità di colore: 4 Black-white, Grayscale 2, Color 1 'si imposta il rapporto dpix/dpiy=1 Dim DPI_X = DPI Dim DPI_Y = DPI ' dimensioni dell'area (max A4) Dim Dim_X_inch As Double Dim Dim_Y_inch As Double ' offset posizione x,y dell'origine (max A4) Dim Pos_X_inch As Double Dim Pos_Y_inch As Double ' conversioni da inch a mm Dim_X_inch = Int(10 * Dim_X_mm / 25.4) / 10 Dim_Y_inch = Int(10 * Dim_Y_mm / 25.4) / 10 Pos_X_inch = Pos_X_mm / 25.4 Pos_Y_inch = Pos_Y_mm / 25.4 'scansione su file If Not Dispositivo_Scanner Is Nothing Then For Each Itm In Dispositivo_Scanner.Items For Each ItmProp In Itm.Properties ' gestisce le proprietà dello scanner Select Case ItmProp.PropertyID '====================================== Case 6147 ' Risoluzione orizzontale Try ItmProp.Value = DPI_X Catch ex As Exception MessageBox.Show(ex.Message, "DPI_X Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6148 ' Risoluzione verticale Try ItmProp.Value = DPI_Y Catch ex As Exception MessageBox.Show(ex.Message, "DPI_Y Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6151 ' Scanning Area X Try ItmProp.Value = Dim_X_inch * DPI_X Catch ex As Exception MessageBox.Show(ex.Message, "DIM_X Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6152 ' Scanning Area Y Try ItmProp.Value = Dim_Y_inch * DPI_Y Catch ex As Exception MessageBox.Show(ex.Message, "DIM_Y Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6149 ' Posizione inizio scansione X Try ItmProp.Value = Pos_X_inch * DPI_X Catch ex As Exception MessageBox.Show(ex.Message, "POS_X Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6150 ' Posizione inizio scansione Y Try ItmProp.Value = Pos_Y_inch * DPI_Y Catch ex As Exception MessageBox.Show(ex.Message, "POS_Y Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6146 ' Profondita di colore Try ItmProp.Value = Prof_Colore Catch ex As Exception MessageBox.Show(ex.Message, "Prof_Colore Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6154 ' Luminosità Try ItmProp.Value = Luminosita Catch ex As Exception MessageBox.Show(ex.Message, "Lumin(-100;+100)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try '====================================== Case 6155 ' Contrast Try ItmProp.Value = Contrasto Catch ex As Exception MessageBox.Show(ex.Message, "Contr(-100;+100)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return Nothing End Try End Select Next Next ' prende il device disponibile Dim Item As WIA.Item = Dispositivo_Scanner.Items(1) Dim ImageFile As WIA.ImageFile Try ImageFile = Finestra.ShowTransfer(Item, , True) Dim ImageProcess1 As New WIA.ImageProcess ImageProcess1.Filters.Add(ImageProcess1.FilterInfos("Convert").FilterID) ' sceglie il formato in cui salvare la scansione Dim Estensione As String = "" ImageProcess1.Filters(1).Properties("Quality").Value = Qualita Select Case Formato_File Case 0 ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatJPEG Estensione = ".jpg" Case 1 ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatBMP Estensione = ".bmp" Case 2 ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatGIF Estensione = ".gif" Case 3 ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatPNG Estensione = ".png" Case 4 : ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatTIFF Estensione = ".tif" Case Else ImageProcess1.Filters(1).Properties("FormatID").Value = WIA.FormatID.wiaFormatJPEG Estensione = ".jpg" End Select ImageFile = ImageProcess1.Apply(ImageFile) If Not File.Exists(NOME_FILE & Estensione) Then ImageFile.SaveFile(NOME_FILE & Estensione) IngAC_SCAN_TO_FILE = (NOME_FILE & Estensione) Else Kill(NOME_FILE & Estensione) ImageFile.SaveFile(NOME_FILE & Estensione) IngAC_SCAN_TO_FILE = (NOME_FILE & Estensione) End If Catch ex As Exception MessageBox.Show("Function IngAC_SCAN_TO_FILE: ", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Function
[/codesyntax]
Funzionamento di IngAC_SCAN_TO_FILE ()
Il funzionamento è abbastanza lineare; occorre innanzi tutto includere nei riferimenti la Windows Image Acquisition (WIA) che permette di accedere allo scanner ed è presente in quasi tutti i sistemi Windows. Ottenere file dallo scanner salvabili in vari formati sarà immediato e semplicissimo.
Dal menu “Progetto” cliccare su “Aggiungi riferimento…”, quindi
selezionare COM -> librerie dei tipi e cercare Microsoft Windows Image Acquisition Library e spuntare.
A questo punto basta copiare il listato che è commentato opportunamente. In caso di errori apparirà un MsgBox con una piccola spiegazione.
Utilizza questa funzione cosi com’è o modificala per ottenere velocemente file dallo scanner.
Nota: ricordarti di Imports System.IO in testa al listato, per poter usare la funzione.