How to implement a Magnifying Glass with DicomObjects
It is possible to implement a magnifying glass tool in DicomObjects.NET.This functionality can be achieved by using a second DicomImage which is a copy of the original image (scaled, zoomed and scrolled accordingly) and overlaying onto the original image.
Because the new image is a “Clone” of the original, it shares the same underlying DataSet, and it therefore uses negligible memory and takes a tiny amount of time to create.
The mouse cursor can be used to control the position of the magnifying glass easily thanks to the DicomViewer Mouse events.
The Magnifying glass (referred to as ZoomImage in the following code) can be initialised through the DicomViewer Mouse Down event.
DicomObjects.NET
private void EndMagnifier()
{
if (MagnifierImage != null)
{
Viewer.Images.Remove(MagnifierImage);
MagnifierImage = null;
}
}
private void SetMagnifier(Point p)
{
if (ActiveImage != null)
{
if (MagnifierImage == null)
{
MagnifierImage = ActiveImage.Clone(true) as DicomImage;
MagnifierImage.StretchMode = StretchModes.NoStretch;
Viewer.Images.Add(MagnifierImage);
MagnifierImage.Zoom = DicomGlobal.Zoom(ActiveImage.Matrix(Viewer)) * 2;
MagnifierImage.Labels.Clear();
}
float MagnifierSize = 0.2F;
RectangleF Rect = new RectangleF(p.X / (float)Viewer.ClientSize.Width,
p.Y / (float)Viewer.ClientSize.Height, 0, 0);
Rect.Inflate(MagnifierSize / 2, MagnifierSize / 2);
MagnifierImage.Area = Rect;
// Work out how far to scroll the magnified image
// by doing screen ==> Image==> Screen image coordinates
// not simply done by scaling of offset, as anisometropic pixels can cause problems
var ImageCoords = ActiveImage.ScreenToImage(p, Viewer).Value;
var MagCoords = MagnifierImage.ImageToScreen(ImageCoords, Viewer).Value;
MagnifierImage.Scroll = ((PointF)p) + new SizeF((MagnifierImage.Scroll - new SizeF(MagCoords)));
}
}
private void Viewer_MouseDown(object sender, MouseEventArgs e)
{
Viewer.Focus();
SetMagnifier(e.Location);
}
private void Viewer_MouseMove(object sender, MouseEventArgs e)
{
SetMagnifier(e.Position);
}
private void Viewer_MouseUp(object sender, MouseEventArgs e)
{
EndMagnifier();
}
Download the CSharp Sample Viewer Program to try the Magnifying glass.
DicomObjects.COM
Sub SetMagnifier(ByVal X As Single, ByVal Y As Single)
Const MagnifierSize = 100
Dim x1 As Single, y1 As Single
If Not ActiveImage Is Nothing Then
If MagnifierImage Is Nothing Then
Viewer.AutoDisplay = False
Viewer.Images.Add ActiveImage
Viewer.AutoDisplay = True
Set MagnifierImage = Viewer.Images(Viewer.Images.Count)
MagnifierImage.StretchMode = NoStretch
MagnifierImage.Zoom = ActiveImage.ActualZoom * 2
MagnifierImage.Labels.Clear
End If
If Viewer.ImageIndex(X, Y) <> 0 Then
' image coordinates
x1 = Viewer.ImageXPosition(X, Y)
y1 = Viewer.ImageYPosition(X, Y)
MagnifierImage.ScrollX = x1 * MagnifierImage.ActualZoom - MagnifierSize / 2
MagnifierImage.ScrollY = y1 * MagnifierImage.ActualZoom - MagnifierSize / 2
'now move the magnify viewer within the main viewer
MagnifierImage.Left = (X - MagnifierSize / 2) * 1000 / Viewer.Width
MagnifierImage.Top = (Y - MagnifierSize / 2) * 1000 / Viewer.Height
MagnifierImage.Right = (X + MagnifierSize / 2) * 1000 / Viewer.Width
MagnifierImage.Bottom = (Y + MagnifierSize / 2) * 1000 / Viewer.Height
End If
End If
End Sub
Sub EndMagnifier()
If Not MagnifierImage Is Nothing Then
Viewer.Images.Remove Viewer.Images.IndexOf(MagnifierImage)
Set MagnifierImage = Nothing
Viewer.Refresh
End If
End Sub
Private Sub Viewer_MouseDown(Button As Integer, Shift As Integer, X As Long, Y As Long)
SetMagnifier X, Y
LastPositionX = X
LastPositionY = Y
End Sub
Private Sub Viewer_MouseMove(Button As Integer, Shift As Integer, X As Long, Y As Long)
SetMagnifier X, Y
LastPositionX = X
LastPositionY = Y
End Sub
Private Sub Viewer_MouseUp(Button As Integer, Shift As Integer, X As Long, Y As Long)
EndMagnifier
End Sub
Download the VB6 Sample Viewer Program to try the Magnifying glass.