-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinter.cs
146 lines (127 loc) · 4.89 KB
/
Printer.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
using Emgu.CV;
using System.Drawing.Printing;
namespace CyberedgeImageProcess2024
{
public class Printer
{
private EdgeImagePlus imp;
private static int scaling = 100;
private static bool drawBorder;
private static bool center = true;
private static bool label;
private static bool printSelection;
private static bool rotate;
private static bool actualSize;
private static int fontSize = 12;
private PageSettings pageSettings;
public Printer(EdgeImagePlus imp)
{
this.imp = imp;
}
public void PageSetup()
{
Roi roi = imp != null ? imp.Roi : null;
bool isRoi = roi != null && roi.IsArea();
PrinterSetup printerSetup = new PrinterSetup(imp);
printerSetup.Scaling = scaling;
printerSetup.Center = center;
printerSetup.DrawBorder = drawBorder;
printerSetup.PrintTitle = label;
printerSetup.Rotate = rotate;
printerSetup.ActualSize = actualSize;
if (printerSetup.ShowDialog() == DialogResult.Cancel) return;
scaling = printerSetup.Scaling;
if (scaling < 5.0) scaling = 5;
drawBorder = printerSetup.DrawBorder;
center = printerSetup.Center;
label = printerSetup.PrintTitle;
if (isRoi)
printSelection = printerSetup.PrintSelection;
else
printSelection = false;
rotate = printerSetup.Rotate;
actualSize = printerSetup.ActualSize;
}
public void Print()
{
PrintDocument printDocument = new PrintDocument();
printDocument.DocumentName = "Cyberedge Print";
printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDialog.Document.Print();
}
}
public void SetPageFormat(PageSettings pageSettings)
{
this.pageSettings = pageSettings;
}
private void PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
Roi roi = imp.Roi;
EdgeImagePlus imp2 = imp;
ImageProcessor ip = imp2.GetProcessor();
if (printSelection && roi != null && roi.IsArea())
ip.SetRoi(roi);
ip = ip.Crop();
int width = ip.Width;
int height = ip.Height;
int margin = 0;
if (drawBorder) margin = 1;
double scale = scaling / 100.0;
int dstWidth = (int)(width * scale);
int dstHeight = (int)(height * scale);
int pageX = (int)pageSettings.PrintableArea.X;
int pageY = (int)pageSettings.PrintableArea.Y;
int dstX = pageX + margin;
int dstY = pageY + margin;
Image img = ip.CreateImage().ToBitmap();
double pageWidth = pageSettings.PrintableArea.Width - 2 * margin;
double pageHeight = pageSettings.PrintableArea.Height - 2 * margin;
if (label && pageWidth - dstWidth < fontSize + 5)
{
dstY += fontSize + 5;
pageHeight -= fontSize + 5;
}
if (actualSize)
{
if (center && dstWidth < pageWidth && dstHeight < pageHeight)
{
dstX += (int)(pageWidth - dstWidth) / 2;
dstY += (int)(pageHeight - dstHeight) / 2;
}
}
else if (dstWidth > pageWidth || dstHeight > pageHeight)
{
// scale to fit page
double hscale = pageWidth / dstWidth;
double vscale = pageHeight / dstHeight;
double scale2 = hscale <= vscale ? hscale : vscale;
dstWidth = (int)(dstWidth * scale2);
dstHeight = (int)(dstHeight * scale2);
}
else if (center)
{
dstX += (int)(pageWidth - dstWidth) / 2;
dstY += (int)(pageHeight - dstHeight) / 2;
}
g.DrawImage(img, new Rectangle(
dstX, dstY, dstWidth, dstHeight), new Rectangle(
0, 0, width, height), GraphicsUnit.Pixel);
if (drawBorder)
g.DrawRectangle(new Pen(Color.Black), dstX - 1, dstY - 1, dstWidth + 1, dstHeight + 1);
if (label)
{
g.DrawString(imp.Title,
new Font(new FontFamily("宋体"), fontSize, FontStyle.Regular),
System.Drawing.Brushes.Black,
pageX + 5,
pageY + fontSize);
}
}
}
}