using Newtonsoft.Json.Linq; using ProductionControl.Device; using ProductionControl.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace ProductionControl.UI { public partial class FrmScannerShow2 : Form { private Size size; #region pic缩放变量 private double ratio = 1; // 图片的起始显示比例 private double ratioStep = 0.1; private Size pic_size; private int xPos; private int yPos; #endregion ScannerDev scannerDev; public IntPtr picHwnd { get { return this.pictureBox1.Handle; } } public FrmScannerShow2(Size _size, ScannerDev dev = null) { InitializeComponent(); scannerDev = dev; size = _size; this.SizeChanged += FrmScannerShow2_SizeChanged; FrmScannerShow2_SizeChanged(null,null); //this.ClientSize = new Size(this.ClientSize.Width, // (int)(this.ClientSize.Width * (_size.Height * 1.0f / _size.Width))); // //this.Width = Screen.PrimaryScreen.WorkingArea.Width / 2; //this.Location = new Point(0, 0); } private void FrmScannerShow2_SizeChanged(object sender, EventArgs e) { var newSize = Util.getNewSize(this.ClientSize, new Size(size.Width * 100, size.Height * 100)); this.pnlPic.Size = newSize; if (this.pnlPic.Width < this.ClientSize.Width) this.pnlPic.Left = (this.ClientSize.Width - this.pnlPic.Width) / 2; if (this.pnlPic.Height < this.ClientSize.Height) this.pnlPic.Top = (this.ClientSize.Height - this.pnlPic.Height) / 2; } private void FrmPhotoShow_Load(object sender, EventArgs e) { } private void FrmScannerShow2_FormClosed(object sender, FormClosedEventArgs e) { if (scannerDev != null) { scannerDev.stop(); scannerDev.close(); scannerDev = null; } } //---------- #region 图片缩放控制 private void reloadPic(Bitmap bmp) { var newSize = Util.getNewSize(this.ClientSize, new Size(bmp.Size.Width * 100, bmp.Size.Height * 100)); this.pnlPic.Size = newSize; if (this.pnlPic.Width < this.ClientSize.Width) this.pnlPic.Left = (this.ClientSize.Width - this.pnlPic.Width) / 2; if (this.pnlPic.Height < this.ClientSize.Height) this.pnlPic.Top = (this.ClientSize.Height - this.pnlPic.Height) / 2; ratio = 1.0;// 图片的起始显示比例 Size size = bmp.Size; //this.pnlPic.ClientSize = new Size(this.pnlPic.ClientSize.Width, // (int)(this.pnlPic.ClientSize.Width * (size.Height * 1.0f / size.Width))); this.pictureBox1.Size = this.pnlPic.ClientSize; this.pictureBox1.Location = new Point(0, 0); this.pictureBox1.Image = bmp;//del pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel); pictureBox1.MouseMove += pictureBox1_MouseMove; pictureBox1.MouseDown += pictureBox1_MouseDown; this.ActiveControl = this.pictureBox1; // 设置焦点 pic_size = this.pnlPic.ClientSize; } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { Point pictureBoxPoint = this.PointToClient(Cursor.Position); //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " + // $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " + // $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}"; if (e.Delta > 0) { ratio += ratioStep; if (ratio > 100) // 放大上限 ratio = 100; else { int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5); int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5); //this.Text = $"{pictureBox1.Width}-{pic_size.Width};{ratio},{pictureBox1.Left}-{e.X}* 比例:{e.X*1.0d/pictureBox1.Width}={e.X}/{pictureBox1.Width} * {(ratioStep)}={x} | " + // $"{pictureBox1.Top}-{e.Y}* {e.Y * 1.0f / pictureBox1.Height * (ratio - 1.0d)}={y}"; this.changePictureBoxSize(new Point(x, y), ratio); } } else if (ratio - ratioStep >= 1) { ratio -= ratioStep; //if (ratio < 0.1) // 放大下限 // ratio = 0.1; //else int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5); int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5); this.changePictureBoxSize(new Point(x, y), ratio); } } private void changePictureBoxSize(Point location, double ratio) { var picSize = pictureBox1.Size; picSize.Width = Convert.ToInt32(pic_size.Width * ratio); picSize.Height = Convert.ToInt32(pic_size.Height * ratio); pictureBox1.Size = picSize; if (location.X > 0) location.X = 0; if (location.Y > 0) location.Y = 0; if (picSize.Width + location.X < this.pnlPic.ClientSize.Width) location.X = -(picSize.Width - this.pnlPic.ClientSize.Width); if (picSize.Height + location.Y < this.pnlPic.ClientSize.Height) location.Y = -(picSize.Height - this.pnlPic.ClientSize.Height); //Point location = new Point(); //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2; //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2; this.pictureBox1.Location = location; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { try { // 鼠标按下拖拽图片 if (e.Button == MouseButtons.Left) { //this.Text = $"{e.X}:{e.Y};{xPos}:{yPos}"; // 限制拖拽出框 if (pictureBox1.Width > this.pnlPic.ClientSize.Width || pictureBox1.Height > this.pnlPic.ClientSize.Height) { int moveX = e.X - xPos; int moveY = e.Y - yPos; if ((pictureBox1.Left + moveX) <= 0 && (pictureBox1.Top + moveY) <= 0 && (pictureBox1.Right + moveX) >= this.pnlPic.ClientSize.Width && (pictureBox1.Bottom + moveY) >= this.pnlPic.ClientSize.Height) { pictureBox1.Left += moveX;//设置x坐标. pictureBox1.Top += moveY;//设置y坐标. } } } } catch (Exception dd) { MessageBox.Show(dd.Message); } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { xPos = e.X;//当前x坐标. yPos = e.Y;//当前y坐标. } #endregion } }