在pictureBox中画网格出现的问题

shijies 2021-09-08 21:50:36

用以下代码画网格,可是显示出的线段不能对齐,甚至横线在竖线右部的外边缘还冒出了一段,不知何故。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                                          
            Graphics g = pictureBox1.CreateGraphics();
            Pen bluePen = new Pen(Color.Blue, 3);

            for (int i = 0; i < this.pictureBox1.Width; i++)
            {
               ;
                g.DrawLine(bluePen, new Point(i, 0), new Point(i, this.pictureBox1.Height));
                i += 60;
            }
            //水平
            for (int j = 0; j < this.pictureBox1.Height; j++)
            {
                g.DrawLine(bluePen, new Point(0, j), new Point(this.pictureBox1.Width, j));
                j += (this.pictureBox1.Height - 6) / 10;
            }

        }
    }
}

...全文
248 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2021-09-10
  • 打赏
  • 举报
回复

计算i与j的值 其实很简单,你需要先确定行列数
dx = Width/ 列数
dy = Height/行数

shijies 2021-09-10
  • 打赏
  • 举报
回复

竖线在最底部也冒出头了

shijies 2021-09-09
  • 打赏
  • 举报
回复

如果要正方形网格,设置pictureBox的Width与Height相等,另外还要计算i与j的值,这个办法是不是比较麻烦?

shijies 2021-09-09
  • 打赏
  • 举报
回复

以第一行为例进行分析,横线最右边的点的坐标为:
new Point(0, this.pictureBox1.Height)
最右边竖线与第一行横线交点的坐标是:
new Point(this.pictureBox1.Width,this.pictureBox1.Height-1)
是不是这样?我也是糊涂了。

xuzuning 2021-09-09
  • 打赏
  • 举报
回复

点 new Point(i, this.pictureBox1.Height) 在 点 new Point(this.pictureBox1.Width, j) 的右边,这就是横线出头的原因

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧