571 lines
21 KiB
C#
571 lines
21 KiB
C#
using DevExpress.XtraEditors;
|
||
using FellowOakDicom;
|
||
using FellowOakDicom.Imaging;
|
||
using HL_FristAidPlatform_DTO;
|
||
using HL_FristAidPlatform_Public;
|
||
using HL_FristAidPlatform_Public.Public;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Threading;
|
||
using System.Windows.Forms;
|
||
|
||
namespace HL_FristAidPlatform_MultiSystemPublic
|
||
{
|
||
public partial class Form_ImageInformation : XtraForm
|
||
{
|
||
#region 变量
|
||
//1.声明自适应类实例
|
||
AutoSizeFormClass autoSizeForm = new AutoSizeFormClass();
|
||
|
||
private DicomFile _file;
|
||
|
||
private DicomImage _image;
|
||
|
||
private bool _grayscale;
|
||
|
||
private double _windowWidth;
|
||
|
||
private double _windowCenter;
|
||
|
||
private int _frame;
|
||
|
||
private Image _current;
|
||
|
||
private Image _previous;
|
||
private int index = 0;
|
||
|
||
private DataTable dataTable = new DataTable();
|
||
|
||
/// <summary>
|
||
/// 当前病人编号(GUID)
|
||
/// </summary>
|
||
private string Cur_PatientGuid = string.Empty;
|
||
|
||
/// <summary>
|
||
/// 当前系统模块编号(ID)
|
||
/// </summary>
|
||
private string Cur_SystemModuleID = string.Empty;
|
||
#endregion
|
||
|
||
public Form_ImageInformation()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
/// <summary>
|
||
/// CT影像
|
||
/// </summary>
|
||
/// <param name="_patientGuid"></param>
|
||
/// <param name="_systemModuleID"></param>
|
||
public Form_ImageInformation(string _patientGuid, string _systemModuleID)
|
||
{
|
||
Cur_PatientGuid = _patientGuid;
|
||
Cur_SystemModuleID = _systemModuleID;
|
||
InitializeComponent();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 窗体加载
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void Form_ImageInformation_Load(object sender, EventArgs e)
|
||
{
|
||
BindList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载数据列表
|
||
/// </summary>
|
||
private void BindList()
|
||
{
|
||
try
|
||
{
|
||
string Url = string.Format("api/service/T_Service_Image/GetList?patientGuid={0}&systemModuleID={1}&fileType={2}", Cur_PatientGuid, Cur_SystemModuleID, (int)Enumerate.ServiceImageFileType.CT检查);
|
||
dataTable = DBHelpClass.Get(Url);
|
||
if (dataTable != null && dataTable.Rows.Count > 0)
|
||
{
|
||
MemoryStream ms = new MemoryStream(Convert.FromBase64String(dataTable.Rows[index]["FileImage"].ToString()));
|
||
SelectImage(ms, dataTable.Rows[index]["FileName"] + "");
|
||
groupControl_Show.Text = string.Format("报告显示区【共{0}张,当前第1张】", dataTable.Rows.Count);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog(this.Text, "加载数据列表:\r\n" + ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上传影像图片
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void simpleButton_DIC_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
#region 上传影像图片
|
||
OpenFileDialog openFileDialog1 = new OpenFileDialog();
|
||
openFileDialog1.Filter = "影像文件(*.dcm;*.JPG;*.PNG;*.jpeg;*.BMP)|*.JPG;*.PNG;*.BMP;*.jpeg|All files(*.*)|*.*";
|
||
openFileDialog1.FilterIndex = 2;
|
||
openFileDialog1.RestoreDirectory = true;
|
||
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
FileInfo file = new FileInfo(openFileDialog1.FileName);
|
||
string str = "";
|
||
if (file.Extension == ".dcm" || file.Extension == "")
|
||
{
|
||
DicomFile dcmFile = DicomFile.Open(openFileDialog1.FileName);
|
||
_file = dcmFile;
|
||
//DicomImage dcmImage = new DicomImage(dcmFile.Dataset);//可以增加第二个参数来指定获取第几帧
|
||
//IImage image = dcmImage.RenderImage();
|
||
//Bitmap bitmap = image.AsSharedBitmap();
|
||
using (Stream stream = dcmFile.File.OpenRead())
|
||
{
|
||
stream.Seek(0, SeekOrigin.Begin);
|
||
Byte[] picData = new byte[stream.Length];
|
||
stream.Read(picData, 0, picData.Length);
|
||
str = System.Convert.ToBase64String(picData);
|
||
}
|
||
ThreadPool.QueueUserWorkItem(
|
||
delegate
|
||
{
|
||
try
|
||
{
|
||
_image = new DicomImage(_file.Dataset);
|
||
_grayscale = _image.IsGrayscale;
|
||
if (_grayscale)
|
||
{
|
||
_windowWidth = _image.WindowWidth;
|
||
_windowCenter = _image.WindowCenter;
|
||
}
|
||
_frame = 0;
|
||
Invoke(new WaitCallback(SizeForImage), _image);
|
||
Invoke(new WaitCallback(DisplayImage), _image);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
OnException(ex);
|
||
}
|
||
});
|
||
}
|
||
else
|
||
{
|
||
if (_file != null)
|
||
{
|
||
if (_file.File.Exists)
|
||
{
|
||
_file = null;
|
||
}
|
||
}
|
||
Bitmap bitmap = new Bitmap(openFileDialog1.FileName);
|
||
str = PublicClass.Jpeg2String(bitmap);
|
||
pbDisplay.Image = bitmap;
|
||
}
|
||
|
||
string Url = "api/service/T_Service_Image/UploadFileForFileStream";
|
||
List<T_Service_ImageDTO> list = new List<T_Service_ImageDTO>();
|
||
T_Service_ImageDTO model = new T_Service_ImageDTO();
|
||
model.GUID = Guid.NewGuid().ToString();
|
||
model.CreateTime = PublicClass.DateTimeNow();
|
||
model.FileType = (int)Enumerate.ServiceImageFileType.CT检查;
|
||
model.PatientGuid = Cur_PatientGuid;
|
||
model.DeleteFlag = 0;
|
||
model.SystemModuleID = Convert.ToInt64(Cur_SystemModuleID);
|
||
model.FilePath = str;
|
||
model.FileName = file.Name;
|
||
list.Add(model);
|
||
ClientFactory<T_Service_ImageDTO> httpClient = new HttpClientFactory<T_Service_ImageDTO>();
|
||
Client<T_Service_ImageDTO> client = httpClient.VisitFactory();
|
||
if (client.Post(Url, list).Success)
|
||
{
|
||
XtraMessageBox.Show("上传影像报告成功!请在左侧报告显示区查看。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
BindList();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
XtraMessageBox.Show("打开文件出错,请重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
PublicClass.WriteErrorLog(this.Text, "上传影像图片:\r\n" + ex);
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog(this.Text, "上传影像图片:\r\n" + ex);
|
||
}
|
||
}
|
||
|
||
#region 影像图片显示
|
||
|
||
private delegate void ExceptionHandler(Exception e);
|
||
|
||
protected void OnException(Exception e)
|
||
{
|
||
if (_image == null) return;
|
||
if (InvokeRequired)
|
||
{
|
||
BeginInvoke(new ExceptionHandler(OnException), e);
|
||
return;
|
||
}
|
||
|
||
MessageBox.Show(this, (e.InnerException ?? e).Message, "Image Render Exception", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
Close();
|
||
}
|
||
|
||
protected void DisplayImage(object state)
|
||
{
|
||
if (_image == null) return;
|
||
try
|
||
{
|
||
var image = (DicomImage)state;
|
||
|
||
_previous = pbDisplay.Image;
|
||
pbDisplay.Image = null;
|
||
//_current = image.RenderImage(_frame).AsClonedBitmap();
|
||
_current = (Image)image.RenderImage(_frame).Clone();
|
||
pbDisplay.Image = _current;
|
||
|
||
Text = _grayscale
|
||
? $"DICOM Image Display [scale: {Math.Round(image.Scale, 1)}, wc: {image.WindowCenter}, ww: {image.WindowWidth}]"
|
||
: $"DICOM Image Display [scale: {Math.Round(image.Scale, 1)}]";
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
OnException(e);
|
||
}
|
||
}
|
||
|
||
protected void SizeForImage(object state)
|
||
{
|
||
if (_image == null) return;
|
||
var image = (DicomImage)state;
|
||
|
||
var max = SystemInformation.WorkingArea.Size;
|
||
|
||
var maxW = max.Width - (Width - pbDisplay.Width);
|
||
var maxH = max.Height - (Height - pbDisplay.Height);
|
||
|
||
if (image.Width > maxW || image.Height > maxH) image.Scale = Math.Min((double)maxW / image.Width, (double)maxH / image.Height);
|
||
else image.Scale = 1.0;
|
||
|
||
Width = (int)(image.Width * image.Scale) + (Width - pbDisplay.Width);
|
||
Height = (int)(image.Height * image.Scale) + (Height - pbDisplay.Height);
|
||
|
||
if (Width >= (max.Width * 0.99) || Height >= (max.Height * 0.99)) CenterToScreen(); // center very large images on the screen
|
||
else
|
||
{
|
||
CenterToParent();
|
||
if (Bottom > max.Height) Top -= Bottom - max.Height;
|
||
if (Top < 0) Top = 0;
|
||
if (Right > max.Width) Left -= Right - max.Width;
|
||
if (Left < 0) Left = 0;
|
||
}
|
||
}
|
||
|
||
private bool _dragging = false;
|
||
|
||
private Point _lastPosition = Point.Empty;
|
||
|
||
private void OnMouseDown(object sender, MouseEventArgs e)
|
||
{
|
||
if (!_grayscale) return;
|
||
|
||
_lastPosition = e.Location;
|
||
_dragging = true;
|
||
}
|
||
|
||
private void OnMouseUp(object sender, MouseEventArgs e)
|
||
{
|
||
_dragging = false;
|
||
}
|
||
|
||
private void OnMouseLeave(object sender, EventArgs e)
|
||
{
|
||
_dragging = false;
|
||
}
|
||
|
||
private void OnMouseMove(object sender, MouseEventArgs e)
|
||
{
|
||
if (!_dragging) return;
|
||
if (_image == null) return;
|
||
_image.WindowWidth += e.X - _lastPosition.X;
|
||
_image.WindowCenter += e.Y - _lastPosition.Y;
|
||
|
||
_lastPosition = e.Location;
|
||
|
||
DisplayImage(_image);
|
||
}
|
||
|
||
private void OnMouseDoubleClick(object sender, MouseEventArgs e)
|
||
{
|
||
if (_image == null) return;
|
||
if (e.Button == MouseButtons.Left)
|
||
{
|
||
_image.WindowCenter = _windowCenter;
|
||
_image.WindowWidth = _windowWidth;
|
||
}
|
||
|
||
DisplayImage(_image);
|
||
}
|
||
|
||
private void OnKeyUp(object sender, KeyEventArgs e)
|
||
{
|
||
if (_image == null) return;
|
||
if (e.KeyCode == Keys.Right)
|
||
{
|
||
_frame++;
|
||
if (_frame >= _image.NumberOfFrames) _frame--;
|
||
DisplayImage(_image);
|
||
return;
|
||
}
|
||
|
||
if (e.KeyCode == Keys.Left)
|
||
{
|
||
_frame--;
|
||
if (_frame < 0) _frame++;
|
||
DisplayImage(_image);
|
||
return;
|
||
}
|
||
|
||
if (e.KeyCode == Keys.O)
|
||
{
|
||
_image.ShowOverlays = !_image.ShowOverlays;
|
||
DisplayImage(_image);
|
||
return;
|
||
}
|
||
|
||
if (!_image.IsGrayscale) return;
|
||
GrayscaleRenderOptions options = null;
|
||
|
||
if (e.KeyCode == Keys.D0) options = GrayscaleRenderOptions.FromDataset(_file.Dataset);
|
||
else if (e.KeyCode == Keys.D1) options = GrayscaleRenderOptions.FromWindowLevel(_file.Dataset);
|
||
else if (e.KeyCode == Keys.D2) options = GrayscaleRenderOptions.FromImagePixelValueTags(_file.Dataset);
|
||
else if (e.KeyCode == Keys.D3) options = GrayscaleRenderOptions.FromMinMax(_file.Dataset);
|
||
else if (e.KeyCode == Keys.D4) options = GrayscaleRenderOptions.FromBitRange(_file.Dataset);
|
||
else if (e.KeyCode == Keys.D5) options = GrayscaleRenderOptions.FromHistogram(_file.Dataset, 90);
|
||
|
||
if (options != null)
|
||
{
|
||
_image.WindowWidth = options.WindowWidth;
|
||
_image.WindowCenter = options.WindowCenter;
|
||
|
||
DisplayImage(_image);
|
||
}
|
||
}
|
||
|
||
private void OnClientSizeChanged(object sender, EventArgs e)
|
||
{
|
||
if (_image == null) return;
|
||
var image = _image;
|
||
if (image == null || pbDisplay.Image == null) return;
|
||
|
||
if (WindowState == FormWindowState.Normal)
|
||
{
|
||
if (pbDisplay.Width > pbDisplay.Height)
|
||
{
|
||
if (image.Width > image.Height) image.Scale = (double)pbDisplay.Height / image.Height;
|
||
else image.Scale = (double)pbDisplay.Width / image.Width;
|
||
}
|
||
else
|
||
{
|
||
if (image.Width > image.Height) image.Scale = (double)pbDisplay.Width / image.Width;
|
||
else image.Scale = (double)pbDisplay.Height / image.Height;
|
||
}
|
||
|
||
// scale viewing window to match rescaled image size
|
||
Width = (int)(image.Width * image.Scale) + (Width - pbDisplay.Width);
|
||
Height = (int)(image.Height * image.Scale) + (Height - pbDisplay.Height);
|
||
}
|
||
|
||
if (WindowState == FormWindowState.Maximized)
|
||
{
|
||
image.Scale = Math.Min(
|
||
(double)pbDisplay.Width / image.Width,
|
||
(double)pbDisplay.Height / image.Height);
|
||
}
|
||
|
||
DisplayImage(image);
|
||
}
|
||
|
||
#endregion
|
||
|
||
private void Form_ImageInformation_SizeChanged(object sender, EventArgs e)
|
||
{
|
||
//autoSizeForm.controlAutoSize(this);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上一张
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void simpleButton_On_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataTable != null && dataTable.Rows.Count > 0)
|
||
{
|
||
if (index == 0)
|
||
{
|
||
labelControl_Info.Text = "当前已经已是第一份报告文件!";
|
||
}
|
||
else
|
||
{
|
||
index--;
|
||
if (dataTable.Rows.Count >= index)
|
||
{
|
||
labelControl_Info.Text = "";
|
||
groupControl_Show.Text = string.Format("报告显示区【共{0}张,当前第{1}张】", dataTable.Rows.Count, index + 1);
|
||
MemoryStream ms = new MemoryStream(Convert.FromBase64String(dataTable.Rows[index]["FileImage"].ToString()));
|
||
SelectImage(ms, dataTable.Rows[index]["FileName"] + "");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下一张
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void simpleButton_Next_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataTable != null && dataTable.Rows.Count > 0)
|
||
{
|
||
if (index + 1 == dataTable.Rows.Count)
|
||
{
|
||
labelControl_Info.Text = "当前已经已是最后一份报告文件!";
|
||
}
|
||
else
|
||
{
|
||
index++;
|
||
if (index < dataTable.Rows.Count)
|
||
{
|
||
labelControl_Info.Text = "";
|
||
groupControl_Show.Text = string.Format("报告显示区【共{0}张,当前第{1}张】", dataTable.Rows.Count, index + 1);
|
||
MemoryStream ms = new MemoryStream(Convert.FromBase64String(dataTable.Rows[index]["FileImage"].ToString()));
|
||
SelectImage(ms, dataTable.Rows[index]["FileName"] + "");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找图片
|
||
/// </summary>
|
||
/// <param name="ms"></param>
|
||
/// <param name="fileName"></param>
|
||
private void SelectImage(MemoryStream ms, string fileName)
|
||
{
|
||
try
|
||
{
|
||
#region 查找图片
|
||
_image = null;
|
||
string[] list = fileName.Split('.');
|
||
bool flag = false;
|
||
if (list.Length >= 2)
|
||
{
|
||
if (list[1] != ".dcm")
|
||
{
|
||
Image img = Image.FromStream(ms);
|
||
pbDisplay.Image = img;
|
||
}
|
||
}
|
||
if (list.Length > 1)
|
||
{
|
||
if (list[1] == ".dcm")
|
||
{
|
||
flag = true;
|
||
}
|
||
}
|
||
if (list.Length == 1)
|
||
{
|
||
flag = true;
|
||
}
|
||
if (flag)
|
||
{
|
||
DicomFile dcmFile = DicomFile.Open(ms);
|
||
ThreadPool.QueueUserWorkItem(
|
||
delegate
|
||
{
|
||
try
|
||
{
|
||
_image = new DicomImage(dcmFile.Dataset);
|
||
_grayscale = _image.IsGrayscale;
|
||
if (_grayscale)
|
||
{
|
||
_windowWidth = _image.WindowWidth;
|
||
_windowCenter = _image.WindowCenter;
|
||
}
|
||
_frame = 0;
|
||
Invoke(new WaitCallback(SizeForImage), _image);
|
||
Invoke(new WaitCallback(DisplayImage), _image);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
OnException(ex);
|
||
}
|
||
});
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog(this.Text, "查找图片:\r\n" + ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除当前报告
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void simpleButton_Delete_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
#region 删除当前报告
|
||
if (dataTable != null && dataTable.Rows.Count > 0)
|
||
{
|
||
long id = PublicClass.ToInt64(dataTable.Rows[index]["ID"], -1);
|
||
if (id > 0)
|
||
{
|
||
List<T_Service_ImageDTO> list = new List<T_Service_ImageDTO>();
|
||
T_Service_ImageDTO model = new T_Service_ImageDTO();
|
||
model.ID = id;
|
||
model.GUID = Guid.NewGuid().ToString();
|
||
model.CreateTime = PublicClass.DateTimeNow();
|
||
list.Add(model);
|
||
|
||
ClientFactory<T_Service_ImageDTO> httpClient = new HttpClientFactory<T_Service_ImageDTO>();
|
||
Client<T_Service_ImageDTO> client = httpClient.VisitFactory();
|
||
|
||
string Url = "api/service/T_Service_Image/LogicalDelete";
|
||
if (client.Post(Url, list).Success)
|
||
{
|
||
XtraMessageBox.Show("删除当前报告文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
BindList();
|
||
}
|
||
else
|
||
{
|
||
XtraMessageBox.Show("删除当前报告文件失败!请重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
PublicClass.WriteErrorLog(this.Text, "删除当前报告:\r\n" + ex);
|
||
}
|
||
}
|
||
}
|
||
}
|