How to implement a Magnifying Glass in COM version of DicomObjects

A magnifying glass has been implemented and included in any of our sample viewer programs on the examples web page.

Following is VB.NET code showing you how to make a Magnifying Glass using the COM version of DicomObjects.

Please NOTE, in order to get it working, Viewer's Container's (Normally a Windows Form) ScaleMode needs to be set to Pixel, and Viewer's UseScrollBars property needs to be set to false.

The Magnifying glass (refered to as ZoomImage in the following code) is initialised through the DicomViewer Mouse Down event

 ZoomHeight = 100  ' The Height of the Magnifying Glass
 ZoomWidth  = 100  ' The Width of the Magnifying Glass
If (Viewer.get_ImageIndex(e.x, e.y) <> 0) Then BaseImage = Viewer.Images(Viewer.get_ImageIndex(e.x, e.y))
Viewer.AutoDisplay = False Viewer.Images.Add(BaseImage) Viewer.AutoDisplay = True
ZoomImage = Viewer.Images(Viewer.Images.Count) ' Last added image ZoomImage.StretchToFit = False ZoomImage.Zoom = BaseImage.ActualZoom * 2 ' Can use any value you like here
Zooming = True ZoomScrollX = BaseImage.ScrollX ZoomScrollY = BaseImage.ScrollY
Viewer.UseScrollBars = False SetMagnifier(e.x, e.y) End If

The ZoomImage's size and position are calculated in the following SetMagnifier routine

Private Sub SetMagnifier(ByVal x As Integer, ByVal y As Integer)
 Dim X1, Y1 As Integer
 Dim t as Integer
If (Viewer.get_ImageIndex(x, y) <> 0) Then X1 = Viewer.ImageXPosition(x, y) Y1 = Viewer.ImageYPosition(x, y)
' for rotataed/flipped images If (ZoomImage.FlipState And doFlipHorizontal) > 0 Then X1 = ZoomImage.SizeX - X1 End If
If (ZoomImage.FlipState And doFlipVertical) > 0 Then Y1 = ZoomImage.SizeY - Y1 End If
If ZoomImage.RotateState = doRotateLeft Then t = Y1 Y1 = ZoomImage.SizeX - X1 X1 = t End If
If ZoomImage.RotateState = doRotateRight Then t = X1 X1 = ZoomImage.SizeY - Y1 Y1 = t End If
If ZoomImage.RotateState = doRotate180 Then X1 = ZoomImage.SizeX - X1 Y1 = ZoomImage.SizeY - Y1 End If
ZoomImage.ScrollX = Int(X1 * ZoomImage.ActualZoom - ZoomWidth / 2) ZoomImage.ScrollY = Int(Y1 * ZoomImage.ActualZoom - ZoomHeight / 2)
'now move the magnify viewer within the main viewer
ZoomImage.Left = Int(x - ZoomWidth / 2) * 1000 / Viewer.Width ZoomImage.Top = Int(y - ZoomHeight / 2) * 1000 / Viewer.Size.Height ZoomImage.Right = Int(x + ZoomWidth / 2) * 1000 / Viewer.Width ZoomImage.Bottom = Int(y + ZoomHeight / 2) * 1000 / Viewer.Height End If End Sub

When the mouse cursor is moved the ZoomImage needs to be repositioned, the DicomViewer Mouse move event simply calls the SetMagnifier routine and passes to it the new Mouse x,y coordinates expressed as a system.Drawing.Point

If Zooming Then
   SetMagnifier(e.x, e.y)
End If

Finally when the mouse button is released the ZoomImage is removed from the DicomViewer in the MouseUpEvent

If Zooming Then ' Magnifier
   Zooming = False
   Viewer.Images.Remove(Viewer.Images.Count)
   Viewer.UseScrollBars = True
   Viewer.Refresh()
   BaseImage.ScrollX = ZoomScrollX
   BaseImage.ScrollY = ZoomScrollY
End If

There is slight difference in COM and .NET version of DicomObjects when you come to the Zoom and Scroll properties as everything is encapsulated in the Matrix property in the .NET version. Follow the link for more inforamation about How_to_implement_a_Magnifying_Glass_in_.NET_version_of_DicomObjects

User login