Implement Centered Zoom in DicomObjects


Implementing the Centered Zoom function in DicomObjects is simple and easy.

DicomObjects.NET

In the code below, 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:

    
privatevoiddicomViewer1_MouseDown(objectsender,MouseEventArgse)
{
	// last mouse position for the zoom operation
	LastXforZoom=e.X;
	LastYforZoom=e.Y;
}

privatevoidtrackBar1_Scroll(objectsender,EventArgse)
{
	PointReferencePoint;

	// calculate the Image Coordinates of 
	// the user-clicked point or centre as required
	ReferencePoint=radioButton1.Checked?newPoint(LastXforZoom,LastYforZoom):
		newPoint(dicomViewer1.Width/2,dicomViewer1.Height/2);
	
	//1)find the image coordinates of the reference point(held in single item array pts)
	Point[]pts=new[]{ReferencePoint};
	System.Drawing.Drawing2D.Matrixmx;
	
	//2)use an inverted copy of the Matrix to go from screen to image space
	mx=dicomViewer1.CurrentImage.Matrix(dicomViewer1);
	mx.Invert();
	mx.TransformPoints(pts);
	
	//3)change Image Zoom
	// Anarbitrary calculation to give a reasonable exponential zoom range
	dicomViewer1.CurrentImage.Zoom=(float)(1+(trackBar1.Value-11)*0.125);
	
	//4)apply new Image Matrix to calculate Screen Coordinates of the reference point
	// transforming image back to screen, using the new modified Matrix
	System.Drawing.Drawing2D.Matrixmx2;
	mx2=dicomViewer1.CurrentImage.Matrix(dicomViewer1);
	mx2.TransformPoints(pts);
	
	//5)Calculate offset to modify Image's Scroll property 
	// to leave Reference point unchanged on the screen
	SizeAdjustment=newSize(pts[0])-newSize(ReferencePoint);
	dicomViewer1.CurrentImage.Scroll-=Adjustment;
}

privatevoidForm1_Load(objectsender,EventArgse)
{
	OpenFileDialogd=newOpenFileDialog();
	if(d.ShowDialog()==DialogResult.OK)
	{
		dicomViewer1.Images.Read(d.FileName);
		dicomViewer1.CurrentImage.StretchMode=DicomObjects.Enums.StretchModes.None;
	}
}

DicomObjects.COM

Since there is no Matrix object available in COM version, you will have to calculate the Image.ScrollX and Image.ScrollY values yourself.

  
Dim xinit As Integer, yinit As Integer, Origzoom As Single
Dim LastMouseX As Integer, LastMouseY As Integer

Private Sub Command1_Click()
    CommonDialog1.ShowOpen
    DicomViewer1.Images.ReadFile CommonDialog1.FileName
    DicomViewer1.CurrentImage.StretchToFit = False
    DicomViewer1.CurrentImage.Zoom = 1
End Sub

Private Sub DicomViewer1_MouseDown(Button As Integer, Shift As Integer, x As Long, y As Long)
    LastMouseX = x
    LastMouseY = y
End Sub

Private Sub Slider1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
	xinit = DicomViewer1.CurrentImage.ActualScrollX
	yinit = DicomViewer1.CurrentImage.ActualScrollY
	' set up inital zoom values here
	Origzoom = DicomViewer1.CurrentImage.ActualZoom	
End Sub

Private Sub Slider1_Scroll()
	Dim Image As DicomImage, oldzoom As Single, newzoom As Single
	Dim CentreX As Integer, CentreY As Integer
	Set Image = DicomViewer1.CurrentImage
	newzoom = 2 ^ (Slider1 / 4)
	oldzoom = Image.ActualZoom ' this allows for a stretched to fit image also    
	If ImageCentre Then
		CentreX = DicomViewer1.Width / 2
		CentreY = DicomViewer1.Height / 2
	Else
		CentreX = LastMouseX
		CentreY = LastMouseY
	End If
	
	Image.ShowUpdates = False 'prevent multiple updates
	Image.Zoom = newzoom
	Image.ScrollX = xinit - (CentreX + xinit) * (1 - Image.Zoom / Origzoom)
	Image.ScrollY = yinit - (CentreY + yinit) * (1 - Image.Zoom / Origzoom)
	Image.StretchToFit = False
	Image.ShowUpdates = True
End Sub


We use cookies to give you the best possible experience on our website. By continuing to use this website, you agree with our use of cookies. for more information please click HERE