-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageViewForm.cs
168 lines (152 loc) · 6.15 KB
/
ImageViewForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Li.krkrfgformat
{
//public delegate void MainFormImageChangeValue(Bitmap bitmap);
public partial class ImageViewForm : Form
{
//public event MainFormImageChangeValue MainFormImageChange;
//
//移动图片相关
//
private bool pictureBoxIsMoving = false;
private Point pictureBoxMouseFristDownLocation = new Point();
private int mouseXOffset = 0, mouseYOffset = 0;
private Point pictureBoxDefultLocation = new Point(0, 0);
private int panelXStart, panelYStart;
public Image Picture { set; get; }
public ImageViewForm()
{
InitializeComponent();
}
private void ImageViewBox_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBoxIsMoving && imageViewBox.SizeMode == PictureBoxSizeMode.AutoSize)
{
if (e.Button == MouseButtons.Left)
{
mouseXOffset = Control.MousePosition.X - pictureBoxMouseFristDownLocation.X;
mouseYOffset = Control.MousePosition.Y - pictureBoxMouseFristDownLocation.Y;
panel1.HorizontalScroll.Value = (panelXStart - mouseXOffset > 0) ? panelXStart - mouseXOffset : 0;
panel1.VerticalScroll.Value = (panelYStart - mouseYOffset > 0) ? panelYStart - mouseYOffset : 0;
imageViewBox.Refresh();
}
}
}
private void ImageViewBox_MouseUp(object sender, MouseEventArgs e)
{
pictureBoxIsMoving = false;
imageViewBox.Cursor = Cursors.Default;
}
private void ImageViewBox_MouseDown(object sender, MouseEventArgs e)
{
if (imageViewBox.SizeMode == PictureBoxSizeMode.AutoSize)
{
if (e.Button == MouseButtons.Left)
{
pictureBoxIsMoving = true;
pictureBoxMouseFristDownLocation = Control.MousePosition;
panelXStart = panel1.HorizontalScroll.Value;
panelYStart = panel1.VerticalScroll.Value;
imageViewBox.Cursor = Cursors.Hand;
}
}
}
public void PictureChange(Bitmap bitmap)
{
//int XScroll = panel1.HorizontalScroll.Value;
//int YScroll = panel1.VerticalScroll.Value;
//Size oldPicSize = Picture.Size;
panel1.AutoScroll = true;
Picture = bitmap;
//imageViewBox.BackgroundImage = Li.Drawing.Picture.CreatBackImage(Picture.Width, Picture.Height);
imageViewBox.Image = Picture;
PicLocationSet();
//panel1.HorizontalScroll.Value = (XScroll / oldPicSize.Width) * Picture.Size.Width;
//panel1.VerticalScroll.Value = (YScroll / oldPicSize.Height) * Picture.Size.Height;
//PicLocationSet();
}
private void ImageViewForm_SizeChanged(object sender, EventArgs e)
{
if(imageViewBox.SizeMode == PictureBoxSizeMode.Zoom)
{
imageViewBox.Size = panel1.Size;
}
else
{
PicLocationSet();
}
}
private void ToolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedItem = toolStripComboBox1.SelectedIndex;
switch (selectedItem)
{
case 0:
panel1.AutoScroll = true;
imageViewBox.SizeMode = PictureBoxSizeMode.AutoSize;
break;
case 1:
panel1.AutoScroll = false;
imageViewBox.Size = panel1.Size;
imageViewBox.Location = new Point(0, 0);
imageViewBox.SizeMode = PictureBoxSizeMode.Zoom;
break;
default:
panel1.AutoScroll = false;
imageViewBox.Location = new Point(0, 0);
imageViewBox.Size = panel1.Size;
imageViewBox.SizeMode = PictureBoxSizeMode.Zoom;
break;
}
}
private void ImageView_Load(object sender, EventArgs e)
{
toolStripComboBox1.SelectedIndex = 0;
panel1.AutoScroll = true;
panel1.HorizontalScroll.Value = 0;
panel1.VerticalScroll.Value = 0;
imageViewBox.Size = Picture.Size;
imageViewBox.Location = new Point(0, 0);
//imageViewBox.BackgroundImage = Li.Drawing.Picture.CreatBackImage(Picture.Width, Picture.Height);
imageViewBox.Image = Picture;
}
private void PicLocationSet()
{
if (imageViewBox.Image == null)
return;
if (imageViewBox.Image.Width > panel1.Size.Width || imageViewBox.Image.Height > panel1.Size.Height)
{
panel1.AutoScroll = true;
}
else
{
imageViewBox.BackColor = System.Drawing.Color.White;
imageViewBox.Size = imageViewBox.Image.Size;
Point p = new Point
{
X = panel1.Size.Width / 2 - (imageViewBox.Image.Width / 2),
Y = panel1.Size.Height / 2 - (imageViewBox.Image.Height / 2)
};
imageViewBox.Location = p;
panel1.AutoScroll = true;
}
}
public void ClearForm()
{
imageViewBox.Image = null;
panel1.AutoScroll = false;
panel1.HorizontalScroll.Value = 0;
panel1.VerticalScroll.Value = 0;
imageViewBox.Size = new Size(0, 0);
toolStripComboBox1.SelectedIndex = 0;
}
}
}