-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
289 lines (255 loc) · 13.9 KB
/
Copy pathForm1.cs
File metadata and controls
289 lines (255 loc) · 13.9 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
namespace LMaxPrint;
public partial class Form1 : Form
{
private FlowLayoutPanel toolBar = null!;
private ImageCanvas canvas = null!;
private StatusStrip statusStrip = null!;
private ToolStripStatusLabel lblStatus = null!;
private OpenFileDialog openFileDialog = null!;
private SaveFileDialog saveFileDialog = null!;
private PrintDocument printDocument = null!;
private PageSetupDialog pageSetupDialog = null!;
private PrintDialog printDialog = null!;
private Bitmap? _renderedPrintBitmap;
private ThemeSettings _theme = null!;
public Form1()
{
InitializeComponent();
_theme = ThemeSettings.Load();
if (_theme.WindowWidth > 0 && _theme.WindowHeight > 0)
{
this.Width = _theme.WindowWidth;
this.Height = _theme.WindowHeight;
}
InitializeCustomComponents();
InitializePrintComponents();
BindEvents();
ApplyTheme();
}
private void InitializeCustomComponents()
{
statusStrip = new StatusStrip();
lblStatus = new ToolStripStatusLabel("就绪 | 拖入图片或 Ctrl+O 导入");
statusStrip.Items.Add(lblStatus);
this.Controls.Add(statusStrip);
canvas = new ImageCanvas { Dock = DockStyle.Fill };
this.Controls.Add(canvas);
toolBar = new FlowLayoutPanel {
Dock = DockStyle.Top,
AutoSize = true,
FlowDirection = FlowDirection.LeftToRight,
WrapContents = true,
Padding = new Padding(5)
};
this.Controls.Add(toolBar);
CreateButton("导入 (Ctrl+O)", OnImportClick);
CreateButton("左转 (←)", (s, e) => canvas.Rotate(-90));
CreateButton("右转 (→)", (s, e) => canvas.Rotate(90));
CreateButton("水平镜像", (s, e) => canvas.ToggleMirrorX());
CreateButton("垂直镜像", (s, e) => canvas.ToggleMirrorY());
CreateButton("适应纸张 (R)", (s, e) => canvas.ResetTransform());
CreateButton("页面设置", OnPageSetupClick);
CreateButton("主题设置", OnThemeConfigClick);
CreateButton("保存 (Ctrl+S)", OnSaveClick);
CreateButton("打印 (Ctrl+P)", OnPrintClick);
}
private Button CreateButton(string text, EventHandler clickHandler)
{
var btn = new Button { Text = text, Width = 100, Height = 35, Margin = new Padding(5, 5, 0, 5), FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand };
btn.FlatAppearance.BorderSize = 1;
btn.Click += clickHandler;
toolBar.Controls.Add(btn);
return btn;
}
private void ApplyTheme()
{
this.BackColor = _theme.GetWindowBgColor();
toolBar.BackColor = _theme.GetWindowBgColor();
statusStrip.BackColor = _theme.GetWindowBgColor();
lblStatus.ForeColor = _theme.GetButtonTextColor();
foreach (Control c in toolBar.Controls)
{
if (c is Button btn)
{
btn.BackColor = _theme.GetButtonBgColor();
btn.ForeColor = _theme.GetButtonTextColor();
btn.FlatAppearance.BorderColor = ControlPaint.Dark(_theme.GetButtonBgColor());
btn.FlatAppearance.MouseOverBackColor = ControlPaint.Light(_theme.GetButtonBgColor());
}
}
canvas.ApplyTheme(_theme);
}
private void InitializePrintComponents()
{
openFileDialog = new OpenFileDialog { Filter = "图像文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.webp|所有文件|*.*", Title = "选择要导入的图片" };
saveFileDialog = new SaveFileDialog { Filter = "PNG 图像|*.png|JPEG 图像|*.jpg|BMP 图像|*.bmp", Title = "保存渲染后的图像" };
printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pageSetupDialog = new PageSetupDialog { Document = printDocument };
printDialog = new PrintDialog { Document = printDocument, UseEXDialog = true };
UpdateCanvasPageInfo();
}
private void UpdateCanvasPageInfo()
{
var ps = printDocument.DefaultPageSettings;
float w = ps.Bounds.Width / 100f;
float h = ps.Bounds.Height / 100f;
if (ps.Landscape && w < h) { float temp = w; w = h; h = temp; }
else if (!ps.Landscape && w > h) { float temp = w; w = h; h = temp; }
canvas.SetPageInfo(w, h,
ps.Margins.Top / 100f, ps.Margins.Bottom / 100f,
ps.Margins.Left / 100f, ps.Margins.Right / 100f);
}
private void BindEvents()
{
canvas.ImageChanged += (s, e) => { var img = canvas.GetOriginalImage(); if (img != null) lblStatus.Text = $"已加载 | 原始: {img.Width}x{img.Height}"; };
canvas.TransformChanged += (s, e) => lblStatus.Text = "视图已更新 | 虚线框内为最终打印区域";
canvas.PhysicalSizeChanged += (s, sizeStr) => lblStatus.Text = $"🎯 {sizeStr} | 左键拖图,滚轮缩放图 | Ctrl+滚轮缩放画布,中键/右键拖动画布";
printDocument.PrintPage += PrintDocument_PrintPage;
}
private void OnImportClick(object? sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) canvas.LoadImage(openFileDialog.FileName); }
private void OnThemeConfigClick(object? sender, EventArgs e)
{
using var frm = new ThemeConfigForm(_theme);
if (frm.ShowDialog(this) == DialogResult.OK)
{
_theme.Save();
ApplyTheme();
}
}
private void OnSaveClick(object? sender, EventArgs e)
{
var img = canvas.GetPrintableImage();
if (img == null) { MessageBox.Show("请先导入图片。"); return; }
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
var path = saveFileDialog.FileName;
var format = ImageFormat.Png;
if (path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)) format = ImageFormat.Jpeg;
else if (path.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)) format = ImageFormat.Bmp;
Task.Run(() => img.Save(path, format)).ContinueWith(t => this.Invoke(() => lblStatus.Text = t.IsFaulted ? "保存失败" : "保存成功"));
}
catch (Exception ex) { MessageBox.Show($"保存出错: {ex.Message}"); }
}
}
private void OnPageSetupClick(object? sender, EventArgs e)
{
if (pageSetupDialog.ShowDialog() == DialogResult.OK)
{
var ps = pageSetupDialog.PageSettings;
if (ps == null) return;
printDocument.DefaultPageSettings = ps;
var m = ps.Margins;
if (m.Left < 0 || m.Right < 0 || m.Top < 0 || m.Bottom < 0 ||
m.Left + m.Right > ps.Bounds.Width || m.Top + m.Bottom > ps.Bounds.Height) {
ps.Margins = new Margins(0, 0, 0, 0);
}
float w = ps.Bounds.Width / 100f;
float h = ps.Bounds.Height / 100f;
if (ps.Landscape && w < h) { float temp = w; w = h; h = temp; }
else if (!ps.Landscape && w > h) { float temp = w; w = h; h = temp; }
canvas.SetPageInfo(w, h,
ps.Margins.Top / 100f, ps.Margins.Bottom / 100f,
ps.Margins.Left / 100f, ps.Margins.Right / 100f);
lblStatus.Text = $"页面更新: {ps.PaperSize.PaperName}";
}
}
private void OnPrintClick(object? sender, EventArgs e)
{
if (canvas.GetOriginalImage() == null) { MessageBox.Show("请先导入图片。"); return; }
if (printDialog.ShowDialog() == DialogResult.OK)
{
try
{
lblStatus.Text = "正在生成物理裁剪图像...";
this.Update();
PreRenderForPrint();
printDocument.Print();
lblStatus.Text = "打印任务已提交...";
}
catch (Exception ex) { MessageBox.Show($"打印失败: {ex.Message}"); }
}
}
private void PreRenderForPrint()
{
_renderedPrintBitmap?.Dispose();
_renderedPrintBitmap = null;
var originalImage = canvas.GetOriginalImage();
if (originalImage == null) return;
var ps = printDocument.DefaultPageSettings;
float marginW_inch = (ps.Bounds.Width - ps.Margins.Left - ps.Margins.Right) / 100f;
float marginH_inch = (ps.Bounds.Height - ps.Margins.Top - ps.Margins.Bottom) / 100f;
int realDpiX = ps.PrinterResolution.X > 0 ? ps.PrinterResolution.X : 600;
int realDpiY = ps.PrinterResolution.Y > 0 ? ps.PrinterResolution.Y : 600;
int pxW = (int)(marginW_inch * realDpiX);
int pxH = (int)(marginH_inch * realDpiY);
if (pxW > 4000 || pxH > 4000)
{
float ratio = Math.Min(4000f / pxW, 4000f / pxH);
pxW = (int)(pxW * ratio); pxH = (int)(pxH * ratio);
}
if (pxW <= 0 || pxH <= 0) return;
var bitmap = new Bitmap(pxW, pxH, PixelFormat.Format24bppRgb);
using var g = Graphics.FromImage(bitmap);
g.Clear(_theme.GetPaperBgColor());
canvas.RenderToTarget(g, new RectangleF(0, 0, pxW, pxH));
string debugPath = @"D:\Documents\my\LMaxPrint\debug_print.png";
try { bitmap.Save(debugPath, ImageFormat.Png); } catch { }
_renderedPrintBitmap = bitmap;
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
if (_renderedPrintBitmap == null || e.Graphics == null) return;
RectangleF destRect = e.MarginBounds;
RectangleF srcRect = new RectangleF(0, 0, _renderedPrintBitmap.Width, _renderedPrintBitmap.Height);
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.DrawImage(_renderedPrintBitmap, destRect, srcRect, GraphicsUnit.Pixel);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
_theme.WindowWidth = this.Width;
_theme.WindowHeight = this.Height;
}
else
{
_theme.WindowWidth = this.RestoreBounds.Width;
_theme.WindowHeight = this.RestoreBounds.Height;
}
_theme.Save();
base.OnFormClosing(e);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.O: OnImportClick(this, EventArgs.Empty); return true;
case Keys.Control | Keys.P: OnPrintClick(this, EventArgs.Empty); return true;
case Keys.Control | Keys.S: OnSaveClick(this, EventArgs.Empty); return true;
case Keys.Left: canvas.Rotate(-90); return true;
case Keys.Right: canvas.Rotate(90); return true;
case Keys.R: canvas.ResetTransform(); return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}