user2602079
user2602079

Reputation: 1393

Drawing Image wrong size WPF

I'm drawing a cell wall on a map and it is the wrong size. I checked the height of the image in debugger and it was the same size as the cellSize which is correct, but visually it appears shorter and the start of it is not drawn in the correct place. Like it is being drawn over top of at both ends. When I change the Height property of the image it does not make the image bigger. Is that not how to set the image size, with Height and Width?

enter image description here

Here is the code.

private void calculateWall(int wall, int cell)
        {
            int[] mapData = this.getMapData(cell);
            int startOfCol = mapData[0];
            int endOfCol = mapData[1];
            int startOfRow = mapData[2];
            int endOfRow = mapData[3];
            CellSide rightSide = this.getCells()[cell].myRightWall;
            CellSide bottomSide = this.getCells()[cell].myBottomWall;

            float thickness = myMap.myCellSize * (float)0.1;
            Math.Round(thickness, 0);
            int newThickness = Convert.ToInt32(thickness);

            float height = myMap.myCellSize * (float)0.2;
            Math.Round(height, 0);
            int newHeight = Convert.ToInt32(height);

            if (rightSide.hasWall == 1)
            {
                Image verticalWall = new Image();

                // Create source.
                BitmapImage bi = new BitmapImage();

                // BitmapImage.UriSource must be in a BeginInit/EndInit block.
                bi.BeginInit();
                bi.UriSource = new Uri("verticalWall.jpg", UriKind.RelativeOrAbsolute);
                bi.EndInit();

                // Set the image source.
                verticalWall.Source = bi;

                verticalWall.Width = newThickness;
                verticalWall.Height = myMap.myCellSize;
                verticalWall.SetValue(Canvas.TopProperty, Convert.ToDouble(startOfRow));
                verticalWall.SetValue(Canvas.LeftProperty, Convert.ToDouble(endOfCol - (newThickness / 2)));

                verticalWall.IsHitTestVisible = false;
                this.view.pbxMap.Children.Add(verticalWall);
            }

Upvotes: 0

Views: 2366

Answers (2)

user2602079
user2602079

Reputation: 1393

When you programmatically draw an image in WPF, its stretch property is set to uniform by default. Add this line:

verticalWall.Stretch = Stretch.Fill;

Now it will size correctly even if it does not stretch uniformly.

Upvotes: 2

Noctis
Noctis

Reputation: 11763

Regarding your last questions in the comments.

Ok, here's a screenshot of how I structured your project: Screenshot enter image description here

Now, wherever you use any of the pictures,you'll have to add the folder to that path:

Source="exit.png"

Will become:

Source="/Images/exit.png"

This:

<Page.Background>
    <ImageBrush ImageSource="marbleBackground.jpg"/>
</Page.Background>

Will become:

<Page.Background>
    <ImageBrush ImageSource="/Images/marbleBackground.jpg"/>
</Page.Background>

And so on ... problem solved :)

Upvotes: 1

Related Questions