diff --git a/PEIS/Model/ReportModel.cs b/PEIS/Model/ReportModel.cs index ba33228..8e35b5a 100644 --- a/PEIS/Model/ReportModel.cs +++ b/PEIS/Model/ReportModel.cs @@ -198,13 +198,37 @@ namespace PEIS.Model { using (MemoryStream memoryStream = new MemoryStream(heart[0].ReportImage)) { - Image bitmap = Image.FromStream(memoryStream); - using (MemoryStream stream = new MemoryStream()) + if (ReportHelper.IsPdf(heart[0].ReportImage)) { - bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); - bitmap.Save(stream, ImageFormat.Jpeg); - var img = stream.ToArray(); - pacs.Add(new Report { ReportImage = img }); + // PDF格式 + using (PdfDocument pdfDocument = PdfDocument.Load(memoryStream)) + { + for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++) + { + using (Image bitmap = pdfDocument.Render(pageIndex, 2480, 3508, false)) + { + using (MemoryStream stream = new MemoryStream()) + { + bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); + bitmap.Save(stream, ImageFormat.Jpeg); + var img = stream.ToArray(); + pacs.Add(new Report { ReportImage = img }); + } + } + } + } + } + else + { + // 图片格式 + Image bitmap = Image.FromStream(memoryStream); + using (MemoryStream stream = new MemoryStream()) + { + bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); + bitmap.Save(stream, ImageFormat.Jpeg); + var img = stream.ToArray(); + pacs.Add(new Report { ReportImage = img }); + } } } diff --git a/PEIS/Utils/ReportHelper.cs b/PEIS/Utils/ReportHelper.cs index 330de0f..aa810a5 100644 --- a/PEIS/Utils/ReportHelper.cs +++ b/PEIS/Utils/ReportHelper.cs @@ -32,37 +32,7 @@ using FastReport.Export.Pdf; namespace PEIS.Utils { public static class ReportHelper - { - - // 逆时针旋转图片的函数 - public static Image RotateImage(Image img) - { - // 旋转角度为负90度 - float angle = -90f; - - // 计算旋转后的图像的矩形范围 - RectangleF bounds = new RectangleF(0, 0, img.Height, img.Width); - - // 创建旋转后的位图 - Bitmap rotatedImage = new Bitmap((int)bounds.Width, (int)bounds.Height); - rotatedImage.SetResolution(img.HorizontalResolution, img.VerticalResolution); - - using (Graphics g = Graphics.FromImage(rotatedImage)) - { - // 设置插值模式以改善旋转后图像的质量 - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - // 设置旋转中心点 - g.TranslateTransform(rotatedImage.Width / 2, rotatedImage.Height / 2); - // 旋转角度 - g.RotateTransform(angle); - // 将旋转中心点恢复到原点 - g.TranslateTransform(-img.Width / 2, -img.Height / 2); - // 绘制旋转后的图片 - g.DrawImage(img, new PointF(0, 0)); - } - - return rotatedImage; - } + { /// /// PDF to Image @@ -73,34 +43,40 @@ namespace PEIS.Utils { try { - // 德宏中医院 ECG 心电保存的结果为图片JPG 不是PDF - using (var ms = new MemoryStream(pdf, 0, pdf.Length)) + if (IsPdf(pdf)) { - return Image.FromStream(ms, true); - } - using (var memoryStream = new MemoryStream(pdf)) - { - using (var pdfDocument = PdfiumViewer.PdfDocument.Load(memoryStream)) + // PDF格式 + using (var memoryStream = new MemoryStream(pdf)) { - for (var pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++) + using (var pdfDocument = PdfiumViewer.PdfDocument.Load(memoryStream)) { - using (var bitmap = pdfDocument.Render(pageIndex, 2480, 3508, false)) + for (var pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++) { - using (var stream = new MemoryStream()) + using (var bitmap = pdfDocument.Render(pageIndex, 2480, 3508, false)) { - bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); - bitmap.Save(stream, ImageFormat.Jpeg); - var img = stream.ToArray(); - - using (var ms = new MemoryStream(img, 0, img.Length)) + using (var stream = new MemoryStream()) { - return Image.FromStream(ms, true); + bitmap.Save(stream, ImageFormat.Jpeg); + var img = stream.ToArray(); + + using (var ms = new MemoryStream(img, 0, img.Length)) + { + return Image.FromStream(ms, true); + } } } } } } } + else + { + // 图片格式 + using (var ms = new MemoryStream(pdf, 0, pdf.Length)) + { + return Image.FromStream(ms, true); + } + } } catch (Exception ex) { @@ -110,6 +86,28 @@ namespace PEIS.Utils return null; } + /// + /// 判断是否PDF + /// + /// + /// + public static bool IsPdf(byte[] fileBytes) + { + const string pdfSignature = "%PDF"; + if (fileBytes.Length >= pdfSignature.Length) + { + for (int i = 0; i < pdfSignature.Length; i++) + { + if (fileBytes[i] != pdfSignature[i]) + { + return false; + } + } + return true; + } + return false; + } + /// /// 获取报告单 /// diff --git a/PEIS/View/Exam/PartForm.cs b/PEIS/View/Exam/PartForm.cs index ad7e4ec..5ac6f53 100644 --- a/PEIS/View/Exam/PartForm.cs +++ b/PEIS/View/Exam/PartForm.cs @@ -1016,8 +1016,7 @@ namespace PEIS.View.Exam var img = ReportHelper.PdfToImg(item.ReportImg); if (img != null) { - var newImg = ReportHelper.RotateImage(img); - Invoke(new Action(() => { picReportExt.Image = newImg; })); + Invoke(new Action(() => { picReportExt.Image = img; picReportExt.Dock = DockStyle.Fill; picReportExt.SizeMode = PictureBoxSizeMode.Zoom; })); } return; diff --git a/PEIS/View/Exam/TotalForm.cs b/PEIS/View/Exam/TotalForm.cs index 2e837f0..2324028 100644 --- a/PEIS/View/Exam/TotalForm.cs +++ b/PEIS/View/Exam/TotalForm.cs @@ -1011,8 +1011,7 @@ namespace PEIS.View.Exam var img = ReportHelper.PdfToImg(item.ReportImg); if (img != null) { - var newImg = ReportHelper.RotateImage(img); - Invoke(new Action(() => { PictureBoxReportExt.Image = newImg; })); + Invoke(new Action(() => { PictureBoxReportExt.Image = img; PictureBoxReportExt.Dock = DockStyle.Fill; PictureBoxReportExt.SizeMode = PictureBoxSizeMode.Zoom; })); } return;