-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathScan.cs
106 lines (93 loc) · 3.25 KB
/
Scan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace ServerScan
{
class ScanSettings
{
public int dpi = 150;
public int color = 2; //4 is black-white, gray is 2, color is 1
public bool adf = false;
public bool tryFlatbed = false; //try flatbed if adf fails
}
class Scan
{
public static void StartScan()
{
if (Program.config.SavePath == "")
{
Program.ShowError("Cannot start scanning, no save path defined");
return;
}
if (!PathWritable(Program.config.SavePath))
{
Program.ShowError("Provided path is not writable");
return;
}
if (Program.config.ScannerID == "")
{
Program.ShowError("Cannot start scanning, no scanner device selected.");
return;
}
//Load settings
ScanSettings settings = new ScanSettings();
settings.color = Program.config.ScanColor;
settings.dpi = Program.config.ScanDpi;
settings.adf = Program.config.ScanADF;
settings.tryFlatbed = Program.config.ScanTryFlatbed;
try
{
SaveImages(WIAScanner.Scan(Program.config.ScannerID, settings));
//Call garbage collector
GC.Collect();
}
catch (Exception ex)
{
Program.ShowError(ex);
}
}
private static Boolean PathWritable(String path)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(path);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
catch (Exception)
{
Program.ShowError("Cannot access designated save path.");
return false;
}
}
private static int SaveImages(List<Image> list)
{
//Check if path still ok
if (!PathWritable(Program.config.SavePath))
{
Program.ShowError("Provided path is not writable");
return 0;
}
Logger.Log("Finished scanning " + list.Count + " files");
DateTime now = DateTime.Now;
String filename = Program.config.SavePath + "\\" + now.ToString("yy_MM_dd-H_mm_ss");
int index = 1;
foreach (Image img in list)
{
img.Save(filename + "-" + index++ + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
}
if (list.Count > 0)
Logger.Log("Images saved in " + Program.config.SavePath);
return list.Count;
}
}
}