简体   繁体   English

绘图图像错误的大小WPF

[英]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. 我在调试器中检查了图像的高度,它与cellSize的大小相同,这是正确的,但在视觉上它看起来更短,并且它的开始没有在正确的位置绘制。 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? 这不是如何设置图像大小,高度和宽度?

在此输入图像描述

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);
            }

Regarding your last questions in the comments. 关于你在评论中的最后一个问题。

Ok, here's a screenshot of how I structured your project: Screenshot 好的,这是我如何构建项目的截图:截图 在此输入图像描述

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 :) 等等......问题解决了:)

When you programmatically draw an image in WPF, its stretch property is set to uniform by default. 以编程方式在WPF中绘制图像时,默认情况下其stretch属性设置为uniform。 Add this line: 添加此行:

verticalWall.Stretch = Stretch.Fill;

Now it will size correctly even if it does not stretch uniformly. 现在即使它没有均匀拉伸也能正确调整尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM