private void ProcessInstance(SeriesImage seriesImage, string userName, PrintOptions options, Dictionary <string, List <AnnContainer> > annotations) { ResetGrayscale(seriesImage.Image, options.ReduceGrayscaleTo8BitsSelected); List <DicomDataSet> ds = _Exporter.GetInstanceDatasets(new string[] { seriesImage.SOPInstanceUID }); double xDpi = 0; double yDpi = 0; if (ds.Count > 0) { AddInsUtils.GetDpi(ds[0], out xDpi, out yDpi); if (options.PatientInfo) { string name = ds[0].GetValue <string>(DicomTag.PatientName, string.Empty); string id = ds[0].GetValue <string>(DicomTag.PatientID, string.Empty); string sex = ds[0].GetValue <string>(DicomTag.PatientSex, string.Empty); string date = ds[0].GetValue <string>(DicomTag.SeriesDate, string.Empty); string icomments = ds[0].GetValue <string>(DicomTag.ImageComments, string.Empty); string fcomments = ds[0].GetValue <string>(DicomTag.FrameComments, string.Empty); string patientDOB = ds[0].GetValue <string>(DicomTag.PatientBirthDate, string.Empty); string seriesDecription = ds[0].GetValue <string>(DicomTag.SeriesDescription, string.Empty); string tag = string.Empty; if (!string.IsNullOrEmpty(name)) { tag += string.Format("Patient: {1}{0}", Environment.NewLine, name.Replace('^', ' ')); } if (!string.IsNullOrEmpty(id)) { tag += string.Format("Patient ID: {1}{0}", Environment.NewLine, id); } if (!string.IsNullOrEmpty(sex)) { tag += string.Format("Sex: {1}{0}", Environment.NewLine, sex); } if (!string.IsNullOrEmpty(date)) { tag += string.Format("Date: {1}{0}", Environment.NewLine, date); } if (!string.IsNullOrEmpty(icomments)) { tag += string.Format("Comments: {1}{0}", Environment.NewLine, icomments); } if (!string.IsNullOrEmpty(fcomments)) { tag += string.Format("Comments: {1}{0}", Environment.NewLine, fcomments); } if (options.LayoutPatientInfo) { tag = string.Empty; if (!string.IsNullOrEmpty(id)) { tag += string.Format("Patient ID: {1}{0}", Environment.NewLine, id); } if (!string.IsNullOrEmpty(name)) { tag += string.Format("Patient: {1}{0}", Environment.NewLine, name.Replace('^', ' ')); } if (!string.IsNullOrEmpty(patientDOB)) { tag += string.Format("Brith Date: {1}{0}", Environment.NewLine, patientDOB); } if (!string.IsNullOrEmpty(sex)) { tag += string.Format("Sex: {1}{0}", Environment.NewLine, sex); } if (!string.IsNullOrEmpty(date)) { tag += string.Format("Date: {1}{0}", Environment.NewLine, date); } if (!string.IsNullOrEmpty(seriesDecription)) { tag += string.Format("Description: {1}{0}", Environment.NewLine, date); } } if (!string.IsNullOrEmpty(tag)) { // adjust the text size based on the size of the output image. var imageTagged = AddTextFooter(seriesImage.Image, tag, options.LayoutPatientInfo ? Math.Max(100, seriesImage.Image.Height / 5) : 100); seriesImage.Image.Dispose(); seriesImage.Image = null; seriesImage.Image = imageTagged; } } } try { if (options.BurnAnnotations) { if (annotations.ContainsKey(seriesImage.SOPInstanceUID)) { foreach (AnnContainer container in annotations[seriesImage.SOPInstanceUID]) { container.IsVisible = true; seriesImage.Image.Burn(container, xDpi, yDpi); } } } } catch { }//ignored }
public Stream PrintLayout(string userName, string seriesInstanceUID, Layout layout, PrintOptions options, string annotationData) { DocumentWriter documentWriterInstance = null; // // Check to see if a width was provide. If not we default to 1024; // if (options.LayoutImageWidth == 0) { options.LayoutImageWidth = 1024; } SeriesImage layoutImageWithInfo = null; using (RasterImage layoutImage = AddInsUtils.CreateLayoutImage(options.LayoutImageWidth, options.WhiteBackground)) { try { options.LayoutPatientInfo = true; Dictionary <string, List <AnnContainer> > annotations = AddInsUtils.GetAnnotations(annotationData); foreach (var box in layout.Boxes) { if (null == box.referencedSOPInstanceUID) { continue; } if (0 == box.referencedSOPInstanceUID.Count) { continue; } //only the first instance List <SeriesImage> seriesImages = _Exporter.GetInstanceImages(new string[] { box.referencedSOPInstanceUID[0] }); if (null == seriesImages) { continue; } if (0 == seriesImages.Count) { continue; } // layout patient info check whether to print the patient information on each image or on the whole layout image. if (!options.LayoutPatientInfo) { //only the first image ProcessInstance(seriesImages[0], userName, options, annotations); } else { // take one of those series image to print them at the bottom of the final resultant image. if (layoutImageWithInfo == null) { layoutImageWithInfo = seriesImages[0]; } } AddInsUtils.LayoutImage(layoutImage, seriesImages[0].Image, box); } documentWriterInstance = OpenDocumentWriterInstance(options.PageWidth, options.PageHeight, 300); string fileName = Path.GetTempFileName(); documentWriterInstance.BeginDocument(fileName, DocumentFormat.Pdf); { var page = new Leadtools.Document.Writer.DocumentWriterRasterPage(); var finalImage = layoutImage; if (options.LayoutPatientInfo) { // add text to the bottom of the resultant image. layoutImageWithInfo.Image = layoutImage; ProcessInstance(layoutImageWithInfo, userName, options, null); finalImage = layoutImageWithInfo.Image; } page.Image = finalImage; documentWriterInstance.AddPage(page); } documentWriterInstance.EndDocument(); MemoryStream ms = new MemoryStream(File.ReadAllBytes(fileName)); File.Delete(fileName); ms.Position = 0; return(ms); } finally { if (null != documentWriterInstance) { documentWriterInstance.EndDocument(); } } } return(null); }