Implement Centered Zoom in DicomObjects.NET

Implementing the Centered Zoom function in .NET version of DicomObjects is lot simpler and neater than the COM version (Sample VB6 program which demonstrates how to do this in DicomObjects.COM can be downloaded from here).

Below is the code, if you want the image to be centered zoom, all you need to do is replace LastXforZoom and LastYforZoom by Viewer.Width/2 and Viewer.Height/2:

Viewer's MouseDown Event:

Private Sub Viewer_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Viewer.MouseDown
'Get the last mouse position for the zoom operation
  LastXforZoom = e.X
  LastYforZoom = e.Y
End Sub

Scroll Bar (which changes the zoom)'s Scroll Event:

Private Sub ZoomBar_Scroll(ByVal sender As System.Object, ByVale As System.EventArgs) Handles tbZoom.Scroll
  Dim origScrollX, origScrollY As Integer
  origScrollX = Viewer.CurrentImage.Scroll.X
  origScrollY = Viewer.CurrentImage.Scroll.Y

  'Calculate the Image Coordinates of the user-clicked point
  Dim zpoint As New Point(LastXforZoom, LastYforZoom)
  Dim pts(0) As Point
  pts(0) = zpoint
  Dim mx As System.Drawing.Drawing2D.Matrix
  mx = Viewer.CurrentImage.Matrix(Viewer)
  'Change Image Zoom
  Viewer.CurrentImage.Zoom = 1 + (tbZoom.Value - 11) * 0.125

  'Apply the new Image Matrix to calculate the new Screen Coordinates
  Dim mx2 As System.Drawing.Drawing2D.Matrix
  mx2 = Viewer.CurrentImage.Matrix(Viewer)

  'Calculate the offset to modify Image's Scroll property
  Dim newScrollX, newScrollY As Integer
  newScrollX = origScrollX - (pts(0).X - LastXforZoom)
  newScrollY = origScrollY - (pts(0).Y - LastYforZoom)
  Viewer.CurrentImage.Scroll = New Point(newScrollX, newScrollY)
End Sub

Sample programs in VB and C# can be found on our DicomObjects_Sample_Programs page.