图像分为9个部分 [英] Image splitting into 9 pieces

查看:87
本文介绍了图像分为9个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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

此代码仅将2个添加到列表中,而不添加其他7个。

This Code adds only 2 pieces to the list but not the other 7 pieces.

请帮助!

推荐答案

更改此部分代码,然后再试一次:

Change this part of code and try again:

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

这篇关于图像分为9个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆