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_MultiSystemPublic { public partial class Form_ImageShow : XtraForm { #region 变量 /// /// 需要显示的图片源 /// private byte[] Cur_Image = null; /// /// 当前图片 /// private Image image; /// /// 图片原始宽 /// private int image_Width; /// /// 图片原始高 /// private int image_Height; /// /// 是否鼠标移动 /// private bool isMouseDown = false; /// /// 绘制 /// System.Drawing.Point p1 = new System.Drawing.Point(); /// /// 绘制 /// System.Drawing.Point p2 = new System.Drawing.Point(); /// /// 是否需要居中显示 /// private bool IsShowInCenter = true; #endregion /// /// 查看图片 /// /// 图片源 public Form_ImageShow(byte[] _image) { InitializeComponent(); Cur_Image = _image; } /// /// 窗体加载 /// /// /// private void Form_ServiceImageShow_Load(object sender, EventArgs e) { //注册鼠标滚动事件 this.MouseWheel += Form_MouseWheel; //显示图片 ShowImage(Cur_Image); } /// /// 鼠标滚动事件 /// /// /// 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); } } /// /// 展示图片 /// /// 图片源 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; //double length = (double)_image.Length / (1024 * 1024); //double length2 = (double)_image.Length / 1024 / 1024; //首次显示时显示图片大小 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 { //double length = (double)Cur_Image.Length / (1024 * 1024); 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); } } /// /// 旋转90° /// /// /// 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); //double length = (double)Cur_Image.Length / (1024 * 1024); 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); } } } }