-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
270 lines (225 loc) · 10.5 KB
/
MainWindow.xaml.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using WIA;
using System.IO;
using System.Windows.Media;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.Windows.Media.Imaging;
using System.Diagnostics;
namespace PivotScan2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string WiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
private Scanner scanner;
private PaperSize pageSizeInches;
private ScannedImageSize imageSizePixels;
private Point imageOffsetInches = new Point();
private int imageRotationSteps = 0;
// Local state for dragging the scanned image, only valid while dragging
private bool isDragging = false;
private Point dragStartPosition = new Point();
private Point imageOffsetStart = new Point();
public MainWindow()
{
InitializeComponent();
PickScanner.SelectedIndex = 0;
PickScanner.ItemsSource = this.Scanners;
PickPaperSize.ItemsSource = PaperSize.Defaults;
PickPaperSize.SelectedItem = PaperSize.Defaults.FirstOrDefault(p => p.Name == Settings.Instance.PaperSize) ?? PaperSize.Defaults[0];
}
internal IEnumerable<Scanner> Scanners
{
get
{
var dm = new DeviceManager();
int i = 0;
foreach (DeviceInfo deviceInfo in dm.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
var scanner = new Scanner { Device = deviceInfo };
if (scanner.Name == Settings.Instance.Scanner)
{
PickScanner.SelectedIndex = i;
this.scanner = scanner;
}
yield return scanner;
++i;
}
}
}
}
private void PickScanner_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedScanner = e.AddedItems[0] as Scanner;
this.scanner = selectedScanner;
Settings.Instance.Scanner = selectedScanner.Name;
Settings.Instance.Save();
}
private void Scan_Click(object sender, RoutedEventArgs e)
{
if (this.scanner == null) return; // No scanner selected/available
Device device = this.scanner.Device.Connect();
ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
ImageFile image = (ImageFile)wiaCommonDialog.ShowTransfer(device.Items[1], WiaFormatPNG);
if (image == null) return; // Scan cancelled
var imagePath = Path.Combine(Settings.AppDataPath, "image." + image.FileExtension);
File.Delete(imagePath);
image.SaveFile(imagePath);
// Save the image dimentions for scaling computations later
this.imageSizePixels = new ScannedImageSize
{
Width = image.Width,
Height = image.Height,
HorizontalResolution = image.HorizontalResolution,
VerticalResolution = image.VerticalResolution,
};
// Center the newly-scanned image
this.imageOffsetInches = new Point();
// Draw the new image
this.ScannedImage.Source = new ImageSourceConverter().ConvertFromString(imagePath) as BitmapSource;
this.UpdateScannedImageBounds();
}
private void PickPaperSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var paperSize = e.AddedItems[0] as PaperSize;
this.pageSizeInches = paperSize;
this.UpdateCanvas();
Settings.Instance.PaperSize = paperSize.Name;
Settings.Instance.Save();
}
private void RotateLeft_Click(object sender, RoutedEventArgs e)
{
this.imageRotationSteps = (this.imageRotationSteps - 1 + 4) % 4;
this.UpdateScannedImageBounds();
}
private void Center_Click(object sender, RoutedEventArgs e)
{
this.imageOffsetInches = new Point();
this.UpdateScannedImageBounds();
}
private void RotateRight_Click(object sender, RoutedEventArgs e)
{
this.imageRotationSteps = (this.imageRotationSteps + 1) % 4;
this.UpdateScannedImageBounds();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
if (this.imageSizePixels == null) return;
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.DefaultExt = ".pdf";
dialog.Filter = "PDF documents (.pdf)|*.pdf";
if (dialog.ShowDialog() != true) return;
var pdf = new PdfDocument();
var page = pdf.AddPage();
page.Size = this.pageSizeInches.PdfSize;
page.Orientation = this.pageSizeInches.Orientation;
var gfx = XGraphics.FromPdfPage(page);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(this.ScannedImage.Source as BitmapSource));
using (var ms = new MemoryStream())
{
encoder.Save(ms);
var image = XImage.FromStream(ms);
XUnit width = XUnit.FromInch(this.imageSizePixels.Width / this.imageSizePixels.HorizontalResolution);
XUnit height= XUnit.FromInch(this.imageSizePixels.Height / this.imageSizePixels.VerticalResolution);
XUnit x = -width / 2 + page.Width / 2 + XUnit.FromInch(this.imageOffsetInches.X);
XUnit y = -height / 2 + page.Height / 2 + XUnit.FromInch(this.imageOffsetInches.Y);
gfx.TranslateTransform(x, y);
gfx.RotateAtTransform(this.imageRotationSteps * 90, new XPoint(width / 2, height / 2));
gfx.DrawImage(image, 0, 0, width, height);
}
try
{
pdf.Save(dialog.FileName);
if (this.OpenAfterSave.IsChecked == true)
{
Process.Start(dialog.FileName);
}
}
catch (IOException)
{
MessageBox.Show("Cannot save as the given file is open - please close it and try again.");
}
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
this.UpdateCanvas();
}
private void UpdateCanvas()
{
double canvasAspectRatio = this.Canvas.ActualWidth / this.Canvas.ActualHeight;
// Constrain the page rectangle to the available canvas area while maintaining aspect ratio
if (canvasAspectRatio <= this.pageSizeInches.AspectRatio)
{
this.PageBackground.Width = this.Canvas.ActualWidth;
this.PageBackground.Height = this.Canvas.ActualWidth / this.pageSizeInches.AspectRatio;
}
else
{
this.PageBackground.Width = this.Canvas.ActualHeight * this.pageSizeInches.AspectRatio;
this.PageBackground.Height = this.Canvas.ActualHeight;
}
// Ensure the dashed page border mirrors the page background rectangle
this.PageBorder.Width = this.PageBackground.Width;
this.PageBorder.Height = this.PageBackground.Height;
double topOffset = (this.Canvas.ActualHeight - this.PageBackground.Height) / 2;
double leftOffset = (this.Canvas.ActualWidth - this.PageBackground.Width) / 2;
// Center the page within the canvas area
Canvas.SetTop(this.PageBackground, topOffset);
Canvas.SetLeft(this.PageBackground, leftOffset);
Canvas.SetTop(this.PageBorder, topOffset);
Canvas.SetLeft(this.PageBorder, leftOffset);
this.UpdateScannedImageBounds();
}
private void UpdateScannedImageBounds()
{
// Only continue if we have a scanned image
if (this.imageSizePixels == null) return;
// Scale the scanned image to the page
this.ScannedImage.Width = this.imageSizePixels.Width / this.imageSizePixels.HorizontalResolution / this.pageSizeInches.Width * this.PageBackground.Width;
this.ScannedImage.Height = this.imageSizePixels.Height / this.imageSizePixels.VerticalResolution / this.pageSizeInches.Height * this.PageBackground.Height;
// Center it on the page, adjusting for the drag offset
Canvas.SetLeft(this.ScannedImage, -this.ScannedImage.Width / 2 + this.Canvas.ActualWidth / 2 + this.imageOffsetInches.X * PagePixelsPerInchX);
Canvas.SetTop(this.ScannedImage, -this.ScannedImage.Height / 2 + this.Canvas.ActualHeight / 2 + this.imageOffsetInches.Y * PagePixelsPerInchY);
// Finally, rotate it
this.ScannedImage.RenderTransform = new RotateTransform(this.imageRotationSteps * 90);
}
private double PagePixelsPerInchX
{
get { return this.PageBackground.Width / this.pageSizeInches.Width; }
}
private double PagePixelsPerInchY
{
get { return this.PageBackground.Height / this.pageSizeInches.Height; }
}
private void Page_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
this.isDragging = true;
this.dragStartPosition = e.GetPosition(this);
this.imageOffsetStart = this.imageOffsetInches;
}
private void Page_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
this.isDragging = false;
}
private void Page_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!this.isDragging) return;
// Total pixel offset of the mouse from the drag start
var dragOffsetPixels = e.GetPosition(this) - this.dragStartPosition;
// Convert the drag offset from pixels to page inches
var dragOffsetInches = new System.Windows.Vector(dragOffsetPixels.X / PagePixelsPerInchX, dragOffsetPixels.Y / PagePixelsPerInchY);
this.imageOffsetInches = this.imageOffsetStart + dragOffsetInches;
this.UpdateScannedImageBounds();
}
}
}