public class ZoomablePictureBox : PictureBox
{
private Image originalImage;
private float zoom = 1.0f;
private Point dragStart;
private Point imagePosition = Point.Empty;
private bool isDragging = false;
private const float ZOOM_FACTOR = 1.2f;
private const float MIN_ZOOM = 0.1f;
private const float MAX_ZOOM = 1.2f;
public List<***ponent> ***ponents =new List<***ponent>();
public float Zoom
{
get { return zoom; }
set
{
zoom = Math.Max(MIN_ZOOM, Math.Min(MAX_ZOOM, value));
Invalidate();
}
}
public new Image Image
{
get { return originalImage; }
set
{
originalImage = value;
zoom = 1.0f;
imagePosition = Point.Empty;
base.Image = value;
Invalidate();
}
}
public ZoomablePictureBox()
{
this.SizeMode = PictureBoxSizeMode.AutoSize;
this.MouseWheel += ZoomablePictureBox_MouseWheel;
this.MouseDown += ZoomablePictureBox_MouseDown;
this.MouseMove += ZoomablePictureBox_MouseMove;
this.MouseUp += ZoomablePictureBox_MouseUp;
}
PointF movePoint;
PointF mouseWorldPos;
protected override void OnPaint(PaintEventArgs pe)
{
if (originalImage != null && zoom != 1.0f)
{
// 自定义绘制缩放后的图像
var destRect = GetDestinationRectangle();
pe.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
pe.Graphics.DrawImage(originalImage, destRect);
// 绘制选中元件
if (IsSelect***pont)
{
float screenWidth = clicked***ponent.Size.Width * zoom;
float screenHeight = clicked***ponent.Size.Height * zoom;
Point point = ImageToScreenPoint(new Point((int)clicked***ponent.Position.X, (int)clicked***ponent.Position.Y));
using (Pen pen = new Pen(Color.Red, 3.0f))
using (Brush fillBrush = new SolidBrush(Color.FromArgb(60, Color.Red)))
{
//pe.Graphics.DrawRectangle(pen,new Rectangle(point.X-20, point.Y-20, 40, 40));
RectangleF rect = new RectangleF(
point.X-20,
point.Y - 20,
40,
40
);
pe.Graphics.DrawLine(pen, rect.X + rect.Width / 2, rect.Y + rect.Height, rect.X + rect.Width / 2, rect.Y + rect.Height + 100);
pe.Gr