1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
 
namespace CSFrameworkV5.Order
{
    public partial class FrmPictViwe : Form
    {
        private float scaleFactor = 1.0f; // 初始缩放比例为1
        private Image originalImage; // 原始图片
        private Point lastMousePos; // 记录鼠标按下的位置
        private Point imageOffset; // 图片的偏移量
        private bool isDragging; // 是否正在拖动图片
 
        public FrmPictViwe(Image pth)
        {
            InitializeComponent();
            pictureBox1.SizeMode =
                PictureBoxSizeMode.AutoSize; // 自动调整 PictureBox 大小以适应图片
            pictureBox1.MouseWheel +=
                PictureBox1_MouseWheel; // 订阅 MouseWheel 事件
            pictureBox1.MouseDown += PictureBox1_MouseDown; // 订阅 MouseDown 事件
            pictureBox1.MouseMove += PictureBox1_MouseMove; // 订阅 MouseMove 事件
            pictureBox1.Paint += PictureBox1_Paint; // 订阅 Paint 事件
            pictureBox1.MouseUp += PictureBox1_MouseUp; // 订阅 MouseUp 事件
            DoubleBuffered = true; // 启用双缓冲绘制
 
 
            try
            {
                originalImage = pth; // 加载图片
                pictureBox1.Image = originalImage; // 设置原始图片
                pictureBox1.Invalidate(); // 刷新PictureBox
            }
            catch (Exception ex)
            {
                MessageBox.Show("图号未上传,请先上传");
                return;
            }
        }
 
        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                // 计算缩放比例
                var zoomFactor = e.Delta > 0 ? 1.2f : 0.8f; // 向上滚动放大,向下滚动缩小
 
                // 更新缩放比例
                scaleFactor *= zoomFactor;
 
                // 计算缩放后的图片大小
                var newWidth = (int)(originalImage.Width * scaleFactor);
                var newHeight = (int)(originalImage.Height * scaleFactor);
 
                // 创建缩放后的图片
                Image scaledImage =
                    new Bitmap(originalImage, newWidth, newHeight);
 
                // 更新 PictureBox 的 Image
                pictureBox1.Image = scaledImage;
 
                // 刷新PictureBox
                pictureBox1.Invalidate();
            }
        }
 
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                lastMousePos = e.Location; // 记录鼠标按下的位置
                isDragging = true; // 开始拖动图片
            }
        }
 
        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                // 计算鼠标移动的距离
                var deltaX = e.X - lastMousePos.X;
                var deltaY = e.Y - lastMousePos.Y;
 
                // 更新图片的偏移量
                imageOffset.X += deltaX;
                imageOffset.Y += deltaY;
 
                // 更新鼠标按下的位置
                lastMousePos = e.Location;
 
                // 刷新PictureBox
                pictureBox1.Invalidate();
            }
        }
 
        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
 
            // 清空画布
            g.Clear(Color.White);
 
            // 平移绘图坐标系
            g.TranslateTransform(imageOffset.X, imageOffset.Y);
 
            // 绘制图片
            g.DrawImage(pictureBox1.Image, Point.Empty);
        }
 
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false; // 停止拖动图片
 
                // 计算缩放后的图片大小
                var newWidth = (int)(originalImage.Width * scaleFactor);
                var newHeight = (int)(originalImage.Height * scaleFactor);
 
                // 调整PictureBox的大小以适应缩放比例
                pictureBox1.Size = new Size(newWidth, newHeight);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // 计算缩放比例
            scaleFactor *= 1.2f;
 
            // 计算缩放后的图片大小
            var newWidth = (int)(originalImage.Width * scaleFactor);
            var newHeight = (int)(originalImage.Height * scaleFactor);
 
            // 创建缩放后的图片
            Image scaledImage = new Bitmap(originalImage, newWidth, newHeight);
 
            // 更新PictureBox的Image
            pictureBox1.Image = scaledImage;
 
            // 刷新PictureBox
            pictureBox1.Invalidate();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            // 计算缩放比例
            scaleFactor *= 0.8f;
 
            // 计算缩放后的图片大小
            var newWidth = (int)(originalImage.Width * scaleFactor);
            var newHeight = (int)(originalImage.Height * scaleFactor);
 
            // 创建缩放后的图片
            Image scaledImage = new Bitmap(originalImage, newWidth, newHeight);
 
            // 更新PictureBox的Image
            pictureBox1.Image = scaledImage;
 
            // 刷新PictureBox
            pictureBox1.Invalidate();
        }
 
 
        private void btnPrint_Click(object sender, EventArgs e)
        {
            var printDocument = new PrintDocument();
            printDocument.PrintPage += new PrintPageEventHandler(PrintImage);
            var printDialog = new PrintDialog();
            printDialog.Document = printDocument;
            if (printDialog.ShowDialog() == DialogResult.OK)
                printDocument.Print();
        }
 
        private void PrintImage(object sender, PrintPageEventArgs e)
        {
            var imageToPrint = originalImage;
            // 获取图像的原始尺寸
            var imageWidth = imageToPrint.Width;
            var imageHeight = imageToPrint.Height;
            // 获取打印区域的尺寸
            var printArea = e.MarginBounds;
            var printWidth = printArea.Width;
            var printHeight = printArea.Height;
            // 等比例缩放图像以适应打印区域
            var scale = Math.Min((float)printWidth / imageWidth,
                (float)printHeight / imageHeight);
            var scaledWidth = (int)(imageWidth * scale * 1.2);
            var scaledHeight = (int)(imageHeight * scale * 1.2);
            // 计算图像在打印区域的位置
            var x = printArea.Left + (printWidth - scaledWidth) / 2;
            var y = printArea.Top + (printHeight - scaledHeight) / 2;
            // 绘制图像
            e.Graphics.DrawImage(imageToPrint, x, y, scaledWidth, scaledHeight);
        }
    }
}