Jeff
Jeff

Reputation: 5956

DrawingImage always draws in upper left, regardless of coordinates supplied

I'm attempting to draw a set of images in an Image in WPF, potentially off from top left, but no matter what I do, either the Image, the DrawingImage or the DrawingGroup are causing the image that is closest to the top left corner to default to the top left corner of the image.

Here's the code:

XAML:

<Canvas Grid.Column="1" Name="Canvas">
    <Image Name="imgLevel">
        <Image.Source>
            <DrawingImage>
                 <DrawingImage.Drawing>
                      <DrawingGroup>
                           <ImageDrawing Rect="120, 120, 100, 100" ImageSource="" />
                           <ImageDrawing Rect="300, 300, 100, 100" ImageSource="" />
                       </DrawingGroup>
                 </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>
</Canvas>

No matter what I set the coordinates to for the first ImageDrawing, it's always in the upper left corner.

Is there a way to override this behaviour? More importantly, I eventually want to be able to specify negative numbers to that Rect. Is this possible?

Upvotes: 1

Views: 759

Answers (3)

Karl
Karl

Reputation: 3216

It's unclear to me why, but placing a one-pixel wide Rectangle (using a nonvisible brush) into the origin of the DrawingGroup solves this problem. It would be good to know what rules govern this behavior in WPF.

Upvotes: 0

marklam
marklam

Reputation: 5358

Add something to your drawing group which is positioned at your desired top-left (for example a RectangleGeometry which covers the whole area you want, but has a Transparent brush).

Then when the DrawingImage gets the bounds of the whole group, it will be forced to at least the size of your 'invisible' geometry.

Upvotes: 1

cplotts
cplotts

Reputation: 14252

Is there any reason you can't use the Canvas.Left and Canvas.Top attached properties on the Image control? Like this:

<Canvas Name="Canvas" Grid.Column="1">
    <Image Canvas.Left="-5" Canvas.Top="-10" Name="imgLevel">
        <Image.Source>
        </Image.Source>
    </Image>
</Canvas>

Upvotes: 1

Related Questions