如何将图像转换为拼图? [关闭]

问题描述 投票:1回答:1

我希望该程序获得用户的图片,然后将图像转换为拼图(例如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次或更多次。

谢谢。

c# image picturebox puzzle
1个回答
2
投票

以下是一个示例:从您的问题中获取的代码,只需进行少量更改即可灵活处理;我已经实施了上述评论,但没有超出这个范围。

我希望你能学到它的乐趣:-)

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
}

我把代码留下了很多;这意味着并非所有问题都得到解决。真的,对阵列没有明显的需求。创建的图像和pbox都添加到它们所属的位置;无需在数组中保留额外的引用,这些引用在代码运行后仍会超出范围。

你的变量拼写也有点困惑。所有私人档案应该是camelCase,所以你不要把第一个字母大写!属性和类是PascalCase。基本上没有什么应该由连续的上限组成(正如我在之前的帖子中看到的那样)。

© www.soinside.com 2019 - 2024. All rights reserved.