LoadJsonBeforeElementCallback Delegate

Summary

Optional delegate method that is used with the LoadJson methods.

Syntax
C#
VB
C++
public delegate bool LoadJsonBeforeElementCallback( 
   LoadJsonBeforeElementData data 
) 
Public Delegate Function LoadJsonBeforeElementCallback( _ 
   ByVal data As LoadJsonBeforeElementData _ 
) As Boolean 
public delegate bool LoadJsonBeforeElementCallback(  
   LoadJsonBeforeElementData^ data 
) 

Parameters

data
An instance of the LoadJsonBeforeElementData class containing the progress callback data.

Return Value

true to include this DICOM element in the DicomDataSet; false to exclude the element from the DicomDataSet

Remarks

This delegate is called once for each DICOM element, before the element is added to the DicomDataSet. If you pass this delegate with the LoadJson methods, you must fill in certain fields of the data For details, see the documentation for LoadJsonBeforeElementData

Example

This example will load a sample DICOM data set, then save it as an JSON file (with no binary data), and then reload the JSON file into a DicomDataSet object. The LoadJsonBeforeElementCallback callback is used to change the patient name, remove the patient birthdate, and display which elements have children.

C#
VB
using Leadtools.Dicom; 
using Leadtools.Dicom.Common; 
using Leadtools.Dicom.Common.Extensions; 
using Leadtools; 
using Leadtools.Dicom.Common.Linq.BasicDirectory; 
using Leadtools.Dicom.Common.DataTypes; 
 
public void LoadJsonCallbackExample() 
{ 
   string dicomFileNameIn = Path.Combine(LEAD_VARS.ImagesDir, "IMAGE2.dcm"); 
   string jsonFileNameOut = Path.Combine(LEAD_VARS.ImagesDir, "test.json"); 
   string dicomFileNameOut = Path.Combine(LEAD_VARS.ImagesDir, "test.dcm"); 
 
   // Initialize DICOM engine 
   DicomEngine.Startup(); 
 
   DicomDataSet ds = new DicomDataSet(); 
 
   // Load an existing DICOM file 
   ds.Load(dicomFileNameIn, DicomDataSetLoadFlags.None); 
 
   // Save as JSON to a file with no binary data 
   // For the demo, keep the json output file size small by skipping the pixel data 
   // Use the SaveJsonCallback delegate to customize the json file 
   DicomDataSetSaveJsonFlags jsonFlags = 
      DicomDataSetSaveJsonFlags.IgnoreBinaryData | 
      DicomDataSetSaveJsonFlags.TrimWhiteSpace; 
 
   ds.SaveJson(jsonFileNameOut, jsonFlags); 
 
   // Use a LoadmlCallback to read the customized json file 
   ds.LoadJson(jsonFileNameOut, DicomDataSetLoadJsonFlags.None, MyLoadJsonBeforeElementCallback, MyLoadJsonAfterElementCallback); 
 
   // Save the result -- there will be no pixel data 
   ds.Save(dicomFileNameOut, DicomDataSetSaveFlags.None); 
 
   DicomEngine.Shutdown(); 
} 
 
private static bool MyLoadJsonBeforeElementCallback(LoadJsonBeforeElementData data) 
{ 
   // Remove Patient Birth Date 
   if (data.Tag == DicomTag.PatientBirthDate) 
   { 
      return false; 
   } 
 
   // Change the patient name 
   if (data.Tag == DicomTag.PatientName) 
   { 
      data.ElementValue = "NewLast^NewFirst"; 
   } 
 
   // Write the elements to the consolee that have child elements 
   if (data.HasChildElements) 
   { 
      Console.WriteLine("Element {0} has child elements.", data.Tag); 
   } 
   return true; 
} 
 
private static void MyLoadJsonAfterElementCallback(LoadJsonAfterElementData data) 
{ 
   if (data.DicomElement.Tag == DicomTag.PixelData) 
   { 
      // here you could call one of the following to set the pixel data 
      //    data.DicomDataSet.SetBinaryValue  
      //    data.DicomDataSet.SetImage() 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools.Dicom 
Imports Leadtools.Dicom.Common 
Imports Leadtools.Dicom.Common.Extensions 
Imports Leadtools.Dicom.Common.Linq 
Imports Leadtools.Dicom.Common.Linq.BasicDirectory 
Imports Leadtools 
Imports Leadtools.Dicom.Common.DataTypes 
 
Public Sub LoadJsonCallbackExample() 
   Dim dicomFileNameIn As String = Path.Combine(LEAD_VARS.ImagesDir, "IMAGE2.dcm") 
   Dim jsonFileNameOut As String = Path.Combine(LEAD_VARS.ImagesDir, "test.json") 
   Dim dicomFileNameOut As String = Path.Combine(LEAD_VARS.ImagesDir, "test.dcm") 
 
   ' Initialize DICOM engine 
   DicomEngine.Startup() 
 
   Dim ds As New DicomDataSet() 
 
   ' Load an existing DICOM file 
   ds.Load(dicomFileNameIn, DicomDataSetLoadFlags.None) 
 
   ' Save as JSON to a file with no binary data 
   ' For the demo, keep the json output file size small by skipping the pixel data 
   ' Use the SaveJsonCallback delegate to customize the json file 
   Dim jsonFlags As DicomDataSetSaveJsonFlags = DicomDataSetSaveJsonFlags.IgnoreBinaryData Or DicomDataSetSaveJsonFlags.TrimWhiteSpace 
 
   ds.SaveJson(jsonFileNameOut, jsonFlags) 
 
   ' Use a LoadmlCallback to read the customized json file 
   ds.LoadJson(jsonFileNameOut, DicomDataSetLoadJsonFlags.None, AddressOf MyLoadJsonBeforeElementCallback, AddressOf MyLoadJsonAfterElementCallback) 
 
   ' Save the result -- there will be no pixel data 
   ds.Save(dicomFileNameOut, DicomDataSetSaveFlags.None) 
 
   DicomEngine.Shutdown() 
End Sub 
 
Private Shared Function MyLoadJsonBeforeElementCallback(ByVal data As LoadJsonBeforeElementData) As Boolean 
   ' Remove Patient Birth Date 
   If data.Tag = DicomTag.PatientBirthDate Then 
      Return False 
   End If 
 
   ' Change the patient name 
   If data.Tag = DicomTag.PatientName Then 
      data.ElementValue = "NewLast^NewFirst" 
   End If 
 
   ' Write the elements to the consolee that have child elements 
   If data.HasChildElements Then 
      Console.WriteLine("Element {0} has child elements.", data.Tag) 
   End If 
   Return True 
End Function 
 
Private Shared Sub MyLoadJsonAfterElementCallback(ByVal data As LoadJsonAfterElementData) 
   If data.DicomElement.Tag = DicomTag.PixelData Then 
      ' here you could call one of the following to set the pixel data 
      '    data.DicomDataSet.SetBinaryValue  
      '    data.DicomDataSet.SetImage() 
   End If 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 
Requirements

Target Platforms

Help Version 20.0.2019.11.25
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2019 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Dicom.Common Assembly