One Picture Multiple Picturebox's

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

One Picture Multiple Picturebox's
M1z23R
So, as the title says, i'll teach you how to place one picture in a few picturebox's. I needed this for my 'puzzle - jigsaw' game in MMC. I made code my self and since another task is given, i'll post tutorial about it.

Do as listed:

-Create new project
-Create 9 picturebox's with same size, lets say 150,150
-Place picturebox's next to each other !
-Create 1 button and change it's text to "Choose" or "Browse" "Pick" or whatever

Open button click event and add this code
Code: Select all
   Dim opfd As New OpenFileDialog
        opfd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"

        opfd.ShowDialog()
        If opfd.FileName = "" Then Exit Sub
        Dim img As Bitmap = Image.FromFile(opfd.FileName)
        NewBmp = New Bitmap(img, 450, 450)

Also add this declaration above the Sub
Code: Select all
Dim NewBmp As Bitmap
So that it looks like this
Code: Select all
   Dim NewBmp As Bitmap
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ''
        Dim opfd As New OpenFileDialog
        opfd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"

        opfd.ShowDialog()
        If opfd.FileName = "" Then Exit Sub
        Dim img As Bitmap = Image.FromFile(opfd.FileName)
        NewBmp = New Bitmap(img, 450, 450)
End Sub
So, this thing
Code: Select all
 NewBmp = New Bitmap(img, 450, 450)
What this means is sets global NewBmp to new size (450,450)
Chose whatever size you want, but if you've set 3x3 picturebox's size as 150,150 it's whole size is 450,450.

Now, add this code for each picturebox's paint event
Code: Select all
  Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, 0, 0)
    End Sub
Now, this is only for first picture box. You see this:
Code: Select all
 e.Graphics.DrawImage(NewBmp, 0, 0)
Now, this draws graphic at specified coordinates(0,0), but for next picturebox, it will be something else
Code: Select all
Private Sub PictureBox2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -150, 0)
    End Sub
The X coordinate is negative, because the picture should be drawn at point before the picturebox, now the part for that picturebox should be right.

Here is code for all picturebox's
Code: Select all

   Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, 0, 0)
    End Sub

    Private Sub PictureBox2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -150, 0)
    End Sub

    Private Sub PictureBox3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox3.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -300, 0)
    End Sub

    Private Sub PictureBox4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox4.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, 0, -150)
    End Sub

    Private Sub PictureBox5_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox5.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -150, -150)
    End Sub

    Private Sub PictureBox6_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox6.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -300, -150)
    End Sub

    Private Sub PictureBox7_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox7.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, 0, -300)
    End Sub

    Private Sub PictureBox8_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox8.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -150, -300)
    End Sub

    Private Sub PictureBox9_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox9.Paint
        On Error Resume Next
        e.Graphics.DrawImage(NewBmp, -300, -300)
    End Sub
Now, to button choose just add one more thing, that will draw our picture :
Code: Select all
Dim PictureBoxs() as picturebox = {PictureBox1, PictureBox2,PictureBox3,PictureBox4,PictureBox5,PictureBox6,PictureBox7,PictureBox8,PictureBox9}
'This gets all pictureBox's in a collection

For each pb as PictureBox in PictureBoxs
pb.Refresh
Next
'This refreshes each picturebox and rises Paint Event. Now our picture will be drawn.
Hope you like it, if you find any bugs please let me know ;)
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Nice tutorial M1z23R! :)
Keep it up cooll;
You used the same thing for the MMDC?
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Yes, for Jigsaw game, but i added picture box moving and some other stuff :)
3 posts Page 1 of 1
Return to “Tutorials”