├── .gitignore
├── README.md
├── ScannerDemo.sln
├── ScannerDemo
├── App.config
├── Form1.Designer.cs
├── Form1.cs
├── Form1.resx
├── Program.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
├── Scanner.cs
└── ScannerDemo.csproj
└── Screenshots
├── scanning-preview.png
└── scanning-progress.png
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 | *.VC.VC.opendb
85 |
86 | # Visual Studio profiler
87 | *.psess
88 | *.vsp
89 | *.vspx
90 | *.sap
91 |
92 | # TFS 2012 Local Workspace
93 | $tf/
94 |
95 | # Guidance Automation Toolkit
96 | *.gpState
97 |
98 | # ReSharper is a .NET coding add-in
99 | _ReSharper*/
100 | *.[Rr]e[Ss]harper
101 | *.DotSettings.user
102 |
103 | # JustCode is a .NET coding add-in
104 | .JustCode
105 |
106 | # TeamCity is a build add-in
107 | _TeamCity*
108 |
109 | # DotCover is a Code Coverage Tool
110 | *.dotCover
111 |
112 | # NCrunch
113 | _NCrunch_*
114 | .*crunch*.local.xml
115 | nCrunchTemp_*
116 |
117 | # MightyMoose
118 | *.mm.*
119 | AutoTest.Net/
120 |
121 | # Web workbench (sass)
122 | .sass-cache/
123 |
124 | # Installshield output folder
125 | [Ee]xpress/
126 |
127 | # DocProject is a documentation generator add-in
128 | DocProject/buildhelp/
129 | DocProject/Help/*.HxT
130 | DocProject/Help/*.HxC
131 | DocProject/Help/*.hhc
132 | DocProject/Help/*.hhk
133 | DocProject/Help/*.hhp
134 | DocProject/Help/Html2
135 | DocProject/Help/html
136 |
137 | # Click-Once directory
138 | publish/
139 |
140 | # Publish Web Output
141 | *.[Pp]ublish.xml
142 | *.azurePubxml
143 | # TODO: Comment the next line if you want to checkin your web deploy settings
144 | # but database connection strings (with potential passwords) will be unencrypted
145 | *.pubxml
146 | *.publishproj
147 |
148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
149 | # checkin your Azure Web App publish settings, but sensitive information contained
150 | # in these scripts will be unencrypted
151 | PublishScripts/
152 |
153 | # NuGet Packages
154 | *.nupkg
155 | # The packages folder can be ignored because of Package Restore
156 | **/packages/*
157 | # except build/, which is used as an MSBuild target.
158 | !**/packages/build/
159 | # Uncomment if necessary however generally it will be regenerated when needed
160 | #!**/packages/repositories.config
161 | # NuGet v3's project.json files produces more ignoreable files
162 | *.nuget.props
163 | *.nuget.targets
164 |
165 | # Microsoft Azure Build Output
166 | csx/
167 | *.build.csdef
168 |
169 | # Microsoft Azure Emulator
170 | ecf/
171 | rcf/
172 |
173 | # Windows Store app package directories and files
174 | AppPackages/
175 | BundleArtifacts/
176 | Package.StoreAssociation.xml
177 | _pkginfo.txt
178 |
179 | # Visual Studio cache files
180 | # files ending in .cache can be ignored
181 | *.[Cc]ache
182 | # but keep track of directories ending in .cache
183 | !*.[Cc]ache/
184 |
185 | # Others
186 | ClientBin/
187 | ~$*
188 | *~
189 | *.dbmdl
190 | *.dbproj.schemaview
191 | *.pfx
192 | *.publishsettings
193 | node_modules/
194 | orleans.codegen.cs
195 |
196 | # Since there are multiple workflows, uncomment next line to ignore bower_components
197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
198 | #bower_components/
199 |
200 | # RIA/Silverlight projects
201 | Generated_Code/
202 |
203 | # Backup & report files from converting an old project file
204 | # to a newer Visual Studio version. Backup files are not needed,
205 | # because we have git ;-)
206 | _UpgradeReport_Files/
207 | Backup*/
208 | UpgradeLog*.XML
209 | UpgradeLog*.htm
210 |
211 | # SQL Server files
212 | *.mdf
213 | *.ldf
214 |
215 | # Business Intelligence projects
216 | *.rdl.data
217 | *.bim.layout
218 | *.bim_*.settings
219 |
220 | # Microsoft Fakes
221 | FakesAssemblies/
222 |
223 | # GhostDoc plugin setting file
224 | *.GhostDoc.xml
225 |
226 | # Node.js Tools for Visual Studio
227 | .ntvs_analysis.dat
228 |
229 | # Visual Studio 6 build log
230 | *.plg
231 |
232 | # Visual Studio 6 workspace options file
233 | *.opt
234 |
235 | # Visual Studio LightSwitch build output
236 | **/*.HTMLClient/GeneratedArtifacts
237 | **/*.DesktopClient/GeneratedArtifacts
238 | **/*.DesktopClient/ModelManifest.xml
239 | **/*.Server/GeneratedArtifacts
240 | **/*.Server/ModelManifest.xml
241 | _Pvt_Extensions
242 |
243 | # Paket dependency manager
244 | .paket/paket.exe
245 | paket-files/
246 |
247 | # FAKE - F# Make
248 | .fake/
249 |
250 | # JetBrains Rider
251 | .idea/
252 | *.sln.iml
253 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # csharp-scanner-wia
2 | This repository contains a really simple scanner application that implements WIA (Windows Image Acquisition).
3 |
4 | # How to use
5 |
6 | Simply clone the repository:
7 |
8 | ```batch
9 | git clone https://github.com/ourcodeworld/csharp-scanner-wia.git
10 | ```
11 |
12 | And then open the project with Visual Studio and start the application. Enjoy it !
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | [Read the official article in Our Code World here](http://ourcodeworld.com/articles/read/382/creating-a-scanning-application-in-winforms-with-csharp).
--------------------------------------------------------------------------------
/ScannerDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScannerDemo", "ScannerDemo\ScannerDemo.csproj", "{346DBA90-52EB-4E4F-A899-07778C14F8F2}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {346DBA90-52EB-4E4F-A899-07778C14F8F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {346DBA90-52EB-4E4F-A899-07778C14F8F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {346DBA90-52EB-4E4F-A899-07778C14F8F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {346DBA90-52EB-4E4F-A899-07778C14F8F2}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/ScannerDemo/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/ScannerDemo/Form1.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace ScannerDemo
2 | {
3 | partial class Form1
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Windows Form Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | this.groupBox1 = new System.Windows.Forms.GroupBox();
32 | this.label3 = new System.Windows.Forms.Label();
33 | this.comboBox1 = new System.Windows.Forms.ComboBox();
34 | this.label2 = new System.Windows.Forms.Label();
35 | this.textBox2 = new System.Windows.Forms.TextBox();
36 | this.label1 = new System.Windows.Forms.Label();
37 | this.textBox1 = new System.Windows.Forms.TextBox();
38 | this.button2 = new System.Windows.Forms.Button();
39 | this.listBox1 = new System.Windows.Forms.ListBox();
40 | this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
41 | this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
42 | this.button1 = new System.Windows.Forms.Button();
43 | this.pictureBox1 = new System.Windows.Forms.PictureBox();
44 | this.label4 = new System.Windows.Forms.Label();
45 | this.groupBox1.SuspendLayout();
46 | this.tableLayoutPanel1.SuspendLayout();
47 | this.tableLayoutPanel2.SuspendLayout();
48 | ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
49 | this.SuspendLayout();
50 | //
51 | // groupBox1
52 | //
53 | this.groupBox1.Controls.Add(this.label4);
54 | this.groupBox1.Controls.Add(this.label3);
55 | this.groupBox1.Controls.Add(this.comboBox1);
56 | this.groupBox1.Controls.Add(this.label2);
57 | this.groupBox1.Controls.Add(this.textBox2);
58 | this.groupBox1.Controls.Add(this.label1);
59 | this.groupBox1.Controls.Add(this.textBox1);
60 | this.groupBox1.Controls.Add(this.button2);
61 | this.groupBox1.Controls.Add(this.listBox1);
62 | this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
63 | this.groupBox1.Location = new System.Drawing.Point(3, 3);
64 | this.groupBox1.Name = "groupBox1";
65 | this.groupBox1.Size = new System.Drawing.Size(209, 667);
66 | this.groupBox1.TabIndex = 1;
67 | this.groupBox1.TabStop = false;
68 | this.groupBox1.Text = "Properties";
69 | //
70 | // label3
71 | //
72 | this.label3.AutoSize = true;
73 | this.label3.Location = new System.Drawing.Point(9, 291);
74 | this.label3.Name = "label3";
75 | this.label3.Size = new System.Drawing.Size(68, 13);
76 | this.label3.TabIndex = 7;
77 | this.label3.Text = "Image format";
78 | //
79 | // comboBox1
80 | //
81 | this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
82 | this.comboBox1.FormattingEnabled = true;
83 | this.comboBox1.Items.AddRange(new object[] {
84 | "PNG",
85 | "JPEG",
86 | "BMP",
87 | "GIF",
88 | "TIFF"});
89 | this.comboBox1.Location = new System.Drawing.Point(9, 307);
90 | this.comboBox1.Name = "comboBox1";
91 | this.comboBox1.Size = new System.Drawing.Size(194, 21);
92 | this.comboBox1.TabIndex = 6;
93 | //
94 | // label2
95 | //
96 | this.label2.AutoSize = true;
97 | this.label2.Location = new System.Drawing.Point(9, 238);
98 | this.label2.Name = "label2";
99 | this.label2.Size = new System.Drawing.Size(49, 13);
100 | this.label2.TabIndex = 5;
101 | this.label2.Text = "Filename";
102 | //
103 | // textBox2
104 | //
105 | this.textBox2.Location = new System.Drawing.Point(9, 254);
106 | this.textBox2.Name = "textBox2";
107 | this.textBox2.Size = new System.Drawing.Size(194, 20);
108 | this.textBox2.TabIndex = 4;
109 | this.textBox2.Text = "myscan";
110 | //
111 | // label1
112 | //
113 | this.label1.AutoSize = true;
114 | this.label1.Location = new System.Drawing.Point(9, 341);
115 | this.label1.Name = "label1";
116 | this.label1.Size = new System.Drawing.Size(119, 13);
117 | this.label1.TabIndex = 3;
118 | this.label1.Text = "Output scanned images";
119 | //
120 | // textBox1
121 | //
122 | this.textBox1.Enabled = false;
123 | this.textBox1.Location = new System.Drawing.Point(9, 357);
124 | this.textBox1.Name = "textBox1";
125 | this.textBox1.Size = new System.Drawing.Size(194, 20);
126 | this.textBox1.TabIndex = 2;
127 | //
128 | // button2
129 | //
130 | this.button2.Location = new System.Drawing.Point(9, 383);
131 | this.button2.Name = "button2";
132 | this.button2.Size = new System.Drawing.Size(194, 38);
133 | this.button2.TabIndex = 1;
134 | this.button2.Text = "Change output folder";
135 | this.button2.UseVisualStyleBackColor = true;
136 | this.button2.Click += new System.EventHandler(this.button2_Click);
137 | //
138 | // listBox1
139 | //
140 | this.listBox1.FormattingEnabled = true;
141 | this.listBox1.Location = new System.Drawing.Point(9, 46);
142 | this.listBox1.Name = "listBox1";
143 | this.listBox1.Size = new System.Drawing.Size(194, 173);
144 | this.listBox1.TabIndex = 0;
145 | //
146 | // tableLayoutPanel1
147 | //
148 | this.tableLayoutPanel1.ColumnCount = 2;
149 | this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
150 | this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 521F));
151 | this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 0);
152 | this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 1, 0);
153 | this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
154 | this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
155 | this.tableLayoutPanel1.Name = "tableLayoutPanel1";
156 | this.tableLayoutPanel1.RowCount = 1;
157 | this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
158 | this.tableLayoutPanel1.Size = new System.Drawing.Size(736, 673);
159 | this.tableLayoutPanel1.TabIndex = 3;
160 | //
161 | // tableLayoutPanel2
162 | //
163 | this.tableLayoutPanel2.ColumnCount = 1;
164 | this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
165 | this.tableLayoutPanel2.Controls.Add(this.button1, 0, 0);
166 | this.tableLayoutPanel2.Controls.Add(this.pictureBox1, 0, 1);
167 | this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
168 | this.tableLayoutPanel2.Location = new System.Drawing.Point(218, 3);
169 | this.tableLayoutPanel2.Name = "tableLayoutPanel2";
170 | this.tableLayoutPanel2.RowCount = 2;
171 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
172 | this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 606F));
173 | this.tableLayoutPanel2.Size = new System.Drawing.Size(515, 667);
174 | this.tableLayoutPanel2.TabIndex = 2;
175 | //
176 | // button1
177 | //
178 | this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
179 | this.button1.Location = new System.Drawing.Point(3, 3);
180 | this.button1.Name = "button1";
181 | this.button1.Size = new System.Drawing.Size(509, 55);
182 | this.button1.TabIndex = 0;
183 | this.button1.Text = "Start scan";
184 | this.button1.UseVisualStyleBackColor = true;
185 | this.button1.Click += new System.EventHandler(this.button1_Click);
186 | //
187 | // pictureBox1
188 | //
189 | this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
190 | this.pictureBox1.Location = new System.Drawing.Point(3, 64);
191 | this.pictureBox1.Name = "pictureBox1";
192 | this.pictureBox1.Size = new System.Drawing.Size(509, 600);
193 | this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
194 | this.pictureBox1.TabIndex = 1;
195 | this.pictureBox1.TabStop = false;
196 | //
197 | // label4
198 | //
199 | this.label4.AutoSize = true;
200 | this.label4.Location = new System.Drawing.Point(6, 24);
201 | this.label4.Name = "label4";
202 | this.label4.Size = new System.Drawing.Size(87, 13);
203 | this.label4.TabIndex = 8;
204 | this.label4.Text = "Select a scanner";
205 | //
206 | // Form1
207 | //
208 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
209 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
210 | this.ClientSize = new System.Drawing.Size(736, 673);
211 | this.Controls.Add(this.tableLayoutPanel1);
212 | this.Name = "Form1";
213 | this.Text = "WIA Scanner Example";
214 | this.Load += new System.EventHandler(this.Form1_Load);
215 | this.groupBox1.ResumeLayout(false);
216 | this.groupBox1.PerformLayout();
217 | this.tableLayoutPanel1.ResumeLayout(false);
218 | this.tableLayoutPanel2.ResumeLayout(false);
219 | ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
220 | this.ResumeLayout(false);
221 |
222 | }
223 |
224 | #endregion
225 | private System.Windows.Forms.GroupBox groupBox1;
226 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
227 | private System.Windows.Forms.ListBox listBox1;
228 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
229 | private System.Windows.Forms.Button button1;
230 | private System.Windows.Forms.PictureBox pictureBox1;
231 | private System.Windows.Forms.TextBox textBox1;
232 | private System.Windows.Forms.Button button2;
233 | private System.Windows.Forms.Label label1;
234 | private System.Windows.Forms.Label label2;
235 | private System.Windows.Forms.TextBox textBox2;
236 | private System.Windows.Forms.Label label3;
237 | private System.Windows.Forms.ComboBox comboBox1;
238 | private System.Windows.Forms.Label label4;
239 | }
240 | }
241 |
242 |
--------------------------------------------------------------------------------
/ScannerDemo/Form1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.Data;
5 | using System.Drawing;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 | using System.Windows.Forms;
11 | using WIA;
12 |
13 | namespace ScannerDemo
14 | {
15 | public partial class Form1 : Form
16 | {
17 | public Form1()
18 | {
19 | InitializeComponent();
20 | }
21 |
22 | private void Form1_Load(object sender, EventArgs e)
23 | {
24 | ListScanners();
25 |
26 | // Set start output folder TMP
27 | textBox1.Text = Path.GetTempPath();
28 | // Set JPEG as default
29 | comboBox1.SelectedIndex = 1;
30 |
31 | }
32 |
33 | private void ListScanners()
34 | {
35 | // Clear the ListBox.
36 | listBox1.Items.Clear();
37 |
38 | // Create a DeviceManager instance
39 | var deviceManager = new DeviceManager();
40 |
41 | // Loop through the list of devices and add the name to the listbox
42 | for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
43 | {
44 | // Add the device only if it's a scanner
45 | if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
46 | {
47 | continue;
48 | }
49 |
50 | // Add the Scanner device to the listbox (the entire DeviceInfos object)
51 | // Important: we store an object of type scanner (which ToString method returns the name of the scanner)
52 | listBox1.Items.Add(
53 | new Scanner(deviceManager.DeviceInfos[i])
54 | );
55 | }
56 | }
57 |
58 | private void button1_Click(object sender, EventArgs e)
59 | {
60 | Task.Factory.StartNew(StartScanning).ContinueWith(result => TriggerScan());
61 | }
62 |
63 | private void TriggerScan()
64 | {
65 | Console.WriteLine("Image succesfully scanned");
66 | }
67 |
68 | public void StartScanning()
69 | {
70 | Scanner device = null;
71 |
72 | this.Invoke(new MethodInvoker(delegate ()
73 | {
74 | device = listBox1.SelectedItem as Scanner;
75 | }));
76 |
77 | if (device == null)
78 | {
79 | MessageBox.Show("You need to select first an scanner device from the list",
80 | "Warning",
81 | MessageBoxButtons.OK, MessageBoxIcon.Warning);
82 | return;
83 | }else if(String.IsNullOrEmpty(textBox2.Text))
84 | {
85 | MessageBox.Show("Provide a filename",
86 | "Warning",
87 | MessageBoxButtons.OK, MessageBoxIcon.Warning);
88 | return;
89 | }
90 |
91 | ImageFile image = new ImageFile();
92 | string imageExtension = "";
93 |
94 | this.Invoke(new MethodInvoker(delegate ()
95 | {
96 | switch (comboBox1.SelectedIndex)
97 | {
98 | case 0:
99 | image = device.ScanImage(WIA.FormatID.wiaFormatPNG);
100 | imageExtension = ".png";
101 | break;
102 | case 1:
103 | image = device.ScanImage(WIA.FormatID.wiaFormatJPEG);
104 | imageExtension = ".jpeg";
105 | break;
106 | case 2:
107 | image = device.ScanImage(WIA.FormatID.wiaFormatBMP);
108 | imageExtension = ".bmp";
109 | break;
110 | case 3:
111 | image = device.ScanImage(WIA.FormatID.wiaFormatGIF);
112 | imageExtension = ".gif";
113 | break;
114 | case 4:
115 | image = device.ScanImage(WIA.FormatID.wiaFormatTIFF);
116 | imageExtension = ".tiff";
117 | break;
118 | }
119 | }));
120 |
121 |
122 | // Save the image
123 | var path = Path.Combine(textBox1.Text, textBox2.Text + imageExtension);
124 |
125 | if (File.Exists(path))
126 | {
127 | File.Delete(path);
128 | }
129 |
130 | image.SaveFile(path);
131 |
132 | pictureBox1.Image = new Bitmap(path);
133 | }
134 |
135 | private void button2_Click(object sender, EventArgs e)
136 | {
137 | FolderBrowserDialog folderDlg = new FolderBrowserDialog();
138 | folderDlg.ShowNewFolderButton = true;
139 | DialogResult result = folderDlg.ShowDialog();
140 |
141 | if (result == DialogResult.OK)
142 | {
143 | textBox1.Text = folderDlg.SelectedPath;
144 | }
145 | }
146 |
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/ScannerDemo/Form1.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
--------------------------------------------------------------------------------
/ScannerDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using System.Windows.Forms;
6 |
7 | namespace ScannerDemo
8 | {
9 | static class Program
10 | {
11 | ///
12 | /// The main entry point for the application.
13 | ///
14 | [STAThread]
15 | static void Main()
16 | {
17 | Application.EnableVisualStyles();
18 | Application.SetCompatibleTextRenderingDefault(false);
19 | Application.Run(new Form1());
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/ScannerDemo/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("ScannerDemo")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("ScannerDemo")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("346dba90-52eb-4e4f-a899-07778c14f8f2")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/ScannerDemo/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace ScannerDemo.Properties
12 | {
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// Returns the cached ResourceManager instance used by this class.
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ScannerDemo.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// Overrides the current thread's CurrentUICulture property for all
56 | /// resource lookups using this strongly typed resource class.
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/ScannerDemo/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/ScannerDemo/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace ScannerDemo.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/ScannerDemo/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ScannerDemo/Scanner.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Runtime.InteropServices;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using WIA;
8 | using System.Windows.Forms;
9 |
10 | namespace ScannerDemo
11 | {
12 | class Scanner
13 | {
14 | const string WIA_SCAN_COLOR_MODE = "6146";
15 | const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
16 | const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
17 | const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
18 | const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
19 | const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
20 | const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
21 | const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
22 | const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
23 |
24 | private readonly DeviceInfo _deviceInfo;
25 | private int resolution = 300;
26 | private int width_pixel = 1250;
27 | private int height_pixel = 1700;
28 | private int color_mode = 1;
29 |
30 | public Scanner(DeviceInfo deviceInfo)
31 | {
32 | this._deviceInfo = deviceInfo;
33 | }
34 |
35 | ///
36 | /// Scan an image with the specified format
37 | ///
38 | /// Expects a WIA.FormatID constant
39 | ///
40 | public ImageFile ScanImage(string imageFormat)
41 | {
42 | // Connect to the device and instruct it to scan
43 | // Connect to the device
44 | var device = this._deviceInfo.Connect();
45 |
46 | // Select the scanner
47 | CommonDialogClass dlg = new CommonDialogClass();
48 |
49 | var item = device.Items[1];
50 |
51 | try
52 | {
53 | AdjustScannerSettings(item, resolution, 0, 0, width_pixel, height_pixel, 0, 0, color_mode);
54 |
55 | object scanResult = dlg.ShowTransfer(item, imageFormat, true);
56 |
57 | if(scanResult != null)
58 | {
59 | var imageFile = (ImageFile)scanResult;
60 |
61 | // Return the imageFile
62 | return imageFile;
63 | }
64 | }
65 | catch (COMException e)
66 | {
67 | // Display the exception in the console.
68 | Console.WriteLine(e.ToString());
69 |
70 | uint errorCode = (uint)e.ErrorCode;
71 |
72 | // Catch 2 of the most common exceptions
73 | if (errorCode == 0x80210006)
74 | {
75 | MessageBox.Show("The scanner is busy or isn't ready");
76 | }
77 | else if(errorCode == 0x80210064)
78 | {
79 | MessageBox.Show("The scanning process has been cancelled.");
80 | }
81 | else
82 | {
83 | MessageBox.Show("A non catched error occurred, check the console","Error",MessageBoxButtons.OK);
84 | }
85 | }
86 |
87 | return new ImageFile();
88 | }
89 |
90 | ///
91 | /// Adjusts the settings of the scanner with the providen parameters.
92 | ///
93 | /// Expects a
94 | /// Provide the DPI resolution that should be used e.g 150
95 | ///
96 | ///
97 | ///
98 | ///
99 | ///
100 | /// Modify the contrast percent
101 | /// Set the color mode
102 | private void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel, int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int colorMode)
103 | {
104 | SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
105 | SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
106 | SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
107 | SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
108 | SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels);
109 | SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels);
110 | SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
111 | SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);
112 | SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode);
113 | }
114 |
115 | ///
116 | ///
117 | ///
118 | ///
119 | ///
120 | ///
121 | private void SetWIAProperty(IProperties properties, object propName, object propValue)
122 | {
123 | Property prop = properties.get_Item(ref propName);
124 |
125 | try
126 | {
127 | prop.set_Value(ref propValue);
128 | }
129 | catch
130 | {
131 | // DPI can only be set to values listed in SubTypeValues
132 | // This sets the DPI to the lowest one supported by the scanner
133 | if (propName.ToString() == WIA_HORIZONTAL_SCAN_RESOLUTION_DPI || propName.ToString() == WIA_VERTICAL_SCAN_RESOLUTION_DPI)
134 | {
135 | foreach (object test in prop.SubTypeValues)
136 | {
137 | prop.set_Value(test);
138 | break;
139 | }
140 | }
141 | }
142 | }
143 |
144 | ///
145 | /// Declare the ToString method
146 | ///
147 | ///
148 | public override string ToString()
149 | {
150 | return (string) this._deviceInfo.Properties["Name"].get_Value();
151 | }
152 |
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/ScannerDemo/ScannerDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {346DBA90-52EB-4E4F-A899-07778C14F8F2}
8 | WinExe
9 | Properties
10 | ScannerDemo
11 | ScannerDemo
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Form
51 |
52 |
53 | Form1.cs
54 |
55 |
56 |
57 |
58 |
59 | Form1.cs
60 |
61 |
62 | ResXFileCodeGenerator
63 | Resources.Designer.cs
64 | Designer
65 |
66 |
67 | True
68 | Resources.resx
69 |
70 |
71 | SettingsSingleFileGenerator
72 | Settings.Designer.cs
73 |
74 |
75 | True
76 | Settings.settings
77 | True
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | {94A0E92D-43C0-494E-AC29-FD45948A5221}
86 | 1
87 | 0
88 | 0
89 | tlbimp
90 | False
91 | False
92 |
93 |
94 |
95 |
102 |
--------------------------------------------------------------------------------
/Screenshots/scanning-preview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ourcodeworld/csharp-scanner-wia/277e283a637a331e0807953c7f6d88ecb2aab3ed/Screenshots/scanning-preview.png
--------------------------------------------------------------------------------
/Screenshots/scanning-progress.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ourcodeworld/csharp-scanner-wia/277e283a637a331e0807953c7f6d88ecb2aab3ed/Screenshots/scanning-progress.png
--------------------------------------------------------------------------------