如何将图像转换为拼图?

How to convert image to puzzle?

我想让程序获取用户的照片,然后将图像转换为拼图(例如 100 块),然后制作 100 个图片框。

我将以下代码用于 4 件和 9 件。

if (Image_Num.SelectedIndex == 0)
        {
            PB = new PictureBox[4];

            int W, H;
            var imgarray = new Image[4];
            var img = User_Image.Image;
            W = img.Width / 2;
            H = img.Height / 2;
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    var index = i * 2 + j;
                    imgarray[index] = new Bitmap(W, H);
                    var graphics = Graphics.FromImage(imgarray[index]);
                    graphics.DrawImage(img, new Rectangle(0, 0, W, H), new Rectangle(i * W, j * H, W, H), GraphicsUnit.Pixel);
                    graphics.Dispose();
                }
            }

            PB[0] = new PictureBox
            {
                Name = "P1",
                Size = new Size(100, 100),
                Location = new Point(394, 60),
                Image = imgarray[0],
                SizeMode = PictureBoxSizeMode.StretchImage
            };
            ...

            PB[0].MouseEnter += Images_M_E;
            ...


            PB[0].MouseLeave += Images_M_L;
            ...


            PB[0].MouseClick += Images_C;
            ...

            Controls.Add(PB[0]);
            ...
        }

我做不到 100 次或更多次。

谢谢。

这是一个示例:从您的问题中提取的代码经过最小的更改以使其灵活;我已经执行了上述评论,但没有超出此范围。

希望您能从中获得乐趣:-)

private void SetUpPuzzle_Click(int parts)  // use the number of parts on each side
{
    Control board = panel1;  // use the control you want to use or the form!
    int total = parts * parts;
    var PB = new PictureBox[total];

    var imgarray = new Image[total];
    var img = User_Image.Image;
    int W = img.Width / parts;
    int H = img.Height / parts;
    int size = 100;
    for (int x = 0; x < parts; x++)
    {
        for (int y = 0; y < parts; y++)
        {
            var index = x * parts + y;
            imgarray[index] = new Bitmap(W, H);
            using (Graphics graphics = Graphics.FromImage(imgarray[index]))
                graphics.DrawImage(img, new Rectangle(0, 0, W, H), 
                                   new Rectangle(x * W, y * H, W, H), GraphicsUnit.Pixel);
            PB[index] = new PictureBox
            {
                Name = "P"+ index,
                Size = new Size(size, size),
                Location = new Point(x * size, y * size),
                Image = imgarray[index],
                SizeMode = PictureBoxSizeMode.StretchImage
            };
            PB[index].MouseEnter += Images_M_E;
            PB[index].MouseLeave += Images_M_L;
            board.Controls.Add(PB[index]);
        }
    }
}

您可以传入任何(相当小的)数字。您可能还想传入完整图像,并且可能希望使尺寸灵活; 100x100 是代码中的最后一个幻数。但一切都在你自己的时间里..

以下是所有 pbox 都可以使用的常见 事件的两个示例:

private void Images_M_E(object sender, EventArgs e)
{
    PictureBox pb = sender as PictureBox;  // here we get a reference to the actual pbox
    pb.BorderStyle = BorderStyle.FixedSingle;   // do what you..
}
private void Images_M_L(object sender, EventArgs e)
{
    PictureBox pb = sender as PictureBox;
    pb.BorderStyle = BorderStyle.None;   // ..want to do here
}

我基本上保留了代码;这意味着并非所有问题都得到解决。真的,没有明显的数组需求。创建的图像和 pboxes 都添加到它们所属的位置;不需要在数组中保留额外的引用,在代码 运行 through.

之后无论如何都会超出范围

另外你的变量拼写有点混乱。所有私有字段都应该是camelCase,所以你不要首字母大写!属性和 类 是 PascalCase。基本上任何东西都不应该由连续的大写字母组成(正如我在你之前的帖子中看到的那样。)