Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

c# - Image splitting into 9 pieces

My Code:

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

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

Please Help!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...