If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

TV static noise animation-adding random generator

Hi,

First forum posting.

I am a newbie (retired social worker) using Visual Basic 2010 Express edition and need some help with an animation for an eLearning lesson. The animation illustrates a type of white noise (like TV static) called dynamic visual noise. I've got much of the code worked out (with a lot of help) but I discovered that the speed of the animation, using this code is wrong and need some help to fix the code, get it working correctly.

The animation has a 640 X 640 canvas, with 8 X 8 white and black dots filling it using 80 across and 80 down, for a total of 6400 dots. Some of these (approximately 400) change every second (1000 Ms) from white to black or black to white. These 400 dots need to be selected randomly every second.

The VB code I am using is missing the ability to randomly select the 400 dots per second which are the dots to be changed (white to black or black to white).

Here is the current (commented) code that needs an addition of a random generator for the 400 dots every second that will be changed:

Code:

Option Strict On
Public Class Form1
Private gDotSize As Integer = 8
Private gCanvasSize As Integer = 640
Private gDelay As Integer = 100 'ms
Private gProceed As Boolean = False
Private gImage As New System.Drawing.Bitmap(gCanvasSize, gCanvasSize)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetClientSizeCore(gCanvasSize, gCanvasSize)
Me.Text = "Click the form to start and stop"
'Set double-buffering so that graphics may be invalidated without causing flickering.
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
End Sub
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
If gProceed Then
gProceed = False
Else
gProceed = True
Dim vThread As New System.Threading.Thread(AddressOf GenerateStatic_Background)
With vThread
.IsBackground = True
.Name = "Thread for generating static"
End With
vThread.Start(New Object) 'You could pass an object into the thread here if you wanted.
End If
End Sub
Private Sub GenerateStatic_Background(ByVal Arg As System.Object)
'The canvas is square, so how many rows and columns will there be?
Dim vRows As Integer = Convert.ToInt32(gCanvasSize / gDotSize)
Dim vCols As Integer = Convert.ToInt32(gCanvasSize / gDotSize)
'How many dots will there be total?
Dim vTotalDots As Integer = Convert.ToInt32(vRows * vCols)
'Create blank image to draw on.
Dim vImg As New System.Drawing.Bitmap(gCanvasSize, gCanvasSize)
Using vGr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(vImg)
'Instance random generator object and seed data.
Dim vGen As New System.Security.Cryptography.RNGCryptoServiceProvider
'Prepare an empty array of bytes which will be used to store random seed data.
'These random bytes will be converted to a 32-bit integer. Int32's are 4 bytes
'in length; therefore the array will be 4 bytes in size. Remember, when
'dimensioning static arrays, we do not specify the "length" of the array like
'in C++, but instead we specify the "upper bounds" a.k.a. the last index of
'the array, which would be 3 (0-3).
Dim vSeed(3) As Byte
'Fill the empty array with a cryptographically strong sequence of random values.
vGen.GetBytes(vSeed)
'Instance a Random object using the random bytes (converted to an integer) as a seed.
Dim vRand As Random = New Random(BitConverter.ToInt32(vSeed, 0))
'Variables for choosing the color.
Dim vCurrentRand As Integer = 0 'Black is 0; white is 1
Dim vCurrentColor As System.Drawing.Color = System.Drawing.Color.Black
'Do until told to stop by the user.
While gProceed
'Wait the desired period.
System.Threading.Thread.Sleep(gDelay)
'We will start on the first row on the left, work right across the columns, then drop
'down to the second next row (back to the left), and again work right across the
'columns, and so on...
Dim vCurrentDot As New System.Drawing.Rectangle(0, 0, gDotSize, gDotSize)
Dim vCurrentRow As Integer = 0
Dim vCurrentCol As Integer = 0
'Iterate each dot... come up with a random black or white state, and draw it to the image.
For i As Integer = 0 To vTotalDots - 1
'Generate random black or white state for this dot.
vCurrentRand = vRand.Next(0, 2) 'Either 0 or 1
If vCurrentRand = 0 Then vCurrentColor = System.Drawing.Color.Black Else vCurrentColor = System.Drawing.Color.White
'Draw this dot.
vGr.FillRectangle(New System.Drawing.SolidBrush(vCurrentColor), vCurrentDot)
'Increment the current dot for the next iteration.
vCurrentCol += 1 : vCurrentDot.X += gDotSize
If vCurrentCol = vCols Then
vCurrentCol = 0 : vCurrentDot.X = 0
vCurrentRow += 1 : vCurrentDot.Y += gDotSize
End If
Next
'Update the UI.
If Me.InvokeRequired Then
Try
Me.Invoke(New DrawUpdate_Delegate(AddressOf DrawUpdate), CType(vImg, System.Object))
Catch ex As Exception
'Error cuz you ended the program without allowing the thread to stop.
'To prevent this error, you could set gProceed = False in the Form_Closing event so the thread can end naturally.
'This Try block just prevents the error message.
End Try
Else
DrawUpdate(CType(vImg, System.Object))
End If
End While
End Using
End Sub
Private Delegate Sub DrawUpdate_Delegate(ByVal Arg As System.Object)
Private Sub DrawUpdate(ByVal Arg As System.Object)
gImage = DirectCast(Arg, System.Drawing.Bitmap)
Me.Invalidate()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImageUnscaled(gImage, 0, 0)
End Sub
End Class

I would appreciate any code suggestions to add the random generator (described above) and where to add it in the above code. It would also be a huge help if I can change the specific number of random dots so I can try values ranging from 390-400 dots per second to see which value best replicates the precise animation speed needed.

Re: TV static noise animation-adding random generator

Hi David,

Thank you for this suggestion. I suspect that it will not produce the smooth animation seen in the Flash demo which illustrates the "standard." I think I will need to figure out how to add the random.net feature that you mentioned earlier. Or, some timer that from the randomly selected dots does a further random selection of a maximum of 40 of these every 100 ms. Something like that. Unfortunately, my thinking is far far ahead of my ability to implement this in VB code. :-)

Re: TV static noise animation-adding random generator

Does it have to be coded like this? As in, is this an assignment?

Really, this is making it much more difficult on yourself. Any graphic editor can create 3 separate images of random noise. Then you just have to cycle through them in a loop. That would take you about 3 minutes to code.

If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Re: TV static noise animation-adding random generator

Hi Peej,

This is not an assignment, but the final animation has to precisely replicate the standard style, appearance and change speed for "dynamic visual noise" in order to be an effective example and exercise.

So, I think that a code which can randomly choose 400 dots per second, change these from white to black or black to white, on a 640X640 canvas with 80 rows across and 80 rows down of white and black dots (50% of each to start), totaling 6400 will work. The code posted above does much of this, but does not randomly select 400 per second (or 40 per 100 ms) for changing. So, the code would need to add this and change anything else in it to allow this random generator to work properly. As a beginner, I've tried to do this, but I have not been successful and need help.

But, if there is an easier design that accomplishes the same result, that would be great!

One problem is that you has the code Sleep for 100 ms.. meaning at best you could get 10 FPS ... (not quite what you were looking for...)
reducing to 15 ms can increase it to ~ 60 FPS. However with the method your using, if all the code takes 10 ms to complete then that gets added to the 15 ms sleep time.. so you have 25 ms between Frames..

If you follow my article, the timer method explained there can also trigger every 15 ms, however the code execution time is not added to the frame time.. unless code execution time is longer than the timer.

The other thing is that using sleep functions inside loops is not the generally accepted method.

It will also eliminate the problem you put this code in for

Code:

If Me.InvokeRequired Then
Try
Me.Invoke(New DrawUpdate_Delegate(AddressOf DrawUpdate), CType(vImg, System.Object))
Catch ex As Exception
'Error cuz you ended the program without allowing the thread to stop.
'To prevent this error, you could set gProceed = False in the Form_Closing event so the thread can end naturally.
'This Try block just prevents the error message.
End Try
Else
DrawUpdate(CType(vImg, System.Object))
End If

Re: TV static noise animation-adding random generator

Hi,

Thank you very much for this.

I have Visual Basic 2010 Express edition (free version from MS), so I am not sure how to open and run this. So far, I extracted using 7-Zip and tried to run different files in VB 2020 Express, without getting the project to run. I can see some of the source code, but when I tried to use the project conversion wizard, it had many errors, failures to convert.

Is it possible for you to send, or tell me how to use, the code that I would place in a new Form1? Then I can use this in a new project, see if I can get it to run.