-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormMain.cs
More file actions
116 lines (101 loc) · 3.79 KB
/
Copy pathFormMain.cs
File metadata and controls
116 lines (101 loc) · 3.79 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace imageprinter
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
comboBoxPrinters.Items.AddRange(PrinterSettings.InstalledPrinters.Cast<string>().ToArray());
}
private int _currentFile;
private string[] _fileList;
private Rectangle _pageSize;
private PrintDocument _pd;
private void buttonPrint_Click(object sender, EventArgs e)
{
try
{
try
{
_fileList = listBoxImages.Items.Cast<string>().ToArray();
_pd = new PrintDocument();
_pd.PrinterSettings.PrinterName = comboBoxPrinters.SelectedItem.ToString();
_pd.DefaultPageSettings.PaperSize = new PaperSize("custom", (int)(numericUpDownWidth.Value*100), (int)(numericUpDownHeight.Value*100));
_pageSize = _pd.DefaultPageSettings.Bounds;
_pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
_pd.Print();
}
finally
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
Image image = Image.FromFile(_fileList[_currentFile]);
ev.Graphics.DrawImage(image, _pageSize);
if (++_currentFile >= _fileList.Length)
ev.HasMorePages = false;
else
ev.HasMorePages = true;
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Get files";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.Multiselect = true;
fdlg.CheckFileExists = true;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
listBoxImages.Items.AddRange(fdlg.FileNames);
}
}
private void fillImagesFromDirectory(string path)
{
string[] files = Directory.GetFiles(path);
listBoxImages.Items.AddRange(files);
}
private void buttonCustomPageSize_Click(object sender, EventArgs e)
{
}
private void comboBoxPrinters_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
// Save the selected employee's name, because we will remove
// the employee's name from the list.
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = (string)(comboBoxPrinters.SelectedItem);
comboBoxPageSize.Items.AddRange(ps.PaperSizes.Cast<PaperSize>().ToArray());
}
private void comboBoxPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
var paperSize = (PaperSize)comboBoxPageSize.SelectedItem;
numericUpDownWidth.Value = paperSize.Width/(Decimal)100.0;
numericUpDownHeight.Value = paperSize.Height / (Decimal)100.0;
}
}
}