Private Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
Dim rand As New Random()
' Draw the source image.
Dim wid As Integer = picSource.ClientSize.Width
Dim hgt As Integer = picSource.ClientSize.Height
Dim src_bm As New Bitmap(wid, hgt)
Using gr As Graphics = Graphics.FromImage(src_bm)
gr.Clear(Color.White)
' Draw the text.
Using fnt As New Font("Times New Roman", 20, _
FontStyle.Bold, GraphicsUnit.Pixel)
Using sf As New StringFormat()
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
gr.DrawString(txtString.Text, fnt, _
Brushes.Black, wid / 2, hgt / 2, sf)
End Using
End Using
' Draw a grid if desired.
If chkDrawGrid.Checked Then
For x As Integer = 0 To wid Step 20
For y As Integer = 0 To hgt Step 20
gr.DrawLine(Pens.Red, x, 0, x, hgt)
gr.DrawLine(Pens.Red, 0, y, wid, y)
Next y
Next x
End If
End Using
picSource.Image = src_bm
Dim mag0 As Single = rand.Next(2, 6)
Dim per0 As Single = rand.Next(20, 30)
Dim mag1 As Single = rand.Next(3, 6)
Dim per1 As Single = rand.Next(20, 40)
Dim mag2 As Single = rand.Next(1, 3)
Dim per2 As Single = rand.Next(10, 20)
lblParameters.Text = _
mag0 & ", " & per0 & "; " & _
mag1 & ", " & per1 & "; " & _
mag2 & ", " & per2
' Make the destination bitmap.
Dim bm As New Bitmap(wid, hgt)
For x As Integer = 0 To wid - 1
For y As Integer = 0 To hgt - 1
' Find source (x, y).
Dim sx As Single
Dim sy As Single
InvertXY(mag0, per0, mag1, per1, mag2, per2, x, _
y, sx, sy)
' Interpolate.
Dim ix As Integer = Int(sx)
Dim iy As Integer = Int(sy)
If ix < 0 OrElse ix >= wid - 1 OrElse _
iy < 0 OrElse iy >= hgt - 1 _
Then
bm.SetPixel(x, y, Color.White)
Else
Dim dx0 As Single = sx - ix
Dim dy0 As Single = sy - iy
Dim dx1 As Single = 1 - dx0
Dim dy1 As Single = 1 - dy0
Dim r As Integer = _
dy1 * _
(dx1 * src_bm.GetPixel(ix, iy).R + _
dx0 * src_bm.GetPixel(ix + 1, _
iy).R) + _
dy0 * _
(dx1 * src_bm.GetPixel(ix, iy + _
1).R + _
dx0 * src_bm.GetPixel(ix + 1, iy + _
1).R)
Dim g As Integer = _
dy1 * _
(dx1 * src_bm.GetPixel(ix, iy).G + _
dx0 * src_bm.GetPixel(ix + 1, _
iy).G) + _
dy0 * _
(dx1 * src_bm.GetPixel(ix, iy + _
1).G + _
dx0 * src_bm.GetPixel(ix + 1, iy + _
1).G)
Dim b As Integer = _
dy1 * _
(dx1 * src_bm.GetPixel(ix, iy).B + _
dx0 * src_bm.GetPixel(ix + 1, _
iy).B) + _
dy0 * _
(dx1 * src_bm.GetPixel(ix, iy + _
1).B + _
dx0 * src_bm.GetPixel(ix + 1, iy + _
1).B)
bm.SetPixel(x, y, Color.FromArgb(255, r, g, _
b))
End If
Next y
Next x
' Draw random lines if desired.
If chkRandomLines.Checked Then
Using gr As Graphics = Graphics.FromImage(bm)
For i As Integer = 1 To 10
gr.DrawLine(Pens.Black, rand.Next(0, wid), _
0, rand.Next(0, wid), hgt)
gr.DrawLine(Pens.White, rand.Next(0, wid), _
0, rand.Next(0, wid), hgt)
Next i
End Using
End If
picDest.Image = bm
End Sub
|