How to convert image to puzzle? [closed]

我的未来我决定 提交于 2019-12-04 19:32:29

Here is an example: The code taken from your question with minimal changes to make it flexible; I have implemented the above comments but not gone beyond that.

I hope you'll have fun studying it :-)

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

You can pass in any (reasonably small) number. You may also want to pass in the full image and you may want to make the size flexible; the 100x100 is the last magic number in the code. But all in your owm time..

Here are two examples for common events that all pboxes can use:

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
}

I left the code pretty much as it was; this means that not all issues are resolved. There is no obvious need for the arrays, really. The created images and pboxes are both added where they belong; no need to hold extra references in the arrays, which will go out of scope anyway after the code has run through.

Also your spelling of variables is a bit confused. All private filed should be camelCase, so you do not capitalize the first letter! Properties and Classes are PascalCase. Basically nothing should consist of consecutive caps (as I saw in your previous posts.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!