Initialising an image to the viewer size (stretch and scroll)
It is often useful to zoom and scroll an image to fill the viewer on first load. The quick way of doing this is by using StretchMode and a few other properties.
DicomObjects.NET
GetSelectedImage().StretchMode = DicomObjects.Enums.StretchModes.StretchCentred;
However if StretchMode is used it doesn’t allow the image to be scrolled or zoomed. To overcome this problem it is possible to save the scroll and zoom values used by the StretchMode , then remove the StretchMode and apply them to the image. This will leave an image in the viewer that is stretched to the current set StretchMode of the viewer and zooming and scrolling are now available for use.
int MouseDownX, MouseDownY;
private void Viewer_MouseDown(object sender, MouseEventArgs e)
{
if (Viewer.Images.Count > 0 && e.Button == MouseButtons.Left)
{
if (Viewer.CurrentImage.StretchMode != StretchModes.NoStretch)
{
float zoom = DicomGlobal.Zoom(Viewer.CurrentImage.Matrix(Viewer));
Point[] actualScroll = new Point[1] { new Point((int)Viewer.CurrentImage.Scroll.X, (int)Viewer.CurrentImage.Scroll.Y) };
Viewer.CurrentImage.Matrix(Viewer).TransformPoints(actualScroll);
Viewer.CurrentImage.StretchMode = StretchModes.NoStretch;
// apply the values used by stretch mode to a non stretched image
Viewer.CurrentImage.Zoom = zoom;
Viewer.CurrentImage.Scroll = actualScroll[0];
}
MouseDownX = e.X - (int)Viewer.CurrentImage.Scroll.X;
MouseDownY = e.Y - (int)Viewer.CurrentImage.Scroll.Y;
}
}
private void Viewer_MouseMove(object sender, MouseEventArgs e)
{
if (Viewer.Images.Count > 0)
{
Point P = new Point();
if ((e.Button == MouseButtons.Left) && (Viewer.Images.Count != 0))
{
P.X = e.X - MouseDownX;
P.Y = e.Y - MouseDownY;
Viewer.CurrentImage.Scroll = P;
}
}
}
DicomObjects.COM
Viewer.CurrentImage.StretchMode = StretchCentred
Dim x As Single
x = Viewer.CurrentImage.ActualScrollX
Dim y As Single
y = Viewer.CurrentImage.ActualScrollY
Dim z As Single
z = Viewer.CurrentImage.ActualZoom
Viewer.CurrentImage.ScrollX = x
Viewer.CurrentImage.ScrollY = y
Viewer.CurrentImage.Zoom = z
Viewer.CurrentImage.StretchMode = NoStretch