using Newtonsoft.Json.Linq; 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 { public partial class FrmPhotoShow : Form { List lstBmp; private int picIndex=0; // #region pic缩放变量 private double ratio = 1; // 图片的起始显示比例 private double ratioStep = 0.1; private Size pic_size; private int xPos; private int yPos; #endregion public FrmPhotoShow(List list) { InitializeComponent(); lstBmp = list; // //this.Width = Screen.PrimaryScreen.WorkingArea.Width / 2; //this.Location = new Point(0, 0); } protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) return false; else return base.ProcessDialogKey(keyData); } private void FrmPhotoShow_Load(object sender, EventArgs e) { } private void FrmPhotoShow_Shown(object sender, EventArgs e) { this.reloadPic(lstBmp[picIndex]); tslblPageNum.Text = $"第 {picIndex + 1}/{lstBmp.Count} 张"; tsbtnPre.Enabled = (picIndex > 0); tsbtnNext.Enabled = (picIndex < lstBmp.Count - 1); } private void tsbtnPre_Click(object sender, EventArgs e) { if (picIndex <= 0) return; reloadPic(lstBmp[--picIndex]); tslblPageNum.Text = $"第 {picIndex + 1}/{lstBmp.Count} 张"; tsbtnPre.Enabled = (picIndex > 0); tsbtnNext.Enabled = (picIndex < lstBmp.Count - 1); } private void tsbtnNext_Click(object sender, EventArgs e) { if (picIndex >=lstBmp.Count-1) return; reloadPic(lstBmp[++picIndex]); tslblPageNum.Text = $"第 {picIndex + 1}/{lstBmp.Count} 张"; tsbtnPre.Enabled = (picIndex > 0); tsbtnNext.Enabled = (picIndex < lstBmp.Count - 1); } private void FrmPhoto_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: case Keys.Left: if (!this.tsbtnPre.Enabled) return; tsbtnPre_Click(null, null); break; case Keys.Down: case Keys.Right: if (!this.tsbtnNext.Enabled) return; tsbtnNext_Click(null, null); break; } } //---------- #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 private void tsbtnClose_Click(object sender, EventArgs e) { this.Close(); } } }