C# 图像分割成9块

C# 图像分割成9块,c#,C#,我的代码: private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { ngr.Dispose(); List<Image> list = new List<Image>(); Graphics g = Graphics.FromImage(pictureBox1.Image); B

我的代码:

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        ngr.Dispose();
                    List<Image> list = new List<Image>();
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        Brush redBrush = new SolidBrush(Color.Red);
        Pen pen = new Pen(redBrush,3);
        MessageBox.Show(pictureBox1.Image.Width + " " + pictureBox1.Image.Height);
        for (int i = 0; i < pictureBox1.Image.Width; i = (pictureBox1.Image.Width / 3) + i)
        {
            for (int y = 0; y < pictureBox1.Image.Height; y = (pictureBox1.Image.Height / 3) + y)
            {
                Rectangle r = new Rectangle(i, y, pictureBox1.Image.Width / 3, pictureBox1.Image.Height / 3);
                g.DrawRectangle(pen,r );
                if (i > 0 && y > 0)
                {
                    if (i + r.Width < pictureBox1.Image.Width && y + r.Height < pictureBox1.Image.Height)
                    {
                        list.Add(cropImage(pictureBox1.Image, r));
                    }
                }
            }
        }
        g.Dispose();
        pictureBox1.Invalidate();
        pictureBox1.Image = list[0];



    }

    private static Image cropImage(Image img, Rectangle cropArea)
    {
        Bitmap bmpImage = new Bitmap(img);
        Bitmap bmpCrop = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.DontCare);
        return (Image)(bmpCrop);
    }
private void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
ngr.Dispose();
列表=新列表();
图形g=Graphics.FromImage(pictureBox1.Image);
画笔红色画笔=新的SolidBrush(颜色为红色);
钢笔=新钢笔(红色画笔,3);
MessageBox.Show(pictureBox1.Image.Width+“”+pictureBox1.Image.Height);
对于(int i=0;i0&&y>0)
{
if(i+r.宽度
此代码只向列表中添加了2个条目,但没有添加其他7个条目


请帮忙

更改这部分代码,然后重试:

    for (int i = 0; i < 3; i++)
    {
        for (int y = 0; y < 3; y++)
        {
            Rectangle r = new Rectangle(i*(pictureBox1.Image.Width / 3), 
                                        y*(pictureBox1.Image.Height / 3), 
                                        pictureBox1.Image.Width / 3, 
                                        pictureBox1.Image.Height / 3);

            g.DrawRectangle(pen,r );

            list.Add(cropImage(pictureBox1.Image, r));
        }
    }
for(int i=0;i<3;i++)
{
对于(int y=0;y<3;y++)
{
矩形r=新矩形(i*(pictureBox1.Image.Width/3),
y*(图B1.Image.Height/3),
pictureBox1.Image.Width/3,
图片b 1.Image.Height/3);
g、 绘图矩形(笔,r);
添加(cropImage(pictureBox1.Image,r));
}
}