这是Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace pintu
{
    public partial class Form1 : Form
    {
        Game theGame;
        private Image img;        private PictureBox pbLast;        /// <summary>
        /// 记录每一位置是哪一图块
        /// </summary>
        private PictureBox[] pbs;
        private Point[,] pts;//存储每一个位置的座标        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint
                | ControlStyles.UserPaint,
                true);
            theGame = new Game();        }
        //从文件载入图片
        private void loadImg(string fileName)
        {            this.img = new Bitmap(Image.FromFile(fileName), this.panel1.Size);
        }        //对图块进行绘图
        private void updateCard()
        {
            this.SuspendLayout();
            if (this.img != null)
            {
                GraphicsUnit units = GraphicsUnit.Pixel;
                //绘制缩略图
                Image ig = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                Graphics g = Graphics.FromImage(ig);
                g.DrawImage(this.img, this.pictureBox1.ClientRectangle, this.panel1.ClientRectangle, units);
                this.pictureBox1.Image = ig;                //绘制各图块
               
                int len = theGame.geshu;
                theGame.InitGameBoard();
                for (int i = 0; i < len; i++)
                {
                    for (int j = 0; j < len; j++)
                    {                        int posIndex = theGame.GameBoard[i, j];
                        if (-1 == posIndex) posIndex = len * len - 1;  //更新隐藏图块       
                        int oriX = posIndex / len;
                        int oriY = posIndex % len;                        PictureBox pb = pbs[posIndex];
                        ig = new Bitmap(pb.Width, pb.Height);
                        g = Graphics.FromImage(ig);
                        Rectangle igRect = new Rectangle(0, 0, ig.Width, ig.Height);
                        g.DrawImage(this.img,
                            igRect,
                            pts[oriX, oriY].X,
                            pts[oriX, oriY].Y,
                            pb.Width,
                            pb.Height,
                            units
                            );
                        //绘制三维边框
                        ControlPaint.DrawBorder(g, igRect, Color.White, ButtonBorderStyle.Outset);
                        pb.Image = ig;                    }
                }            }
            this.ResumeLayout();
        }        //开始游戏
        void startGame()
        {
            theGame.Start();
            int tt = theGame.geshu;
            for (int i = 0; i < tt; i++)
                for (int j = 0; j < tt; j++)
                {
                    if (-1 == theGame.GameBoard[i, j]) continue;
                    pbs[theGame.GameBoard[i, j]].Location = pts[i, j];
                }
            this.pbLast.Visible = false;
            this.panel1.Enabled = true;        }        /// <summary>
        /// 对界面进行重新布局
        /// </summary>
        void resetGameUi()
        {
            int geshu = theGame.geshu;
            theGame.InitGameBoard();
            int width = this.panel1.Width / geshu;
            int height = this.panel1.Height / geshu;            this.SuspendLayout();
            if (pbLast != null && pbLast.Visible == false) pbLast.Visible = true;
            if (pbs == null)//游戏初始化
            {
                pbs = new PictureBox[theGame.GameBoard.Length];
                for (int index = 0; index < pbs.Length; index++)
                {
                    pbLast = new PictureBox();
                    panel1.Controls.Add(pbLast);
                    pbLast.BorderStyle = BorderStyle.None;
                    pbs[index] = pbLast;
                                    }
            }
            
            
            pts = new Point[geshu, geshu];
            for (int i = 0; i < geshu; i++)
                for (int j = 0; j < geshu; j++)
                {
                    pts[i, j] = new Point(width * j, height * i);
                    PictureBox pb = pbs[theGame.GameBoard[i, j]];
                    pb.Width = width;
                    pb.Height = height;
                    pb.Location = pts[i, j];
                }
            this.ResumeLayout();
            this.panel1.Enabled = false;
            this.updateCard();
        }        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            startGame();
        }        private void 选择图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog1.ShowDialog() && File.Exists(openFileDialog1.FileName))
            {
                string filename = openFileDialog1.FileName;
                this.loadImg(filename);
                this.updateCard();            }
        }    }
}Games.cs
using System;
using System.Collections.Generic;
using System.Text;namespace pintu
{    class Game
    {
        public int[,] GameBoard;
        public int geshu = 3;
        //空白图片的位置
        public Position BlankPos = new Position(0, 0);        public Game()
        {        }
        //初始化
        public void InitGameBoard()
        {
            GameBoard = new int[geshu, geshu];
            int pos = 0;
            for (int i = 0; i < geshu; i++)
            {
                for (int j = 0; j < geshu; j++)
                {
                    GameBoard[i, j] = pos++;
                }
            }
        }        public void Start()
        {
            int bound = geshu;
            int maxValue = geshu * geshu - 1;
            //随即生成图片位置
            int[] rndNums;
            rndNums = this.getRandomNums(maxValue);            //生成布局
            int index = 0;
            for (int i = 0; i < geshu; i++)                for (int j = 0; j < geshu & index < maxValue; j++)
                {
                    GameBoard[i, j] = rndNums[index++];
                }            GameBoard[geshu - 1, geshu - 1] = -1;
            BlankPos.X = BlankPos.Y = geshu - 1;
        }        //得到随即数组
        public int[] getRandomNums(int max)
        {
            int[] result = new int[max];
            for (int i = 0; i < max; i++)
            {
                result[i] = -1;            }
            Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < max; i++)
            {
                int pos = 0;
                do
                {
                    pos = rnd.Next(max);                } while (Array.IndexOf<int>(result, pos) != -1);
                result[i] = pos;
            }
            return result;
        }        public struct Position
        {
            public int X;
            public int Y;
            public Position(int x, int y)
            {
                X = x;
                Y = y;
            }
        }
    }
}