Code:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
ZoomPanel1.ZoomMode = Ab2d.Controls.ZoomPanel.ZoomModeType.Move;
Canvas zoomPanelMiniMapCanvas;
zoomPanelMiniMapCanvas = CreateMinimap(200);
zoomPanelMiniMapCanvas.VerticalAlignment = VerticalAlignment.Center;
zoomPanelMiniMapCanvas.HorizontalAlignment = HorizontalAlignment.Left;
MainGrid.Children.Add(zoomPanelMiniMapCanvas);
}
private Canvas _zoomPanelMiniMapCanvas;
private Rectangle _zoomPanelMiniMapRectangle;
private Canvas CreateMinimap(int width)
{
int height;
Image zoomPanelImage;
FrameworkElement content;
content = ZoomPanel1.Content as FrameworkElement;
if (content == null)
return _zoomPanelMiniMapCanvas;
// Adjust height for aspect ratio
height = Convert.ToInt32(width * content.ActualHeight / content.ActualWidth);
_zoomPanelMiniMapCanvas.Width = width;
_zoomPanelMiniMapCanvas.Height = height;
RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96 /* standard WPF dpi setting 96 */, PixelFormats.Pbgra32);
bmp.Render(content);
zoomPanelImage = new Image();
zoomPanelImage.Source = bmp;
_zoomPanelMiniMapRectangle = new Rectangle();
_zoomPanelMiniMapRectangle.Stroke = Brushes.Red;
_zoomPanelMiniMapCanvas.Children.Add(zoomPanelImage);
_zoomPanelMiniMapCanvas.Children.Add(_zoomPanelMiniMapRectangle);
UpdateMiniMapRectangle(ZoomPanel1.Viewbox);
ZoomPanel1.ViewboxChanged += new Ab2d.Controls.ZoomPanel.ViewboxChangedRoutedEventHandler(ZoomPanel1_ViewboxChanged);
return _zoomPanelMiniMapCanvas;
}
void ZoomPanel1_ViewboxChanged(object sender, Ab2d.Controls.ViewboxChangedRoutedEventArgs e)
{
// NOTE that if ZoomPanel is animated, the ZoomPanel1.Viewbox is not yet changed to the final value because the animation is just started
// The e.NewViewboxValue is already the final value
UpdateMiniMapRectangle(e.NewViewboxValue);
}
private void UpdateMiniMapRectangle(Rect ZoomPanelviewbox)
{
Canvas.SetLeft(_zoomPanelMiniMapRectangle, ZoomPanelviewbox.X * _zoomPanelMiniMapCanvas.Width);
Canvas.SetTop(_zoomPanelMiniMapRectangle, ZoomPanelviewbox.Y * _zoomPanelMiniMapCanvas.Height);
_zoomPanelMiniMapRectangle.Width = ZoomPanelviewbox.Width * _zoomPanelMiniMapCanvas.Width;
_zoomPanelMiniMapRectangle.Height = ZoomPanelviewbox.Height * _zoomPanelMiniMapCanvas.Height;
}