Centers the image display at the specified point.
Syntax
Visual Basic (Declaration) | |
---|
Public Overridable Sub CenterAtPoint( _
ByVal pt As Point _
) |
C# | |
---|
public virtual void CenterAtPoint(
Point pt
) |
C++/CLI | |
---|
public:
virtual void CenterAtPoint(
Point pt
) |
Parameters
- pt
- The point at which to center the image display, in client coordinates.
Example
This example shows a method to change the viewer scale factor (zoom) while
keeping the viewer centered.
Visual Basic | Copy Code |
---|
Private Sub ZoomAndCenter(ByVal viewer As BitmapSourceViewer, ByVal scaleFactor As Double)
Const minimumScaleFactor As Double = 0.05
Const maximumScaleFactor As Double = 11
scaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor))
If viewer.ScaleFactor <> scaleFactor Then
Dim rc As Rect = Rect.Intersect(New Rect(0, 0, viewer.Width, viewer.Height), viewer.PhysicalViewRectangle)
Dim center As System.Windows.Point = New System.Windows.Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2)
viewer.ScaleFactor = scaleFactor
viewer.CenterAtPoint(center)
End If
End Sub |
C# | Copy Code |
---|
void ZoomAndCenter(BitmapSourceViewer viewer, double scaleFactor) { // Minimum and maximum scale factors allowed (change if you have to) const double minimumScaleFactor = 0.05; const double maximumScaleFactor = 11; // Normalize the scale factor based on min and max scaleFactor = Math.Max(minimumScaleFactor, Math.Min(maximumScaleFactor, scaleFactor)); // Check if we need to change the scale factor for the viewer if(viewer.ScaleFactor != scaleFactor) { // Get the current center in logical units // We will use this point later to re-center the viewer // Get what you see in physical coordinates Rect rc = Rect.Intersect(new Rect(0, 0, viewer.Width, viewer.Height), viewer.PhysicalViewRectangle); // Get the center of what you see in physical coordinates Point center = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2); // Set the new scale factor viewer.ScaleFactor = scaleFactor; // Bring the old center into the center of the view viewer.CenterAtPoint(center); } } |
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Vista, and Windows Server 2003 family
See Also