'分割圖片
'自備圖片大小:320*270 配合畫面大小
'以PaintPicture 繪至動態圖片物件,並存入ImageList
'控制項:
'開檔commDialog1
'原圖:image (Name:oPicture)
'拼圖區集合物件:PictureBox (Name:LastPic)
'分割陣列圖基本元件:PictureBox (Name:MovingPic(0))
'動態圖物件:MovingPic(1~k)
'分割圖儲存:ImageList1


Dim RowNUM, ColNUM As Integer
Dim totPic As Integer
Private Sub btnOpenFile_Click()
fileDialog.Filter = "開啟圖檔 ( *.bmp;*.jpg;*.gif) | *.bmp; *.jpg; *.gif;"
fileDialog.ShowOpen
oPicture.Picture = LoadPicture(fileDialog.FileName)
End Sub
Private Sub btnReset_Click()
For i = 1 To RowNUM * ColNUM
Unload MovingPic(i) '載出
Next
Call initPuzzle
End Sub
Private Sub btnStart_Click()
Dim ww, hh As Single
Dim putWidth, putHeight, cutWidth, cutHeight As Single
btnStart.Enabled = False '
btnReset.Enabled = True
btnOpenFile.Enabled = False
Combo1.Enabled = False
oPicture.Visible = False '隱藏原圖
LastPic.Visible = True '顯示分割圖
ww = oPicture.Width '原圖大小
hh = oPicture.Height
cutWidth = ww / ColNUM '原圖分割後之寬度
cutHeight = hh / RowNUM '原圖分割後之高度
putWidth = LastPic.Width / ColNUM '每一併圖的寬度
putHeight = LastPic.Height / RowNUM '每一併圖的高度
MovingPic(0).Visible = False '隱藏基本拼圖元件
'----------------------------------------------------------------------
'分割原圖,共RowNUM*ColNUM 小塊
'----------------------------------------------------------------------
For i = 1 To RowNUM
For j = 1 To ColNUM
k = (i - 1) * ColNUM + j
Load MovingPic(k)
MovingPic(k).Width = putWidth '設定動態分割圖之分割大小
MovingPic(k).Height = putHeight
MovingPic(k).Visible = True '屬性設定
MovingPic(k).AutoRedraw = True
MovingPic(k).AutoSize = True
'--------------------------------------------------------------
px = (j - 1) * cutWidth '原圖分割之起點座標
py = (i - 1) * cutHeight
'--------------------------------------------------------------
'將分割後之圖片繪製於動態分割圖裡,並逐一顯示於相對座標
MovingPic(k).PaintPicture oPicture.Picture, 0, 0, putWidth, putHeight, px, py, cutWidth, cutHeight
MovingPic(k).Move (j - 1) * putWidth, (i - 1) * putHeight
'--------------------------------------------------------------
'將分割後之圖片存入imageList
ImageList1.ListImages.Add k, , MovingPic(k).Image
Next
Next
End Sub
Private Sub initPuzzle()
btnStart.Enabled = True '
btnReset.Enabled = False
btnOpenFile.Enabled = True
Combo1.Enabled = True
oPicture.Visible = True
LastPic.Visible = False
btnStart.Enabled = True
btnReset.Enabled = False
Combo1.AddItem "3 x 3 "
Combo1.AddItem "3 x 4 "
Combo1.AddItem "4 x 3 "
Combo1.AddItem "4 x 4 "
Combo1.AddItem "5 x 5 "
Combo1.ListIndex = 3 '預設 4 x 4
End Sub
Private Sub Combo1_Click()
n = Combo1.ListIndex
Select Case n
Case 0
ColNUM = 3
RowNUM = 3
Case 1
ColNUM = 3
RowNUM = 4
Case 2
ColNUM = 4
RowNUM = 3
Case 3
ColNUM = 4
RowNUM = 4
Case 4
ColNUM = 5
RowNUM = 5
End Select
End Sub
Private Sub Form_Load()
Me.ScaleMode = 1
Call initPuzzle '初始化
End Sub