-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapPrintDocument.cs
224 lines (176 loc) · 7.93 KB
/
MapPrintDocument.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
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
namespace printPoster
{
class MapPrintDocument : PrintDocument
{
Bitmap image;
int curPage;
private Rectangle m_printArea;
public void Load(string fileName, string title = null)
{
image = (Bitmap)Bitmap.FromFile(fileName);
DocumentName = String.IsNullOrEmpty(title) ? fileName : title;
PrintArea = Rectangle.Empty;
Overlap = 0; //mm
}
public void SetImage(Bitmap im)
{
image = im;
}
public Image Image { get { return image; } }
public void SetDpi(float dpi)
{
image.SetResolution(dpi, dpi);
}
public float Overlap { get; set; } // mm todo: range check!
public Rectangle PrintArea
{
get { return m_printArea; }
set
{
var allImage = new Rectangle(new Point(0, 0), image.Size);
m_printArea = !value.IsEmpty ? Rectangle.Intersect(allImage, value) : allImage;
}
}
public void Start()
{
curPage = 0;
Print();
}
public bool PrintNextPage(Graphics grph, PageSettings sett)
{
var ps = sett.PrinterSettings;
int shift = ps.PrintRange == PrintRange.SomePages ? ps.FromPage - ps.MinimumPage : 0;
var sz = GetPageSize(sett);
var srcRect = GetSrcRect(curPage + shift, sz);
//grph.TranslateTransform(-sett.Margins.Left, -sett.Margins.Top);
grph.DrawImage(image, 0, 0, srcRect, GraphicsUnit.Pixel);
#if DEBUG
var s = String.Format("name: {0}, vdpi: {1}, range: {2}, page: {3}, total: {4}\n", DocumentName, image.VerticalResolution, ps.PrintRange, curPage + shift, GetNumPages(sett)) +
String.Format("compositing mode: {0}, CompQual: {1}, px offset mode: {2}, interpol mode: {3}", grph.CompositingMode, grph.CompositingQuality, grph.PixelOffsetMode, grph.InterpolationMode);
grph.PageUnit = GraphicsUnit.Pixel;
using (var font = new Font("Sans serif", 10))
using (var brush = new SolidBrush(Color.Red))
{
// grph.DrawRectangle(new Pen(Color.Red, 1), 0, 0, InchHdthToPx(sz.Width, grph.DpiX) - 1, InchHdthToPx(sz.Height, grph.DpiY) -1);
// grph.DrawRectangle(new Pen(Color.Green, 1), -1, -1, InchHdthToPx(sz.Width, grph.DpiX) + 1, InchHdthToPx(sz.Height, grph.DpiY) + 1);
// grph.DrawRectangle(new Pen(Color.White, 1), -2, -2, InchHdthToPx(sz.Width, grph.DpiX) + 3, InchHdthToPx(sz.Height, grph.DpiY) + 3);
// grph.DrawRectangle(new Pen(Color.Red, 1), 0, 0, sz.Width - 1, sz.Height);
// grph.DrawString(s, font, brush, 0, 0);
}
#endif
curPage++;
return HasMorePages(sett, sz);
}
public bool HasMorePages(PageSettings sett, Size pageSize)
{
switch (sett.PrinterSettings.PrintRange)
{
case PrintRange.AllPages:
return curPage < GetNumPages(pageSize);
case PrintRange.SomePages:
return curPage + sett.PrinterSettings.FromPage <= sett.PrinterSettings.ToPage;
}
throw new NotImplementedException("Methods other than All pages and Some pages are not implemented yet");
}
private static float pxToInchHdth(int px, float dpi)
{
return 100.0f * px / dpi;
}
private static int InchHdthToPx(int iHdth, float dpi)
{
return (int)(iHdth * dpi / 100);
}
public static Size PhysicalMargins(PageSettings sett)
{
return new Size((int)Math.Ceiling(sett.HardMarginX), (int) Math.Ceiling(sett.HardMarginY));
/* return new Size((int)Math.Ceiling(sett.Landscape ? sett.HardMarginY : sett.HardMarginX),
(int)Math.Ceiling(sett.Landscape ? sett.HardMarginX : sett.HardMarginY));
*/
}
// почему-то тормозит, лишний раз не звать, кешируя значения
public static Size GetPageSize(PageSettings sett) // hdth of inch
{
var bnds = sett.Bounds;
var m = sett.Margins;
var ps = PhysicalMargins(sett);
var mrgns = new Margins(Math.Max(m.Left, ps.Width), Math.Max(m.Right, ps.Width), Math.Max(m.Top, ps.Height), Math.Max(m.Bottom, ps.Height));
var pageWidth = bnds.Width - mrgns.Left - mrgns.Right;
var pageHeight = bnds.Height - mrgns.Top - mrgns.Bottom;
var pa = sett.PrintableArea;
if (sett.Landscape)
pa = new RectangleF(pa.Y, pa.X, pa.Height, pa.Width);
return new Size(Math.Min(pageWidth, (int)Math.Floor(pa.Width)), Math.Min(pageHeight, (int)Math.Floor(pa.Height)));
}
public int GetNumPages(PageSettings sett)
{
return sett != null ? GetNumPages(GetPageSize(sett)) : 0;
}
public int GetNumPages(Size pageSz)
{
return GetNumColumns(pageSz) * GetNumRows(pageSz);
}
public int GetNumColumns(Size pageSz)
{
if (pageSz.IsEmpty)
return 0;
float w = pxToInchHdth(PrintArea.Width, image.HorizontalResolution);
float ovrl = Overlap * 100 / 25.4f; // mm to inch / 100
return (int) Math.Ceiling((w - ovrl) / (pageSz.Width - ovrl));
}
public int GetNumRows(Size pageSz)
{
if (pageSz.IsEmpty)
return 0;
float h = pxToInchHdth(PrintArea.Height, image.VerticalResolution);
float ovrl = Overlap * 100 / 25.4f; // mm to inch / 100
return (int)Math.Ceiling((h - ovrl) / (pageSz.Height - ovrl));
}
public Rectangle GetSrcRect(int page, Size pageSz)
{
Size pxSize = new Size(InchHdthToPx(pageSz.Width, image.HorizontalResolution),
InchHdthToPx(pageSz.Height, image.VerticalResolution));
int horOvrl = (int) Math.Floor(Overlap * image.HorizontalResolution / 25.4);
int vertOvrl = (int)Math.Floor(Overlap * image.HorizontalResolution / 25.4);
// todo а если перекрытие больше ширины страницы?
int nCol = (PrintArea.Width - horOvrl + pxSize.Width - horOvrl - 1) / (pxSize.Width - horOvrl);
int row = page / nCol;
int col = page % nCol;
int x = col * (pxSize.Width - horOvrl);
int y = row * (pxSize.Height - vertOvrl);
return new Rectangle(x + PrintArea.X, y + PrintArea.Y,
x + pxSize.Width < PrintArea.Width ? pxSize.Width : PrintArea.Width - x,
y + pxSize.Height < PrintArea.Height ? pxSize.Height : PrintArea.Height - y);
}
}
public class TestMapPrintDocument
{
public StringBuilder sb = new StringBuilder();
public void Test()
{
using (var bm = new Bitmap(2000, 1000))
using (var pd = new MapPrintDocument())
{
bm.SetResolution(150, 150);
pd.SetImage(bm);
pd.PrintArea = new Rectangle(300, 0, 1700, 1000);
var pageSize = new Size(900, 1300);
int n = pd.GetNumPages(pageSize);
sb.AppendLine("pages: " + n.ToString());
for (int i = 0; i < n; i++)
{
var r = pd.GetSrcRect(i, pageSize);
sb.AppendLine("page " + i.ToString() + ": " + r.ToString());
}
}
}
public string Log { get { return sb.ToString(); } }
public void ClearLog()
{
sb = new StringBuilder();
}
}
}