0

I'm trying to draw a rectangle into an Image:

<Canvas>
    <Image>
        <Image.Source>
            <DrawingImage>
                <DrawingImage.Drawing>
                    <DrawingGroup x:Name="_drawingGroup" />
                </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>
</Canvas>

The c# code looks like:

DrawingVisual visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
    Rect rect = new Rect(new Point(100, 100), new Size(200, 100));
    dc.DrawRectangle(Brushes.Blue, null, rect);
}

this._drawingGroup.Children.Add(visual.Drawing);
this._drawingGroup.Freeze();

As you can see, I intend to draw a rectangle with left top corner (100,100). But it doesn't work as expected - it always paint at (0,0). What's wrong with me?

Jamleck
  • 1,017
  • 7
  • 12
sspine
  • 53
  • 1
  • 6

1 Answers1

0

A DrawingImage aligns its Drawing to the visible Bounds of the Drawing.

As a workaround you may just add a transparent rectangle with origin at (0, 0). Note also that you don't need a DrawingVisual to create a Drawing:

drawingGroup.Children.Add(new GeometryDrawing(
    Brushes.Transparent, null,
    new RectangleGeometry(new Rect(0, 0, 1, 1))));

drawingGroup.Children.Add(new GeometryDrawing(
    Brushes.Blue, null,
    new RectangleGeometry(new Rect(100, 100, 200, 100))));

drawingGroup.Freeze();

An alternative to an Image control with a DrawingImage may be a (large enough) Rectangle with a DrawingBrush:

<Rectangle Width="10000" Height="10000">
    <Rectangle.Fill>
        <DrawingBrush ViewboxUnits="Absolute" Viewbox="0,0,10000,10000">
            <DrawingBrush.Drawing>
                <DrawingGroup x:Name="drawingGroup" />
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Rectangle.Fill>
</Rectangle>

Now you could create you Drawing like intended:

drawingGroup.Children.Add(new GeometryDrawing(
    Brushes.Blue, null,
    new RectangleGeometry(new Rect(100, 100, 200, 100))));

drawingGroup.Freeze();
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • The code I post is only a simplified version, and I cannot change the over all code structure. DrawingVisual is also necessary because I will do complicated drawings in it. The workaround of adding a transparent rectangle (0,0,1,1) seems feasible for me. Is there a better way, for example, is it possible to change some properties of DrawingImage to keep the bounds? – sspine Mar 30 '16 at 08:26
  • I doubt there is any kind of drawing that can only be created with a DrawingContext, but not by directly creating the appropriate Drawing objects. – Clemens Mar 30 '16 at 09:55