TV static noise animation-adding random generator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: TV static noise animation-adding random generator

  1. #1
    Join Date
    Feb 2009
    Posts
    37

    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.

    You can view a Flash demo of this noise animation at http://elearningprojects.com/WN5.swf

    More info here about the visual noise animation (even a Delphi source code and Windows demo): http://www.st-andrews.ac.uk/~www_sp/.../personal/jgq/

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: TV static noise animation-adding random generator

    Hard to read, without CODE TAGS
    Code:
    ' Like This
    Please go back and edit your code to include tags so we can read it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2009
    Posts
    37

    Re: TV static noise animation-adding random generator

    Hi David,

    Thanks for your reply. I've now used BBcode as a FF add-on. I edited the above post using this. Hopefully, I did this correctly.

    Kind Regards,
    Last edited by saratogacoach; February 21st, 2011 at 02:35 PM.
    saratogacoach

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: TV static noise animation-adding random generator

    Looks OK now. You can just use the ADVANCED EDITOR here at CG to click the CODE button. (#)

    You can choose a Random.Net(0,400) sequence, but would have to probably choose a few patterns in advance, and switch them every second.

    Look into using Dual-Buffering your screen image
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2009
    Posts
    37

    Re: TV static noise animation-adding random generator

    Hi David,

    Thanks for the tip on setting up code tags. And, I'll keep in mind using double buffering.

    Mindful of your comments about needing some patterns set up in advancem I wondered then if this would be truly a random 400 every 1000 ms?

    I also did some online reading about random.net(), but as a beginner in VB, it's way beyond my skill level.

    So, unfortunately still stuck trying to modify this code to add the random generator.

    Kind Regards,
    saratogacoach

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: TV static noise animation-adding random generator

    Create 6-10 different bitmaps, and populate each one randomly. Then, you could fire a different one every few ms. Probably 30fps. XNA is 60fps, though
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Feb 2009
    Posts
    37

    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. :-)

    Kind Regards,
    saratogacoach

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,880

    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.

  9. #9
    Join Date
    Feb 2009
    Posts
    37

    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.

    Dynamic visual noise animation can be seen in the Flash demo at:
    http://elearningprojects.com/WN5.swf

    and Delphi source code and a Windows demo for dynamic visual noise is at:
    http://www.st-andrews.ac.uk/~www_sp/.../personal/jgq/

    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!

    Kind Regards,
    saratogacoach

  10. #10
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: TV static noise animation-adding random generator

    the main problem is the time needed for this one line of code to complete...

    Code:
    vGr.FillRectangle(New System.Drawing.SolidBrush(vCurrentColor), vCurrentDot)
    read the VB.net Animation article for some tips on better animation techniques..

    Code:
    MyGraphic.DrawRectangle(DelPen, .Location.X - 1,  .Location.Y - 1, 2, 2)
    Also declare your pen(or brush) outside the loop, so that it is ready to use and does not use up CPU time and memory rebuilding it for every dot.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  11. #11
    Join Date
    Feb 2009
    Posts
    37

    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.

    Best Wishes,
    saratogacoach

  12. #12
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: TV static noise animation-adding random generator

    Then, you could just use the FLASH that you posted
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: TV static noise animation-adding random generator

    Quote Originally Posted by saratogacoach View Post
    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
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  14. #14
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: TV static noise animation-adding random generator

    Okay .. i was going on the Topic title and did not initially look at the Flash demo ..

    Once i saw the demo and reread the OP slowly .. i saw the problem...

    So here is a small sample of what is needed using basically the methods from my article...

    In the Timer interval box i've put the default of 1000 ms in but for the same/simular effect as the Flash punch in 100 and start it ...

    I've also included 3 different starting displays...

    Please let me know if i got it right this time .....

    Gremmy...
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  15. #15
    Join Date
    Feb 2009
    Posts
    37

    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.

    Kind Regards,
    saratogacoach

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center