private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// 获取用户输入的矩形中心点、长和宽
double centerX = Convert.ToDouble(textBox1.Text);
double centerY = Convert.ToDouble(textBox2.Text);
double width = Convert.ToDouble(textBox3.Text);
double height = Convert.ToDouble(textBox4.Text);
// 创建绘图对象
Graphics g = e.Graphics;
g.Clear(Color.White);
// 获取坐标轴原点的坐标
double originX = pictureBox1.Width / 2.0 ;
double originY = pictureBox1.Height / 2.0;
// 绘制坐标轴
Pen axisPen = new Pen(Color.Black, 2);
g.DrawLine(axisPen, 0, (float)(pictureBox1.Height / 2.0), pictureBox1.Width, (float)(pictureBox1.Height / 2.0)); // 水平线
g.DrawLine(axisPen, (float)(pictureBox1.Width / 2.0), pictureBox1.Height, (float)(pictureBox1.Width / 2.0), 0); // 垂直线
// 绘制箭头
Point[] arrow = new Point[3];
arrow[0] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 - 5);
arrow[1] = new Point(pictureBox1.Width, pictureBox1.Height / 2);
arrow[2] = new Point(pictureBox1.Width - 10, pictureBox1.Height / 2 + 5);
g.FillPolygon(Brushes.Black, arrow); // 水平箭头
arrow[0] = new Point(pictureBox1.Width / 2 - 5, 10);
arrow[1] = new Point(pictureBox1.Width / 2, 0);
arrow[2] = new Point(pictureBox1.Width / 2 + 5, 10);
g.FillPolygon(Brushes.Black, arrow); // 垂直箭头
// 计算矩形的四个顶点坐标
//centerX = centerX + originX;
//centerY = centerY + originY;
double left = centerX - width / 2.0;
double right = centerX + width / 2.0;
double top = centerY - height / 2.0;
double bottom = centerY + height / 2.0;
// 绘制矩形
Pen rectPen = new Pen(Color.Red, 2);
g.DrawRectangle(rectPen, (float)(originX + left), (float)(originY - bottom), (float)width, (float)height);
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
Rectangle rec = pictureBox1.DisplayRectangle;
Point p = new Point(e.X, e.Y);
int delta = e.Delta;
double zoomFactor = 1.2; // 缩放因子
if (delta < 0)
zoomFactor = 1 / zoomFactor; // 向下滚动,缩小图像
// 修改控件大小,缩放图像
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * zoomFactor);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * zoomFactor);
// 修改图像比例,缩放图像
pictureBox1.Scale(new SizeF((float)zoomFactor, (float)zoomFactor));
// 重新绘制图像
pictureBox1.Invalidate();
}
我这样可以将picturebox控件进行放大缩小,我想知道怎么才能只将绘制出的矩形进行放大缩小(类似于将坐标轴的刻度放大缩小,矩形随之改变),而不改变坐标系和控件的大小,我是初学者,求各位牛人指教一下。