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.
Thanks very much for your help.
Kind Regards,
saratogacoach
Last edited by saratogacoach; February 21st, 2011 at 08:23 PM.
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!
Re: TV static noise animation-adding random generator
Hi,
I took a look at Richard Newcombe's animation article as suggested. As a beginner using VB, applying the new drawing method is way to complex for me.
While I understand the basic ideas, for example, moving it out of the loop, implementing this in code, where to put it, what to remove is beyond my skill level at this time.
Re: TV static noise animation-adding random generator
Originally Posted by saratogacoach
Hi,
I took a look at Richard Newcombe's animation article as suggested. As a beginner using VB, applying the new drawing method is way to complex for me.
????????
It's not a new method .. it's almost the same as the one your using..
Change your core code to look like this ... (i've highlighted key changes...)
Code:
Dim vCurrentDot As New System.Drawing.Rectangle(0, 0, gDotSize, gDotSize)
Dim vCurrentRow As Integer = 0
Dim vCurrentCol As Integer = 0
Dim vPens(2) As System.Drawing.Brush
vPens(0) = Brushes.Black
vPens(1) = Brushes.White
'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(vPens(vCurrentRand), 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
then at the top change the delay to 15 ms
Code:
Private gDelay As Integer = 15 'ms
now test your project...
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.
Re: TV static noise animation-adding random generator
ARG .. have to retype .. still getting random BANNED IP Problems...
Okay let me try again ...
#1 - dots changed per interval is calculated in this bit of code
Code:
Dotcount = CInt(400 / (1000 / Timer1.Interval))
It works like this....
400 (dots) / (per) 1000 (ms) - the actual math used is a little more than that, in that it works out the number per frame.. but i took the numbers from your original spec..
so to make it 1000 per second.. change the above line too
Code:
Dotcount = CInt(1000 / (1000 / Timer1.Interval))
#2 - Goto "Form1.vb Design" in VS .. where you now see the forms design ...
Click on the radio button that is labled 'Random'. Right click , Properties.. Scroll to Checked and set it to true. this will set random as default ...
#3 - this is a little harder than the others..
First at the top under all the other Private delcarations add
Code:
Private StartTime As Date
(just above Public sub .....)
In the Button1_click and button3_click subs .. Just after the 'Timer1.Enabled = True' add
Code:
StartTime = Date.Now
and the last one for this mod...
in the timer1_tick sub just after 'DrawPicture()' add
Code:
If StartTime.AddSeconds(30) < Date.Now Then
Timer1.Enabled = False
End If
this should stop the code after 30 seconds..
Now to trigger on a canvas click.. add this sub (same like last time)
Code:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Timer1.Interval = CInt(TextBox1.Text)
Timer1.Enabled = Not Timer1.Enabled
StartTime = Date.Now
End Sub
that should be it ....
Last edited by GremlinSA; February 23rd, 2011 at 02:40 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.