Skip to content

Create an image puzzle

Bahar Jalali

Asked Apr 5, 2009, 12:30 AM EDT1 answer
Share
LinkedInX
hi every one
how can i create a puzzle in an image?
i mean , i want divide an image into several part and user must drags and drops then to the true location, how can i do it in c#?

thx

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    hi
    nobody don't know ?
    please help with some code or link...

    or a little solution...

    i found a simple code in vb6 and convert it to c#
    but that has error:


    //********************** vb 6 codes*******************

    Option Explicit

    Dim second As Integer
    Dim a As Boolean
    Dim b As Long
    Dim g As String

    Private Sub cmdcorrect_Click()

    Dim i As Integer
    Dim strfilename As String

    a = False

            For i = 0 To 8
           
                strfilename = App.Path + "\" + Trim(Str(i)) + ".bmp"
                img(i).Picture = LoadPicture(strfilename)
                img(i).Tag = i
               
            Next
           
    second = 0

    cmdplay.Enabled = True
    cmdcorrect.Enabled = False

    End Sub

    Private Sub cmdplay_Click()

    Dim z As Integer
    Dim z2 As Integer
    Dim i As Integer

    Call cmdcorrect_Click

        For i = 0 To 8
       
            Randomize
            z = 8 * Rnd
            z2 = 8 * Rnd
            imgtemp.Picture = img(z).Picture
            img(z).Picture = img(z2).Picture
            img(z2).Picture = imgtemp.Picture
            imgtemp.Tag = img(z).Tag
            img(z).Tag = img(z2).Tag
            img(z2).Tag = imgtemp.Tag
           
        Next
       
    a = True
    b = Timer

    Timer1.Enabled = True
    cmdcorrect.Enabled = False
    img(0).Enabled = True
    img(1).Enabled = True
    img(2).Enabled = True
    img(3).Enabled = True
    img(4).Enabled = True
    img(5).Enabled = True
    img(6).Enabled = True
    img(7).Enabled = True
    img(8).Enabled = True

    End Sub

    Private Sub Command1_Click()

    End

    End Sub

    Private Sub Form_Activate()

    'Skin1.ApplySkin hWnd

    End Sub

    Private Sub Form_Load()

    cmdplay.Enabled = False
    img(0).Enabled = False
    img(1).Enabled = False
    img(2).Enabled = False
    img(3).Enabled = False
    img(4).Enabled = False
    img(5).Enabled = False
    img(6).Enabled = False
    img(7).Enabled = False
    img(8).Enabled = False

    End Sub

    Private Sub img_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)

    Dim strtime As String

        If a = False Then
       
            Exit Sub
           
        End If
       
    imgtemp.Picture = img(Index).Picture
    img(Index).Picture = Source.Picture
    Source.Picture = imgtemp.Picture
       
    imgtemp.Tag = img(Index).Tag
    img(Index).Tag = Source.Tag
    Source.Tag = imgtemp.Tag

         If CheckCorrect = True Then
       
            strtime = Str(Int(Timer - b))
            MsgBox ("ÒãÇä ӁÑí ÔÏå : " + strtime + " ËÇäíå "), vbInformation, "ÞåÑãÇä"

            cmdcorrect.Enabled = True
            Command1.Enabled = True
           
            a = False
            img(0).Enabled = False
            img(1).Enabled = False
            img(2).Enabled = False
            img(3).Enabled = False
            img(4).Enabled = False
            img(5).Enabled = False
            img(6).Enabled = False
            img(7).Enabled = False
            img(8).Enabled = False

         End If
    End Sub

    Private Function CheckCorrect() As Boolean

    Dim i As Integer

        For i = 0 To 8
       
            If img(i).Tag <> i Then
           
               CheckCorrect = False
    Exit Function

            End If
           
         Next
        
    CheckCorrect = True
    second = 0

    End Function

    Private Sub Timer1_Timer()

        If a = True Then
       
            second = second + 1
            Label1.Caption = second

                If second = 15 Then Timer1.Enabled = False
                If Timer1.Enabled = False Then
               
                    MsgBox ("æÞÊ ÔãÇ ÊãÇã ÔÏ"), vbCritical, "ÇÊãÇã ÒãÇä"
                    cmdplay.Enabled = False
                    Command1.Enabled = True
                    cmdcorrect.Enabled = True
                    img(0).Enabled = False
                    img(1).Enabled = False
                    img(2).Enabled = False
                    img(3).Enabled = False
                    img(4).Enabled = False
                    img(5).Enabled = False
                    img(6).Enabled = False
                    img(7).Enabled = False
                    img(8).Enabled = False

                 End If
        End If
    End Sub

    //****************** end


    and converted codes  to c#  is :

    // *********************** c# codes ******************
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;

    namespace ProjectName
    {
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
       private System.Windows.Forms.ACTIVESKINLibCtl.Skin Skin1;
       private System.Windows.Forms.Timer Timer1;
       private System.Windows.Forms.Button cmdcorrect;
       private System.Windows.Forms.Button cmdplay;
       private System.Windows.Forms.Button Command1;
       private System.Windows.Forms.Label Label1;
       private System.Windows.Forms.PictureBox imgtemp;
       private System.Windows.Forms.PictureBox img8;
       private System.Windows.Forms.PictureBox img7;
       private System.Windows.Forms.PictureBox img6;
       private System.Windows.Forms.PictureBox img5;
       private System.Windows.Forms.PictureBox img4;
       private System.Windows.Forms.PictureBox img3;
       private System.Windows.Forms.PictureBox img2;
       private System.Windows.Forms.PictureBox img1;
       private System.Windows.Forms.PictureBox img0;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
          // Required for Windows Form Designer support
          InitializeComponent();

          // TODO: Add any constructor code after InitializeComponent call
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
          if( disposing )
          {
            if (components != null)
            {
              components.Dispose();
            }
          }
          base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          this.Skin1 = new System.Windows.Forms.ACTIVESKINLibCtl.Skin();
          this.Timer1 = new System.Windows.Forms.Timer();
          this.cmdcorrect = new System.Windows.Forms.Button();
          this.cmdplay = new System.Windows.Forms.Button();
          this.Command1 = new System.Windows.Forms.Button();
          this.Label1 = new System.Windows.Forms.Label();
          this.imgtemp = new System.Windows.Forms.PictureBox();
          this.img8 = new System.Windows.Forms.PictureBox();
          this.img7 = new System.Windows.Forms.PictureBox();
          this.img6 = new System.Windows.Forms.PictureBox();
          this.img5 = new System.Windows.Forms.PictureBox();
          this.img4 = new System.Windows.Forms.PictureBox();
          this.img3 = new System.Windows.Forms.PictureBox();
          this.img2 = new System.Windows.Forms.PictureBox();
          this.img1 = new System.Windows.Forms.PictureBox();
          this.img0 = new System.Windows.Forms.PictureBox();
          this.SuspendLayout();
          //
          // Skin1
          //
          this.Skin1.Name = "Skin1";
          this.Skin1.Location = new System.Drawing.Point(264, 128);
    //      this.Skin1.OleObjectBlob = "Form1.frx":0000;
          //
          // Timer1
          //
          this.Timer1.Enabled = false;
          this.Timer1.Interval = 1000;
          //
          // cmdcorrect
          //
          this.cmdcorrect.Name = "cmdcorrect";
          this.cmdcorrect.Text = " ";
          this.cmdcorrect.Font = new System.Drawing.Font("B Traffic",9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));;
          this.cmdcorrect.Size = new System.Drawing.Size(81, 33);
          this.cmdcorrect.Location = new System.Drawing.Point(8, 208);
          this.cmdcorrect.TabIndex = 2;
          //
          // cmdplay
          //
          this.cmdplay.Name = "cmdplay";
          this.cmdplay.Text = " ";
          this.cmdplay.DialogResult = System.Windows.Forms.DialogResult.OK;
          this.cmdplay.Font = new System.Drawing.Font("B Traffic",9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));;
          this.cmdplay.Size = new System.Drawing.Size(81, 33);
          this.cmdplay.Location = new System.Drawing.Point(168, 208);
          this.cmdplay.TabIndex = 1;
          //
          // Command1
          //
          this.Command1.Name = "Command1";
          this.Command1.Text = "";
          this.Command1.Font = new System.Drawing.Font("B Traffic",9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));;
          this.Command1.Size = new System.Drawing.Size(81, 33);
          this.Command1.Location = new System.Drawing.Point(88, 208);
          this.Command1.TabIndex = 0;
          //
          // Label1
          //
          this.Label1.Name = "Label1";
          this.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
          this.Label1.AutoSize = true;
          this.Label1.Text = "00";
          this.Label1.Size = new System.Drawing.Size(13, 13);
          this.Label1.Location = new System.Drawing.Point(264, 176);
          this.Label1.TabIndex = 3;
          this.Label1.Visible = false;
          //
          // imgtemp
          //
          this.imgtemp.Name = "imgtemp";
          this.imgtemp.Size = new System.Drawing.Size(81, 65);
          this.imgtemp.Location = new System.Drawing.Point(264, 8);
    //      this.imgtemp.Stretch = -1;
          this.imgtemp.Visible = false;
          //
          // img8
          //
          this.img8.Name = "img8";
          this.img8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img8.DragMode = 1;
          this.img8.Size = new System.Drawing.Size(81, 65);
          this.img8.Location = new System.Drawing.Point(168, 136);
    //      this.img8.Stretch = -1;
          //
          // img7
          //
          this.img7.Name = "img7";
          this.img7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img7.DragMode = 1;
          this.img7.Size = new System.Drawing.Size(81, 65);
          this.img7.Location = new System.Drawing.Point(88, 136);
    //      this.img7.Stretch = -1;
          //
          // img6
          //
          this.img6.Name = "img6";
          this.img6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img6.DragMode = 1;
          this.img6.Size = new System.Drawing.Size(81, 65);
          this.img6.Location = new System.Drawing.Point(8, 136);
    //      this.img6.Stretch = -1;
          //
          // img5
          //
          this.img5.Name = "img5";
          this.img5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img5.DragMode = 1;
          this.img5.Size = new System.Drawing.Size(81, 65);
          this.img5.Location = new System.Drawing.Point(168, 72);
    //      this.img5.Stretch = -1;
          //
          // img4
          //
          this.img4.Name = "img4";
          this.img4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img4.DragMode = 1;
          this.img4.Size = new System.Drawing.Size(81, 65);
          this.img4.Location = new System.Drawing.Point(88, 72);
    //      this.img4.Stretch = -1;
          //
          // img3
          //
          this.img3.Name = "img3";
          this.img3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img3.DragMode = 1;
          this.img3.Size = new System.Drawing.Size(81, 65);
          this.img3.Location = new System.Drawing.Point(8, 72);
    //      this.img3.Stretch = -1;
          //
          // img2
          //
          this.img2.Name = "img2";
          this.img2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img2.DragMode = 1;
          this.img2.Size = new System.Drawing.Size(81, 65);
          this.img2.Location = new System.Drawing.Point(168, 8);
    //      this.img2.Stretch = -1;
          //
          // img1
          //
          this.img1.Name = "img1";
          this.img1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img1.DragMode = 1;
          this.img1.Size = new System.Drawing.Size(81, 65);
          this.img1.Location = new System.Drawing.Point(88, 8);
    //      this.img1.Stretch = -1;
          //
          // img0
          //
          this.img0.Name = "img0";
          this.img0.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    //      this.img0.DragMode = 1;
          this.img0.Size = new System.Drawing.Size(81, 65);
          this.img0.Location = new System.Drawing.Point(8, 8);
    //      this.img0.Stretch = -1;
          //
          // Form1
          //
          this.Controls.AddRange(new System.Windows.Forms.Control[]
          {
                this.Skin1,
                this.cmdcorrect,
                this.cmdplay,
                this.Command1,
                this.Label1,
                this.imgtemp,
                this.img8,
                this.img7,
                this.img6,
                this.img5,
                this.img4,
                this.img3,
                this.img2,
                this.img1,
                this.img0
          });
          this.Name = "Form1";
          this.BackColor = System.Drawing.Color.FromArgb(-4144897);
          this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
          this.ClientSize = new System.Drawing.Size(257, 249);
          this.MaximizeBox = false;
          this.MinimizeBox = false;
          this.ShowInTaskbar = false;
          this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
          this.ResumeLayout(false);
        }
        #endregion

        private short second;
        private bool a;
        private int b;
        private string g;
        private short i;
        private string strfilename;
        private short z;
        private short z2;
        private short i;
        private string strtime;
        private short i;

        private void cmdcorrect_Click()
        {


          a = false;

          For i = 0 To 8;

          strfilename = App.Path + "\" + Trim(Str(i)) + ".bmp";
          img(i).Picture = LoadPicture(strfilename);
          img(i).Tag = i;

          Next;

          second = 0;

          cmdplay.Enabled = true;
          cmdcorrect.Enabled = false;

        }
        private void cmdplay_Click()
        {


          Call cmdcorrect_Click;

          For i = 0 To 8;

          Randomize;
          z = 8 * Rnd;
          z2 = 8 * Rnd;
          imgtemp.Picture = img(z).Picture;
          img(z).Picture = img(z2).Picture;
          img(z2).Picture = imgtemp.Picture;
          imgtemp.Tag = img(z).Tag;
          img(z).Tag = img(z2).Tag;
          img(z2).Tag = imgtemp.Tag;

          Next;

          a = true;
          b = Timer;

          Timer1.Enabled = true;
          cmdcorrect.Enabled = false;
          img(0).Enabled = true;
          img(1).Enabled = true;
          img(2).Enabled = true;
          img(3).Enabled = true;
          img(4).Enabled = true;
          img(5).Enabled = true;
          img(6).Enabled = true;
          img(7).Enabled = true;
          img(8).Enabled = true;

        }
        private void Command1_Click()
        {

        }
        private void Form_Activate()
        {

          Skin1.ApplySkin hWnd;

        }
        private void Form_Load()
        {

          cmdplay.Enabled = false;
          img(0).Enabled = false;
          img(1).Enabled = false;
          img(2).Enabled = false;
          img(3).Enabled = false;
          img(4).Enabled = false;
          img(5).Enabled = false;
          img(6).Enabled = false;
          img(7).Enabled = false;
          img(8).Enabled = false;

        }
        private void img_DragDrop    {


          if ( a = false )
          {;

          Exit Sub;

          };

          imgtemp.Picture = img(Index).Picture;
          img(Index).Picture = Source.Picture;
          Source.Picture = imgtemp.Picture;

          imgtemp.Tag = img(Index).Tag;
          img(Index).Tag = Source.Tag;
          Source.Tag = imgtemp.Tag;

          if ( CheckCorrect = true )
          {;

          strtime = Str(Int(Timer - b));
          MsgBox (" ?  : " + strtime + "  "), vbInformation, "";

          cmdcorrect.Enabled = true;
          Command1.Enabled = true;

          a = false;
          img(0).Enabled = false;
          img(1).Enabled = false;
          img(2).Enabled = false;
          img(3).Enabled = false;
          img(4).Enabled = false;
          img(5).Enabled = false;
          img(6).Enabled = false;
          img(7).Enabled = false;
          img(8).Enabled = false;

          };
        }
        private As CheckCorrect()
        {


          For i = 0 To 8;

          if ( img(i).Tag <> i )
          {;

          CheckCorrect = false;
          Exit Function;

          };

          Next;

          CheckCorrect = true;
          second = 0;

        }
        private void Timer1_Timer()
        {

          if ( a = true )
          {;

          second = second + 1;
          Label1.Text = second;

          if ( second = 15 )
          {
     Timer1.Enabled = false;
          if ( Timer1.Enabled = false )
          {;

          MsgBox ("   "), vbCritical, " ";
          cmdplay.Enabled = false;
          Command1.Enabled = true;
          cmdcorrect.Enabled = true;
          img(0).Enabled = false;
          img(1).Enabled = false;
          img(2).Enabled = false;
          img(3).Enabled = false;
          img(4).Enabled = false;
          img(5).Enabled = false;
          img(6).Enabled = false;
          img(7).Enabled = false;
          img(8).Enabled = false;

          };
          };
        }
      }
    }

    // **************************** end of codes ******

    thx
    0