0 Replies - 1726 Views - Last Post: 19 February 2014 - 07:52 AM

#1 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 208
  • View blog
  • Posts: 1,288
  • Joined: 11-May 12

Split image into grid for individual bitmaps

Post icon  Posted 19 February 2014 - 07:52 AM

I recently figured out a way to take 1 image and transform it to another image in an interesting way.

put 2 images on a form and a button

Main class
Public Class Form1
Private Sub btnFuse_Click(...)
' declare the size of squares (in pixels) of your grid
Dim xStep As Integer = 10
Dim yStep As Integer = 10
' this is used if you want to see the grid as it is drawn
Dim drawGrid As boolean = False
' I created a class to hold the bitmap information
Dim bmpList As New List(Of clsBMPInfo)

' loop through the picture getting the information for the individual bitmaps
For i As Integer = 0 to PictureBox1.Image.Width Step xStep
  For j As Integer = 0 to PictureBox1.Image.Height Step yStep
    Dim bmpInfo As New clsBMPInfo
    bmpInfo.BMPLocation = New Point(i, j)
    bmpInfo.BMPSize = New Size(xStep, yStep)

' randomize the list of bmpInfo can be taken out if you just want it to look like its scrolling from left to right or top to bottom if you flip width and height loops

' create a random number for how many times to randomize the list
Dim loopRnd As New Random(Now.Millisecond)
Dim loopCount As Integer = loopRnd.Next(1, 50)
' create a sleeptime so every random numbers seed will be different within the loop
Dim sleepTime As Integer = loopRnd.Next(0, 900)
For i As Integer = 0 to bmpList.Count - 1
  ' create 2 random numbers for the swap positions in the list
  Dim rnd As New Random(Now.Millisecond + sleepTime)
  Dim rnd2 As New Random(Now.Millisecond * sleepTime)
  Dim b1 As Integer = rnd.Next(0, bmpList.Count - 1)
  Dim b2 As Integer = rnd2.Next(0, bmpList.Count - 1)
  ' create a temporary object to hold first position information
  Dim temp As clsBMPInfo = bmpList(b1)
  ' perform swap
  bmpList(b1) = bmpList(b2)
  bmpList(b2) = temp
  ' change sleepTime to new random number
  sleepTime = loopRnd.Next(0, 900)

' loop through the list and get the bitmaps at the current position and change them in the 1st image to the second image
For Each b As clsBMPInfo In bmpList
  ' create a bitmap of the image you want to grab from
  Dim fromBM As New Bitmap(PictureBox2.Image)
  ' create a bitmap of the image you want to replace to
  Dim toBM As New Bitmap(PictureBox1.Image)
  ' create a graphics object to draw to
  Dim gr As Graphics = Graphics.FromImage(toBM)
  ' create 2 rectangles for the grid pieces
  Dim fromRect As New Rectangle(b.BMPLocation, b.BMPSize)
  Dim toRect As New Rectangle(b.BMPLocation, b.BMPSize)
  ' draw to the graphics object
  gr.DrawImage(fromBM, toRect, fromRect, GraphicsUnit.Pixel)
  ' draw grid if necessary
  If drawGrid
    gr.DrawRectangle(Pens.Red, fromRect)
  End If

  ' wait for a bit, so you can see the animation
  ' set the image of picturebox1 to the bitmap
  PictureBox1.Image = toBM
  ' refresh the picturebox
End Sub
End Class

Public Class clsBMPInfo
    Dim _bmpLocation As Point
    Dim _bmpSize As Size
    Dim _Img As Image

    Public Sub New()

    End Sub

    Public Property BMPLocation As Point
            Return _bmpLocation
        End Get
        Set(value As Point)
            _bmpLocation = value
        End Set
    End Property

    Public Property BMPSize As Size
            Return _bmpSize
        End Get
        Set(value As Size)
            _bmpSize = value
        End Set
    End Property

    Public Property BMPImage As Image
            Return _Img
        End Get
        Set(value As Image)
            _Img = value
        End Set
    End Property
End Class

Posted Image

Posted Image

Is This A Good Question/Topic? 2
  • +

Page 1 of 1