Initialising an image to the viewer size (stretch and scroll)

It is often useful to zoom and scroll an image to fill the viewer. The quick way of doing this is by using StretchMode method i.e.

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 full size of the viewer BUT zooming and scrolling are now available for use.

int MouseDownX, MouseDownY;
Point scrollPoint = new Point();
private void Viewer_Click(object sender, EventArgs e)
{
    if (Viewer.Images.Count == 0)
    {
        Viewer.CurrentImage.StretchMode = DicomObjects.Enums.StretchModes.StretchCentred;
        float Zm = DicomGlobal.Zoom(Viewer.CurrentImage.Matrix(Viewer));
        Viewer.CurrentImage.StretchMode = DicomObjects.Enums.StretchModes.None;
        Viewer.CurrentImage.Zoom = Zm; // apply the values used by the auto stretch to a non stretched image
        Viewer.CurrentImage.Scroll = scrollPoint;
     }
} 

private void Viewer_MouseDown(object sender, MouseEventArgs e)
{
    if ((e.Button == MouseButtons.Left) && (Viewer.Images.Count != 0))
    {
        MouseDownX = e.X - Viewer.CurrentImage.Scroll.X;
        MouseDownY = e.Y - Viewer.CurrentImage.Scroll.Y;
    }          
}
 
private void Viewer_MouseMove(object sender, MouseEventArgs e)
{
    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;
    }
}
Relevance: