locked
How to know actual image size RRS feed

  • Question

  • I have Image-derived class inside of ViewBox, something like this:

     

    <ViewBox ... >

        <Grid ... >

            <Image Stretch="None" ... />      <- instance of my own class

        </Grid>

    </ViewBox>

     

    Image contains some bitmap. Inside of my class I want to know what is the scale of the bitmap shown on the screen, this means, ratio between bitmap width/height and Image ActualWidth/ActualHeight. The problem is that Image ActualWidth/ActualHeight are always equal to bitmap width/height - this is ViewBox service. I cannot look for ViewBox parent, because in other place my Image can be child of ScrollViewer or other conrol, can have ScaleTransform applied etc. Is there any generic way to know bitmap scale?

     

    My purpose is to draw graphics overlays over image, and they should always have the same pen width. I need to apply actual scale correction to overlay pen.

    Saturday, December 1, 2007 6:46 PM

All replies

  • This is XAML illustrating the problem:

     

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Viewbox>
        <Grid>
          <Image Stretch="None">
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <DrawingGroup>
                            <ImageDrawing Rect="0 0 200 200"
                                          ImageSource="http://res.sys-con.com/story/jul05/113346/Windows_Vista_logo.jpg" />

                            <GeometryDrawing>
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Red" Thickness="2"/>
                                </GeometryDrawing.Pen>

                                <GeometryDrawing.Geometry>
                                    <RectangleGeometry Rect="5 5 100 50" />
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                        </DrawingGroup>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
          </Image>
        </Grid>
    </Viewbox>
    </Page>

     

     

    ImageDrawing rectangle is set to actual bitmap size programmatically. GeometryDrawing is overlay showing over the image. When window is resized, overlay border width is changed, and I need to keep it constant.

    Saturday, December 1, 2007 7:05 PM
  • Hi Alex,

    The GeometryDrawing will be part of the DrawingImage, and WPF does the correct thing to resize it along with all those Drawings specified as DrawingImage.Drawing to fit into the new arranged layout slot. Is this behaviour not what you expect?

    Thanks
    Wednesday, December 5, 2007 7:19 AM
  • Yes, this is behavior that I expect. Everything that I draw is automatically resized according to transformation set by parent controls. However, my goal is to keep line width constant during resizing. For example, I draw rectangle with width and height 100, and pen width 2. If actual scale applied to my control is 2, physically this rectangle is drawn as 200*200, pen width 4. I want to know this coefficient and draw with pen size 1, to get constant physical line width on the screen.
    Generally, the problem is: how can any WPF control know actual transform applied to it by its parents. For now my solution is exposing ActualScale property from the control, expecting from a client to provide required information.

    Friday, December 7, 2007 6:43 AM
  •  

    Hello,

     

    I have tried writing control, which overrides ArrangeOverride and MeasureOverride, basically to do the same thing as ViewBox, and rerendering image with each call of ArrangeOverride. This keeps constant line width on the screen, but performance can be horrible if re-rendering a complex geometry.

     

    Bye John Simmons

    Wednesday, February 13, 2008 1:13 PM