geboshi_V1/LeatherProject/LeatherApp/UIExtend/UCImageView.cs

188 lines
7.1 KiB
C#

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 LeatherApp.UIExtend
{
public partial class UCImageView : UserControl
{
private double ratioStep = 0.1;
public UCImageView()
{
InitializeComponent();
pictureBox1.Location = new Point(0, 0);
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.MouseDown += pictureBox1_MouseDown;
}
#region private tool
/// <summary>
/// 获取适合大小
/// </summary>
/// <param name="sourceSize"></param>
/// <param name="targetSize"></param>
/// <returns></returns>
private Size getNewSize(Size sourceSize, Size targetSize)
{
if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
return sourceSize;
int num = sourceSize.Width / targetSize.Width;
int num2 = sourceSize.Height / targetSize.Height;
int num3 = (num < num2) ? num2 : num;
return new Size(sourceSize.Width / num3, sourceSize.Height / num3);
}
/// <summary>
/// 等比计算宽高
/// </summary>
/// <param name="sourceSize"></param>
/// <param name="targetSize"></param>
/// <returns></returns>
private SizeF getNewSize(SizeF sourceSize, SizeF targetSize)
{
if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
return sourceSize;
float num = sourceSize.Width / targetSize.Width;
float num2 = sourceSize.Height / targetSize.Height;
float num3 = (num < num2) ? num2 : num;
return new SizeF(sourceSize.Width / num3, sourceSize.Height / num3);
}
#endregion
public void clear()
{
pictureBox1.Image = null;
pictureBox1.Refresh();
pictureBox1.Visible = false;
}
public void loadImage(Image img)
{
if(img == null) { clear(); return; }
pictureBox1.Visible = true;
pictureBox1.Size = ClientSize;
pictureBox1.Location = new Point(0, 0);
pictureBox1.Image = img;//del
pictureBox1.Refresh();
//ActiveControl = pictureBox1; // 设置焦点
pictureBox1.Width = this.Width;
pictureBox1.Height = this.Height;
}
private void UCImageView_Load(object sender, EventArgs e)
{
pictureBox1.Width = this.Width;
pictureBox1.Height = this.Height;
}
private void checkBorderLine()
{
if (pictureBox1.Width < ClientSize.Width) pictureBox1.Width = ClientSize.Width;
if (pictureBox1.Height < ClientSize.Height) pictureBox1.Height = ClientSize.Height;
if (pictureBox1.Right < ClientSize.Width) pictureBox1.Left += ClientSize.Width - pictureBox1.Right;
if (pictureBox1.Bottom < ClientSize.Height) pictureBox1.Top += ClientSize.Height - pictureBox1.Bottom;
if (pictureBox1.Left > 0) pictureBox1.Left = 0;
if (pictureBox1.Top > 0) pictureBox1.Top = 0;
}
public void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (pictureBox1.Image == null) return;
int x = e.Location.X;
int y = e.Location.Y;
double oldW = pictureBox1.Width;
double oldH = pictureBox1.Height;
int width,height;
double ratio;
if (e.Delta > 0) //放大
{
ratio = 1 + ratioStep;
width = Convert.ToInt32(pictureBox1.Width * ratio);
height = Convert.ToInt32(pictureBox1.Height * ratio);
//if (width * height > 45800000)
// return;
if (width / ClientSize.Width > 10)
return;
pictureBox1.Width = width;
pictureBox1.Height = height;
}
if (e.Delta < 0) //缩小
{
//防止一直缩成负值
if (pictureBox1.Width < this.Width || pictureBox1.Height < this.Height)
return;
ratio = 1.0 - ratioStep;
width = (int)(pictureBox1.Width * ratio);
height= (int)(pictureBox1.Height * ratio);
pictureBox1.Width = width;
pictureBox1.Height = height;
}
this.Text = $"{e.X},{e.Y}";
//求因缩放产生的位移,进行补偿,实现锚点缩放的效果
int VX = (int)((oldW - pictureBox1.Width) / oldW * x);
int VY = (int)((oldH - pictureBox1.Height) / oldH * y);
pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
checkBorderLine();
}
//移动
private int xPos;
private int yPos;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBox1.Image == null) return;
try
{
// 鼠标按下拖拽图片
if (e.Button == MouseButtons.Left)
{
this.Parent.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
// 限制拖拽出框
if (pictureBox1.Width > ClientSize.Width
|| pictureBox1.Height > ClientSize.Height)
{
int moveX = e.X - xPos;
int moveY = e.Y - yPos;
if ((pictureBox1.Left + moveX) <= 0
&& (pictureBox1.Top + moveY) <= 0
&& (pictureBox1.Right + moveX) >= ClientSize.Width
&& (pictureBox1.Bottom + moveY) >= ClientSize.Height)
{
pictureBox1.Left += moveX;//设置x坐标.
pictureBox1.Top += moveY;//设置y坐标.
checkBorderLine();
}
}
}
}
catch (Exception dd)
{
MessageBox.Show(dd.Message);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image == null) return;
xPos = e.X;//当前x坐标.
yPos = e.Y;//当前y坐标.
}
private void UCImageView_ClientSizeChanged(object sender, EventArgs e)
{
checkBorderLine();
}
}
}