C# 填充矩形留白space

C# Filling rectangle leaves a white space

我有一个 C# winforms,我在其中绘制一个矩形然后填充它。

代码如下:

LinearGradientBrush secondGradient = new LinearGradientBrush(frontRect, Color.FromArgb(255, 190, 171, 18), Color.FromArgb(255, 152, 186, 27), 90);

     /* second row element */
                    g.DrawRectangle(pen, leftInsertionBox);
                    g.FillRectangle(secondGradient, leftInsertionBox.X + 1, leftInsertionBox.Y + 1, leftInsertionBox.Width - 1, leftInsertionBox.Height - 1);

                    g.DrawRectangle(pen, leftCoverBox);
                    g.FillRectangle(secondGradient, leftCoverBox.X + 1, leftCoverBox.Y + 1, leftCoverBox.Width - 1, leftCoverBox.Height - 1);
                    /* second row element */

问题是我可以在矩形线和填充之间看到一个白色像素 space。

Any clue on how to solve this?

尝试删除您添加的额外像素。

我会尝试逆向操作。先做填充操作,去除偏移计算(即填充整个区域),然后绘制边框。

g.FillRectangle(secondGradient, leftInsertionBox.X, leftInsertionBox.Y, leftInsertionBox.Width, leftInsertionBox.Height);
g.DrawRectangle(pen, leftInsertionBox);

g.FillRectangle(secondGradient, leftCoverBox.X, leftCoverBox.Y, leftCoverBox.Width, leftCoverBox.Height);
g.DrawRectangle(pen, leftCoverBox);