用C#制作图片拼图 [英] making picture puzzle with c#

查看:282
本文介绍了用C#制作图片拼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计阶段将35 PictureBox添加到表单中,我想将这些PictureBox分配给数组,但是我无法做到这一点,请帮助我解决这个问题!

I added the 35 PictureBox to the form at the design stage and ı want to assign these picturebox to array but ı cant do this please help me about this subject!

推荐答案

首先,您根本不需要PictureBox来执行此操作.您只需要在需要的地方画出需要的图像部分即可.使用picbox控件只会使事情复杂化.

数组将为您做什么?没什么...
First, you don''t need PictureBoxs to do this, at all. You just need to draw the portions of the image you need where you need them. Using picbox controls just complicates things for you.

What is an array going to do for you? Nothing...


下面的代码对我有用:

The code below worked for me:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestTabs
{
    public partial class Form1 : Form
    {
        List<PictureBox> pictureBoxes;
        public Form1()
        {
            pictureBoxes = null;
            InitializeComponent();
        }

        private List<PictureBox> collectPictureBoxes(List<PictureBox> collection, Control me)
        {
            foreach (Control ctrl in me.Controls)
            {
                if (ctrl.GetType() == typeof(PictureBox))
                {
                    collection.Add((PictureBox)ctrl);
                }
                if (ctrl.HasChildren)
                {
                    collectPictureBoxes(collection, ctrl);
                }
            }
            return collection;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBoxes = collectPictureBoxes(new List<PictureBox>(), this);
            foreach (PictureBox pb in pictureBoxes)
            {
                MessageBox.Show(pb.Name);
            }
        }
    }
}




问候,

—MRB




Regards,

—MRB


我不会将它们分配为设计时间:我会在运行时直接输入.这样做的原因是,没有真正的场合需要按名称(仅相对于6x6数组的位置)引用PictureBox.实际上,我可能还是不会使用Pictureboxes,而是直接画在面板上,并有一个6x6的整数数组来告诉我图片的哪个部分应该在哪个正方形中.但是,图片框也可以使用.

如果您必须在设计时创建它们,那么创建它们的数组并不是太困难-只是一团糟:
I woudln''t assign them as design time: I would just go straight in at run time. The reason for that is that is that there is no real occasion when you want to refer to the PictureBoxes by name, just by position relative to a 6x6 array. In actual fact, I probably wouldn''t use Pictureboxes anyway, but would draw directly onto a panel instead, and have a 6x6 array of integers to tell me which section of the picture should be in which square. But, picture boxes will work too.

If you must create them at design time, then creating an array of them isn''t too difficult - it''s just messy:
private PictureBox[,] pictures;
private void SetPictures()
    {
    pictures = new PictureBox[6, 6] {
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox2, pictureBox3},
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox2, pictureBox3},
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox2, pictureBox3},
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox2, pictureBox3},
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox2, pictureBox3},
    {pictureBox1, pictureBox2, pictureBox3, pictureBox1, pictureBox35, null},
    };
    }

(您必须在方法中执行此操作,因为编译器不允许您使用非静态字段初始值设定项)
您将不得不更改名称-我剪切了"n"粘贴以获得我需要的35个.

(You have to do it in a method, as the compiler won''t let you use non-static field initializers)
You will have to change the names - I cut''n''pasted to get the 35 I needed.


这篇关于用C#制作图片拼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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