如何将图像转换为拼图? [英] How to convert image to puzzle?

查看:98
本文介绍了如何将图像转换为拼图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望程序获取用户照片,然后将图像转换为拼图(例如100张),然后制作100个图片框.

I Want the program get a pic of user ,then convert image to puzzle (for example 100 piece) then make 100 picture box.

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

I using this following code for 4 and 9 piece.

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次以上.

I can't do this for 100 times or more.

谢谢.

推荐答案

这里是一个示例:从您的问题中获取的代码经过了最小的更改以使其变得灵活;我已经实施了以上评论,但没有超出范围.

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.

希望您会喜欢它:-)

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是代码中的最后一个幻数.但是,一切都在你的时间里.

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..

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

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
}

我几乎保留了代码;这意味着并非所有问题都得到解决.确实,对数组没有明显的需求.创建的图像和pbox都添加在它们所属的位置.无需在数组中保留额外的引用,这些引用在代码运行完后将超出范围.

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.

此外,变量的拼写有些混乱.所有不公开的文件都应为 camelCase ,因此您不要大写首字母!属性和类是 PascalCase .基本上,任何内容都不应包含连续的上限(正如我在您之前的帖子中所见.)

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.)

这篇关于如何将图像转换为拼图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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