Adorner - extends outside zoompanel, unwanted behavior

I have an Image inside the zoompanel:
<ab2d:ZoomPanel Name="ZoomPanel" Margin="5,5,5,5" Height="600" Width="800"
IsViewboxLimited="True" ViewboxMinSize="0.0078125, 0.0078125" ViewboxLimits="0, 0, 1, 1"
RenderOptions.BitmapScalingMode="NearestNeighbor" ZoomMode="Move" ZoomFactor="1" >
<Image Name="ZoomImage" MouseMove="ZoomImage_MouseMove" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />

I need to draw on top of my image and tried using an adorner for this.
When panning the ZoomPanel the adorner can extend outside the ZoomPanel.

class ROI : Adorner
double X,Y,W,H;

public ROI(System.Windows.UIElement adornerElement, double x, double y, double w, double h)
: base(adornerElement)
X = x;
Y = y;
W = w;
H = h;

protected override void OnRender(DrawingContext drawingContext)
System.Windows.Rect rect = new System.Windows.Rect(X, Y, W, H);
System.Windows.Media.Pen streg = new System.Windows.Media.Pen(Brushes.Red, 1);
drawingContext.DrawRectangle(null, streg, rect);
drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.AdornedElement.RenderSize.Width, this.AdornedElement.RenderSize.Height)));

I tried PushClip and Pop for the adorner but that did not help.

Is there a way to avoid the adorner extending outside the ZoomPanel?

You do not need to use adroner for that.

You can simply define a Canvas or other Panel after the ZoomPanel.
For example:

<Grid ...>

<ZoomPanel Grid.Column="0" Grid.Row="0" ... />
<Canvas Name="ZoomPanelOverlay" Grid.Column="0" Grid.Row="0" IsHitTestVisible="false" />

I used Grid to put both ZoomPanel and Canvas to the same cell and therefore have the same size.

Also do not forget to disable all mouse events on the ZoomPanelOverlay with setting the IsHitTestVisible to false - this way the mouse events will be passed to ZoomPanel.
Andrej Benedik
I use the zoompanel to show images. Pixel values are read out for spectroscopic measurements and precise pixel coordinates and values are important.

The overlay I draw is supposed to scale with the contents of the zoompanel. I need to highlight specific pixels or (rectangular) regions. The overlay should therefore scale and pan with the image itself.

I want to use adorners for the following reasons:
*It is easy to add and remove adorners directly on the Image because i can use the pixelvalues for the drawing coordinates of the rectangles.
*A rectangle is drawn between pixels and not on top of pixels.
*When zooming out the adorner is always visible even though the line gets very thin.

As an alternative solution I have used a grid inside the zoompanel and added an extra image on top of the actual image - this image is transparent for most pixels. I then set color and remove transparency on the regions to draw.
This is not as good as adorners for the following reasons:
*Overlay is on top of pixels(can be solved by increasing resolution of overlay).
*Rendering will make thin lines/small points disappear when zooming out (big issue).
Shifted to adding a canvas inside the zoompanel.

<Canvas Name="Overlay"/>

and then simply add rectangles to the canvas:
Rectangle rect = new Rectangle { Width=roi[2] - roi[0]+1, Height = roi[3] - roi[1]+1, Stroke = Brushes.Red };
Canvas.SetLeft(rect, roi[0]);
Canvas.SetTop(rect, roi[1]);

Where roi[] is Left,Top,Rigth,Bottom of rectangle in Image pixel coordinates.

Forum Jump:

Users browsing this thread:
1 Guest(s)