(WPF) There is an empty space between continuous rectagon.

jun1.lee 21 Reputation points
2022-09-12T08:37:39.99+00:00

I draw a continuous rectangle in WPF Canvas.
A simple sample code is below.

<Window x:Class="_220912_RecPJT.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:_220912_RecPJT"  
        mc:Ignorable="d"  
        Title="MainWindow" Height="450" Width="450">  
    <Grid>  
        <Border BorderThickness="1" BorderBrush="Black" Width="300" Height="300" Margin="10 5 10 10">  
            <Canvas x:Name="cv_test" Width="300" Height="300">  
                  
            </Canvas>  
        </Border>  
  
    </Grid>  
</Window>  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Navigation;  
using System.Windows.Shapes;  
  
namespace _220912_RecPJT  
{  
    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        public MainWindow()  
        {  
            InitializeComponent();  
  
            //canvas draw  
  
            decimal temp_x = (decimal)23539.64;  
            decimal temp_y = (decimal)32793.21;  
            decimal ratio = (decimal)260 / 300;  
  
            decimal xin = (decimal)20.401102;  
  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 2 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 3 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 4 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 5 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 6 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
            DrawRec_Canvas(this.cv_test, (decimal)221.6049 - 7 * xin, (decimal)225.5982, (decimal)this.cv_test.Width, (decimal)this.cv_test.Height, (temp_x / 1000) * ratio, (temp_y / 1000) * ratio);  
  
        }  
        private void DrawRec_Canvas(Canvas can, decimal x, decimal y, decimal can_width, decimal can_height, decimal rec_width, decimal rec_height)  
        {  
            decimal w = (decimal)can.Width;  
            decimal h = (decimal)can.Height;  
  
            decimal real_x = x / can_width;  
            decimal real_y = y / can_height;  
  
            System.Windows.Shapes.Rectangle rec = new System.Windows.Shapes.Rectangle();  
            SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Yellow);  
            rec.Fill = mySolidColorBrush;  
            rec.Opacity = 0.2;  
  
            rec.Width = decimal.ToDouble(rec_width);  
            rec.Height = decimal.ToDouble(rec_height);  
            rec.Stroke = System.Windows.Media.Brushes.Black;  
            rec.StrokeThickness = 1;  
            Canvas.SetLeft(rec, decimal.ToDouble(real_x * w));  
            Canvas.SetBottom(rec, decimal.ToDouble(real_y * h));  
            can.Children.Add(rec);  
        }  
  
    }  
}  

If you copy this code and run it, multiple rectangle will be listed without empty space.
However, adding this sample code to the Canvas of my big project, creates a very small empty space between that rectangle randomly.
And I confirmed that there was no empty space when I changed all the numbers that were not decimal to int in my project.
My project code was over 20,000 lines and I couldn't paste it.
I don't know why I have an empty space when I use Sample code as the same condition in my project.
I think it's related to the decimal data type, but I don't know exactly.

Thank you for any help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes