using System;
|
using System.Drawing;
|
using System.Drawing.Printing;
|
using System.Windows.Forms;
|
|
namespace CSFrameworkV5.Order
|
{
|
public partial class FrmPictViwe : Form
|
{
|
private float scaleFactor = 1.0f; // 初始缩放比例为1
|
private Image originalImage; // 原始图片
|
private Point lastMousePos; // 记录鼠标按下的位置
|
private Point imageOffset; // 图片的偏移量
|
private bool isDragging; // 是否正在拖动图片
|
|
public FrmPictViwe(Image pth)
|
{
|
InitializeComponent();
|
pictureBox1.SizeMode =
|
PictureBoxSizeMode.AutoSize; // 自动调整 PictureBox 大小以适应图片
|
pictureBox1.MouseWheel +=
|
PictureBox1_MouseWheel; // 订阅 MouseWheel 事件
|
pictureBox1.MouseDown += PictureBox1_MouseDown; // 订阅 MouseDown 事件
|
pictureBox1.MouseMove += PictureBox1_MouseMove; // 订阅 MouseMove 事件
|
pictureBox1.Paint += PictureBox1_Paint; // 订阅 Paint 事件
|
pictureBox1.MouseUp += PictureBox1_MouseUp; // 订阅 MouseUp 事件
|
DoubleBuffered = true; // 启用双缓冲绘制
|
|
|
try
|
{
|
originalImage = pth; // 加载图片
|
pictureBox1.Image = originalImage; // 设置原始图片
|
pictureBox1.Invalidate(); // 刷新PictureBox
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show("图号未上传,请先上传");
|
return;
|
}
|
}
|
|
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
|
{
|
if (pictureBox1.Image != null)
|
{
|
// 计算缩放比例
|
var zoomFactor = e.Delta > 0 ? 1.2f : 0.8f; // 向上滚动放大,向下滚动缩小
|
|
// 更新缩放比例
|
scaleFactor *= zoomFactor;
|
|
// 计算缩放后的图片大小
|
var newWidth = (int)(originalImage.Width * scaleFactor);
|
var newHeight = (int)(originalImage.Height * scaleFactor);
|
|
// 创建缩放后的图片
|
Image scaledImage =
|
new Bitmap(originalImage, newWidth, newHeight);
|
|
// 更新 PictureBox 的 Image
|
pictureBox1.Image = scaledImage;
|
|
// 刷新PictureBox
|
pictureBox1.Invalidate();
|
}
|
}
|
|
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
|
{
|
if (e.Button == MouseButtons.Left)
|
{
|
lastMousePos = e.Location; // 记录鼠标按下的位置
|
isDragging = true; // 开始拖动图片
|
}
|
}
|
|
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
|
{
|
if (isDragging)
|
{
|
// 计算鼠标移动的距离
|
var deltaX = e.X - lastMousePos.X;
|
var deltaY = e.Y - lastMousePos.Y;
|
|
// 更新图片的偏移量
|
imageOffset.X += deltaX;
|
imageOffset.Y += deltaY;
|
|
// 更新鼠标按下的位置
|
lastMousePos = e.Location;
|
|
// 刷新PictureBox
|
pictureBox1.Invalidate();
|
}
|
}
|
|
private void PictureBox1_Paint(object sender, PaintEventArgs e)
|
{
|
var g = e.Graphics;
|
|
// 清空画布
|
g.Clear(Color.White);
|
|
// 平移绘图坐标系
|
g.TranslateTransform(imageOffset.X, imageOffset.Y);
|
|
// 绘制图片
|
g.DrawImage(pictureBox1.Image, Point.Empty);
|
}
|
|
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
|
{
|
if (e.Button == MouseButtons.Left)
|
{
|
isDragging = false; // 停止拖动图片
|
|
// 计算缩放后的图片大小
|
var newWidth = (int)(originalImage.Width * scaleFactor);
|
var newHeight = (int)(originalImage.Height * scaleFactor);
|
|
// 调整PictureBox的大小以适应缩放比例
|
pictureBox1.Size = new Size(newWidth, newHeight);
|
}
|
}
|
|
private void button1_Click(object sender, EventArgs e)
|
{
|
// 计算缩放比例
|
scaleFactor *= 1.2f;
|
|
// 计算缩放后的图片大小
|
var newWidth = (int)(originalImage.Width * scaleFactor);
|
var newHeight = (int)(originalImage.Height * scaleFactor);
|
|
// 创建缩放后的图片
|
Image scaledImage = new Bitmap(originalImage, newWidth, newHeight);
|
|
// 更新PictureBox的Image
|
pictureBox1.Image = scaledImage;
|
|
// 刷新PictureBox
|
pictureBox1.Invalidate();
|
}
|
|
private void button2_Click(object sender, EventArgs e)
|
{
|
// 计算缩放比例
|
scaleFactor *= 0.8f;
|
|
// 计算缩放后的图片大小
|
var newWidth = (int)(originalImage.Width * scaleFactor);
|
var newHeight = (int)(originalImage.Height * scaleFactor);
|
|
// 创建缩放后的图片
|
Image scaledImage = new Bitmap(originalImage, newWidth, newHeight);
|
|
// 更新PictureBox的Image
|
pictureBox1.Image = scaledImage;
|
|
// 刷新PictureBox
|
pictureBox1.Invalidate();
|
}
|
|
|
private void btnPrint_Click(object sender, EventArgs e)
|
{
|
var printDocument = new PrintDocument();
|
printDocument.PrintPage += new PrintPageEventHandler(PrintImage);
|
var printDialog = new PrintDialog();
|
printDialog.Document = printDocument;
|
if (printDialog.ShowDialog() == DialogResult.OK)
|
printDocument.Print();
|
}
|
|
private void PrintImage(object sender, PrintPageEventArgs e)
|
{
|
var imageToPrint = originalImage;
|
// 获取图像的原始尺寸
|
var imageWidth = imageToPrint.Width;
|
var imageHeight = imageToPrint.Height;
|
// 获取打印区域的尺寸
|
var printArea = e.MarginBounds;
|
var printWidth = printArea.Width;
|
var printHeight = printArea.Height;
|
// 等比例缩放图像以适应打印区域
|
var scale = Math.Min((float)printWidth / imageWidth,
|
(float)printHeight / imageHeight);
|
var scaledWidth = (int)(imageWidth * scale * 1.2);
|
var scaledHeight = (int)(imageHeight * scale * 1.2);
|
// 计算图像在打印区域的位置
|
var x = printArea.Left + (printWidth - scaledWidth) / 2;
|
var y = printArea.Top + (printHeight - scaledHeight) / 2;
|
// 绘制图像
|
e.Graphics.DrawImage(imageToPrint, x, y, scaledWidth, scaledHeight);
|
}
|
}
|
}
|