StableVersion4.3/HL_FristAidPlatform_EMRS/Form_Image.cs

210 lines
6.3 KiB
C#
Raw Permalink Normal View History

2024-03-11 09:47:34 +08:00
using DevExpress.XtraEditors;
using HL_FristAidPlatform_Public;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace HL_FristAidPlatform_EMRS
{
public partial class Form_Image : XtraForm
{
#region 变量
/// <summary>
/// 需要显示的图片源
/// </summary>
private byte[] Cur_Image = null;
/// <summary>
/// 当前图片
/// </summary>
private Image image;
/// <summary>
/// 图片原始宽
/// </summary>
private int image_Width;
/// <summary>
/// 图片原始高
/// </summary>
private int image_Height;
/// <summary>
/// 是否鼠标移动
/// </summary>
private bool isMouseDown = false;
/// <summary>
/// 绘制
/// </summary>
System.Drawing.Point p1 = new System.Drawing.Point();
/// <summary>
/// 绘制
/// </summary>
System.Drawing.Point p2 = new System.Drawing.Point();
/// <summary>
/// 是否需要居中显示
/// </summary>
private bool IsShowInCenter = true;
#endregion
/// <summary>
/// 查看图片
/// </summary>
/// <param name="_image">图片源</param>
public Form_Image(byte[] _image)
{
InitializeComponent();
Cur_Image = _image;
}
private void Form_Image_Load(object sender, EventArgs e)
{
//注册鼠标滚动事件
this.MouseWheel += Form_MouseWheel;
//显示图片
ShowImage(Cur_Image);
}
/// <summary>
/// 鼠标滚动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0) //放大图片bai
{
pictureBox.Size = new Size(pictureBox.Width + 50, pictureBox.Height + 50);
}
else
{
int Width = pictureBox.Width - 50;
int Height = pictureBox.Height - 50;
//不能小于10
if (Width <= 10)
{
Width = 10;
}
if (Height <= 10)
{
Height = 10;
}
//缩小图片
pictureBox.Size = new Size(Width, Height);
}
if (IsShowInCenter)
{
//设置图片在窗体居中
pictureBox.Location = new Point((this.Width - pictureBox.Width) / 2, (this.Height - pictureBox.Height) / 2);
}
}
/// <summary>
/// 展示图片
/// </summary>
/// <param name="_image">图片源</param>
private void ShowImage(byte[] _image)
{
if (Cur_Image != null)
{
try
{
MemoryStream ms = new MemoryStream(_image);
image = Image.FromStream(ms);
pictureBox.Image = image;
//图片原始值
image_Width = image.Width;
image_Height = image.Height;
//首次显示时显示图片大小
pictureBox.Size = new Size(image_Width, image_Height);
//设置图片在窗体居中
pictureBox.Location = new Point((this.Width - pictureBox.Width) / 2, (this.Height - pictureBox.Height) / 2);
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "展示图片:\r\n" + ex);
}
}
else
{
XtraMessageBox.Show("没有需要显示的图片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_Save_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Image Files(*.JPG;*.PNG;*.jpeg;*.GIF;*.BMP)|*.JPG;*.PNG;*.GIF;*.BMP;*.jpeg|All files(*.*)|*.*";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.Title = "保存";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileStream fs = (FileStream)saveFileDialog.OpenFile();
pictureBox.Image.Save(fs, ImageFormat.Bmp);
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存心电图至电脑:\r\n" + ex);
}
}
private void btn_Revolve_Click(object sender, EventArgs e)
{
image.RotateFlip(RotateFlipType.Rotate90FlipXY);
pictureBox.Image = image;
}
private void btn_Refresh_Click(object sender, EventArgs e)
{
pictureBox.Size = new Size(image_Width, image_Height);
IsShowInCenter = true;
//设置图片在窗体居中
pictureBox.Location = new Point((this.Width - pictureBox.Width) / 2, (this.Height - pictureBox.Height) / 2);
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
IsShowInCenter = false;
isMouseDown = true;
p1 = PointToClient(Control.MousePosition);//记录鼠标坐标
p2 = pictureBox.Location;
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
IsShowInCenter = false;
isMouseDown = false;
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
//图片坐标计算&赋值
if (isMouseDown)
{
int a = PointToClient(Control.MousePosition).X - p1.X;
int b = PointToClient(Control.MousePosition).Y - p1.Y;
IsShowInCenter = false;
//图片新的坐标 = 图片起始坐标 + 鼠标相对位移}
pictureBox.Location = new System.Drawing.Point(p2.X + a, p2.Y + b);
}
}
}
}