Private Class MedicalViewerSeriesManagerFrom : Inherits Form
Private _studyElement As DicomElement
Private _seriesElement As DicomElement
Private _seriesManager As MedicalViewerSeriesManager
Private _imageDataList As List(Of MedicalViewerImageData)
Private doubleArray As Double()
Private patientElement As DicomElement
Private referenceUID As String
Private imageElement As DicomElement
Private _medicalViewer As MedicalViewer
Public Sub New()
End Sub
Private Sub MedicalViewerSeriesManagerFrom_SizeChanged(ByVal sender As Object, ByVal e As EventArgs)
_medicalViewer.Size = New Size(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
End Sub
Public Sub New(ByVal output As MedicalViewerSeriesManager)
Dim _codecs As RasterCodecs = New RasterCodecs()
AddHandler SizeChanged, AddressOf MedicalViewerSeriesManagerFrom_SizeChanged
' Create the medical viewer and adjust the size and the location.
_medicalViewer = New MedicalViewer(1, 2)
_medicalViewer.Location = New Point(0, 0)
_medicalViewer.Size = New Size(Me.ClientRectangle.Right, Me.ClientRectangle.Bottom)
_seriesManager = output
Dim cell As MedicalViewerMultiCell = New MedicalViewerMultiCell(Nothing, True, 1, 1)
Dim index As Integer
Dim count As Integer = output.Stacks(0).Items.Count
Dim codecsInformation As CodecsImageInfo
Dim imageInformation As MedicalViewerImageInformation() = New MedicalViewerImageInformation(count - 1) {}
index = 0
Do While index < count
codecsInformation = _codecs.GetInformation(CStr(output.Stacks(0).Items(index).Data), True)
imageInformation(index) = New MedicalViewerImageInformation()
imageInformation(index).ImageHeight = codecsInformation.Width
imageInformation(index).ImageWidth = codecsInformation.Width
imageInformation(index).XResolution = codecsInformation.XResolution
imageInformation(index).YResolution = codecsInformation.YResolution
index += 1
Loop
AddHandler cell.FramesRequested, AddressOf cell_FramesRequested
AddHandler FormClosing, AddressOf MedicalViewerSeriesManagerFrom_FormClosing
cell.EnableLowMemoryUsage(2, count, imageInformation)
_medicalViewer.Cells.Add(cell)
cell.PixelSpacing = output.Stacks(0).PixelSpacing
cell.ImageOrientation = output.Stacks(0).Items(0).ImageOrientationArray
cell.FrameOfReferenceUID = output.Stacks(0).Items(0).FrameOfReferenceUID
index = 0
Do While index < count
cell.SetImagePosition(index, output.Stacks(0).Items(index).ImagePosition, (index = count - 1))
index += 1
Loop
' add some action that will be used to change the properties of the images inside the control.
cell.AddAction(MedicalViewerActionType.WindowLevel)
cell.AddAction(MedicalViewerActionType.Alpha)
cell.AddAction(MedicalViewerActionType.Offset)
cell.AddAction(MedicalViewerActionType.Stack)
' assign the added actions to a mouse button, meaning that when the user click and drag the mouse button, the associated action will be activated.
cell.SetAction(MedicalViewerActionType.WindowLevel, MedicalViewerMouseButtons.Left, MedicalViewerActionFlags.Active Or MedicalViewerActionFlags.RealTime)
cell.SetAction(MedicalViewerActionType.Alpha, MedicalViewerMouseButtons.Middle, MedicalViewerActionFlags.Active Or MedicalViewerActionFlags.RealTime)
cell.SetAction(MedicalViewerActionType.Offset, MedicalViewerMouseButtons.Right, MedicalViewerActionFlags.Active Or MedicalViewerActionFlags.RealTime)
cell.SetAction(MedicalViewerActionType.Stack, MedicalViewerMouseButtons.Wheel, MedicalViewerActionFlags.Active Or MedicalViewerActionFlags.RealTime)
' adjust some properties to the cell and add some tags.
_medicalViewer.Cells(0).SetTag(2, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, "EX. ID 230-36-5448")
_medicalViewer.Cells(0).SetTag(4, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Frame)
_medicalViewer.Cells(0).SetTag(6, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Scale)
_medicalViewer.Cells(0).SetTag(2, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.WindowLevelData)
_medicalViewer.Cells(0).SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.FieldOfView)
_medicalViewer.Cells(0).SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.UserData, "Echo number = " & output.Stacks(0).EchoNumber.ToString())
Controls.Add(_medicalViewer)
_medicalViewer.Dock = DockStyle.Fill
End Sub
Private Sub cell_FramesRequested(ByVal sender As Object, ByVal e As MedicalViewerRequestedFramesInformationEventArgs)
Dim cell As MedicalViewerMultiCell = CType(sender, MedicalViewerMultiCell)
Dim _codecs As RasterCodecs = New RasterCodecs()
Dim i As Integer
Dim image As RasterImage
Dim fileName As String
If e.RequestedFramesIndexes.Length > 0 Then
fileName = CStr(_seriesManager.Stacks(0).Items(e.RequestedFramesIndexes(0)).Data)
image = _codecs.Load(fileName)
Else
Return
End If
i = 1
Do While i < e.RequestedFramesIndexes.Length
fileName = CStr(_seriesManager.Stacks(0).Items(e.RequestedFramesIndexes(i)).Data)
image.AddPage(_codecs.Load(fileName))
i += 1
Loop
cell.SetRequestedImage(image, e.RequestedFramesIndexes, MedicalViewerSetImageOptions.Insert)
End Sub
Private Sub MedicalViewerSeriesManagerFrom_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
End Sub
' Find the study using the Study instance UID, and return it's DicomElement if the study is found
Private Function FindStudy(ByVal ds As DicomDataSet, ByVal studyInstanceUID As String) As DicomElement
' get the parent element.
Dim patientElement As DicomElement = ds.GetFirstKey(Nothing, True)
Dim studyElement As DicomElement = Nothing
Dim studyInformationElement As DicomElement = Nothing
Dim studyID As String
studyElement = ds.GetChildKey(patientElement)
studyElement = ds.GetChildElement(studyElement, True)
Do While Not studyElement Is Nothing
studyInformationElement = ds.FindFirstElement(studyElement, DicomTag.StudyInstanceUID, True)
If Not studyInformationElement Is Nothing Then
studyID = ds.GetConvertValue(studyInformationElement)
If studyID = studyInstanceUID Then
Return studyInformationElement
End If
End If
studyElement = ds.GetNextKey(studyElement, True)
studyElement = ds.GetChildElement(studyElement, True)
Loop
Return Nothing
End Function
' Find the series using the Series instance UID, and return it's DicomElement if the series is found
Private Function FindSeries(ByVal ds As DicomDataSet, ByVal studyElement As DicomElement, ByVal seriesInstanceUID As String) As DicomElement
Dim seriesElement As DicomElement = Nothing
Dim seriesInformationElement As DicomElement = Nothing
Dim seriesID As String
seriesElement = ds.GetChildKey(studyElement)
seriesElement = ds.GetChildElement(seriesElement, True)
Do While Not seriesElement Is Nothing
seriesInformationElement = ds.FindFirstElement(seriesElement, DicomTag.SeriesInstanceUID, True)
If Not seriesInformationElement Is Nothing Then
seriesID = ds.GetConvertValue(seriesInformationElement)
If seriesID = seriesInstanceUID Then
Return seriesInformationElement
End If
End If
seriesElement = ds.GetNextKey(seriesElement, True)
seriesElement = ds.GetChildElement(seriesElement, True)
Loop
Return Nothing
End Function
' return the first frame file name of the series.
Private Function GetFirstImageName(ByVal ds As DicomDataSet, ByVal seriesElement As DicomElement, ByVal directoryPath As String, <System.Runtime.InteropServices.Out()> ByRef imageElement As DicomElement) As String
Dim imageIDElement As DicomElement = Nothing
imageElement = ds.GetChildKey(seriesElement)
imageElement = ds.GetChildElement(imageElement, True)
Do While Not imageElement Is Nothing
imageIDElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, True)
If Not imageIDElement Is Nothing Then
Return directoryPath & "\" & ds.GetConvertValue(imageIDElement)
End If
Loop
Return ""
End Function
' return the next frame file name of the series.
Private Function GetNextImageName(ByVal ds As DicomDataSet, ByVal directoryPath As String, ByRef imageElement As DicomElement) As String
Dim nextImageElement As DicomElement = Nothing
imageElement = ds.GetNextKey(imageElement, True)
imageElement = ds.GetChildElement(imageElement, True)
Do While Not imageElement Is Nothing
nextImageElement = ds.FindFirstElement(imageElement, DicomTag.ReferencedFileID, True)
If Not imageElement Is Nothing Then
Dim echoElement As DicomElement = ds.FindFirstElement(imageElement, DicomTag.EchoNumber, True)
Return directoryPath & "\" & ds.GetConvertValue(nextImageElement)
End If
Loop
Return ""
End Function
Private Function AddImageToImageArray(ByVal ds As DicomDataSet, ByVal index As Integer, ByVal imagePath As String, <System.Runtime.InteropServices.Out()> ByRef echoNumber As Integer) As Boolean
echoNumber = -1
Dim imageData As MedicalViewerImageData = New MedicalViewerImageData()
patientElement = ds.FindFirstElement(Nothing, DicomTag.ImagePositionPatient, True)
doubleArray = ds.GetDoubleValue(patientElement, 0, 3)
imageData.ImagePosition = Point3D.FromDoubleArray(doubleArray)
imageData.Data = imagePath
imageData.EchoNumber = echoNumber
patientElement = ds.FindFirstElement(Nothing, DicomTag.FrameOfReferenceUID, True)
referenceUID = ds.GetConvertValue(patientElement)
imageData.FrameOfReferenceUID = referenceUID
patientElement = ds.FindFirstElement(Nothing, DicomTag.ImageOrientationPatient, True)
imageData.ImageOrientation = ds.GetConvertValue(patientElement)
patientElement = ds.FindFirstElement(Nothing, DicomTag.PixelSpacing, True)
doubleArray = ds.GetDoubleValue(patientElement, 0, 2)
imageData.PixelSpacing = New Point2D(CSng(doubleArray(0)), CSng(doubleArray(1)))
_imageDataList.Add(imageData)
Return True
End Function
Public Function LoadJamesHead() As MedicalViewerSeriesManager
DicomEngine.Startup()
Dim ds As DicomDataSet = New DicomDataSet()
'INSTANT VB NOTE: Embedded comments are not maintained by Instant VB
'ORIGINAL LINE: ds.Load(/*LeadtoolsExamples.Common.ImagesPath.Path*/@"\\pc202\readwritefolder\DICOM Dataset\DICOM Volume datasets" + "\\Miller James-CT Head full Study\\DICOMDIR", DicomDataSetLoadFlags.None);
ds.Load(Path.Combine(LEAD_VARS.ImagesDir, "Miller James-CT Head full Study\DICOMDIR"), DicomDataSetLoadFlags.None)
'INSTANT VB NOTE: Embedded comments are not maintained by Instant VB
'ORIGINAL LINE: string directoryPath = /*LeadtoolsExamples.Common.ImagesPath.Path*/ @"\\pc202\readwritefolder\DICOM Dataset\DICOM Volume datasets" + "\\Miller James-CT Head full Study\\";
Dim directoryPath As String = Path.Combine(LEAD_VARS.ImagesDir, "\Miller James-CT Head full Study\")
Dim studyInstanceUID As String = "1.3.12.2.1107.5.1.4.50772.30000009122208074910900000022"
Dim seriesInstanceUID As String = "1.3.12.2.1107.5.1.4.50772.30000009122208215356200001997"
_studyElement = FindStudy(ds, studyInstanceUID)
_seriesElement = FindSeries(ds, _studyElement, seriesInstanceUID)
Dim count As Integer = 256
_seriesManager = New MedicalViewerSeriesManager()
_imageDataList = New List(Of MedicalViewerImageData)()
Dim dicomDataSet As DicomDataSet
Dim imageIndex As Integer
Dim imagePath As String
Dim echoNumber As Integer = 0
imagePath = GetFirstImageName(ds, _seriesElement, directoryPath, imageElement)
imageIndex = 0
Do While imageIndex < count
Try
dicomDataSet = New DicomDataSet()
dicomDataSet.Load(imagePath, DicomDataSetLoadFlags.None)
AddImageToImageArray(dicomDataSet, imageIndex, imagePath, echoNumber)
dicomDataSet.Dispose()
imagePath = GetNextImageName(ds, directoryPath, imageElement)
Catch exception As System.Exception
System.Diagnostics.Debug.Assert(False, exception.Message)
Throw
End Try
imageIndex += 1
Loop
_seriesManager.Sort(_imageDataList)
DicomEngine.Shutdown()
Return _seriesManager
End Function
End Class
Private Function GetMedicalControlForReverseExample() As MedicalViewerSeriesManagerFrom
Dim form As MedicalViewerSeriesManagerFrom = New MedicalViewerSeriesManagerFrom()
Dim output As MedicalViewerSeriesManager = form.LoadJamesHead()
Return New MedicalViewerSeriesManagerFrom(output)
End Function
'
Public Sub MedicalViewerSeriesManagerLoadingExample()
Dim myForm As MedicalViewerSeriesManagerFrom = GetMedicalControlForReverseExample()
myForm.ShowDialog()
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class |