Image noise: Difference between revisions

m
→‎{{header|Visual Basic .NET}}: reduced long widths of code and comment lines, removed overwide warning.
m (→‎{{header|Visual Basic .NET}}: reduced long widths of code and comment lines, removed overwide warning.)
Line 2,634:
 
=={{header|Visual Basic .NET}}==
{{lines too long|Visual Basic .NET}}
Windows Forms Application.
 
Line 2,640 ⟶ 2,639:
 
Public Class frmSnowExercise
Dim bRunning As Boolean = True
 
Private Sub Form1_Load(ByVal sender As System.Object,
Dim bRunning As Boolean = True
ByVal e As System.EventArgs) Handles MyBase.Load
 
' Tell windows we want to handle all the painting and that we want it
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' to double buffer the form's rectangle (Double Buffering
' removes/ reduces flickering).
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint _
Or ControlStyles.OptimizedDoubleBuffer, True)
UpdateStyles()
 
' Tell windows we want' to handle allPrevent the paintinguser andfrom thatresizing the wewindow. wantOur itdraw tocode doubleis buffernot
' setup to recalculate on the fly.
' the form's rectangle (Double Buffering removes/reduces flickering).
FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
MaximizeBox = False
UpdateStyles()
 
' Prevent the user from' resizing theThe window. Oursize drawand codethe isclient notrectangle aren't the same.
' To get the proper dimensions for our exercise we need to
' setup to recalculate on the fly.
' figure out the difference and add it to our 320x240
FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
' requirement.
MaximizeBox = False
Width = 320 + Size.Width - ClientSize.Width
Height = 240 + Size.Height - ClientSize.Height
 
' ThePop the window, sizebring andit to the clientfront rectangleand aren'tgive thewindows time to same.
' reflect the changes.
' To get the proper dimensions for our exercise we need to
Show()
' figure out the difference and add it to our 320x240
' requirement. Activate()
Application.DoEvents()
Width = 320 + Size.Width - ClientSize.Width
Height = 240 + Size.Height - ClientSize.Height
 
' Pop the window, bring' it toHit the frontloop and givekeep going until we windowsreceive timea toclose request.
' reflect the changes. RenderLoop()
Show()
Activate()
Application.DoEvents()
 
' We're done. Exit the application.
' Hit the loop and keep going until we receive a close request.
RenderLoop Close()
 
End Sub
' We're done. Exit the application.
Close()
 
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As _
End Sub
System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
' Close the application when the user hits escape.
If e.KeyChar = ChrW(Keys.Escape) Then bRunning = False
End Sub
 
Private Sub Form1_KeyPressForm1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress_
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' Close the application when the user hits escape.
' We'll cancel the form close request if we're still running so we
If e.KeyChar = ChrW(Keys.Escape) Then bRunning = False
' don't get an error during runtime and set the close request flag.
End Sub
e.Cancel = bRunning
bRunning = False
End Sub
 
Private Sub RenderLoop()
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' We'll cancel the form close request if we're still running so we don't get an error during runtime and set the close request flag.
e.Cancel = bRunning
bRunning = False
End Sub
 
Const cfPadding As Single = 5.0F
Private Sub RenderLoop()
 
Dim b As New Bitmap(ClientSize.Width, ClientSize.Width,
Const cfPadding As Single = 5.0F
PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(b)
Dim r As New Random(Now.Millisecond)
Dim oBMPData As BitmapData = Nothing
Dim oPixels() As Integer = Nothing
Dim oBlackWhite() As Integer = {Color.White.ToArgb, Color.Black.ToArgb}
Dim oStopwatch As New Stopwatch
Dim fElapsed As Single = 0.0F
Dim iLoops As Integer = 0
Dim sFPS As String = "0.0 FPS"
Dim oFPSSize As SizeF = g.MeasureString(sFPS, Font)
Dim oFPSBG As RectangleF = New RectangleF(ClientSize.Width - cfPadding -
oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)
 
' Get ourselves a nice, clean, black canvas to work with.
Dim b As New Bitmap(ClientSize.Width, ClientSize.Width, PixelFormat.Format32bppArgb)
g.Clear(Color.Black)
Dim g As Graphics = Graphics.FromImage(b)
Dim r As New Random(Now.Millisecond)
Dim oBMPData As BitmapData = Nothing
Dim oPixels() As Integer = Nothing
Dim oBlackWhite() As Integer = {Color.White.ToArgb, Color.Black.ToArgb}
Dim oStopwatch As New Stopwatch
Dim fElapsed As Single = 0.0F
Dim iLoops As Integer = 0
Dim sFPS As String = "0.0 FPS"
Dim oFPSSize As SizeF = g.MeasureString(sFPS, Font)
Dim oFPSBG As RectangleF = New RectangleF(ClientSize.Width - cfPadding - oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)
 
' Get ourselves a nice,' clean,Prep blackour canvasbitmap tofor worka withread.
oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height),
g.Clear(Color.Black)
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
 
' PrepAllocate oursufficient bitmapspace for athe pixel data and read.
' flash copy it to our array.
oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
' We want an integer to hold the color for each pixel in the canvas.
Array.Resize(oPixels, b.Width * b.Height)
Runtime.InteropServices.Marshal.Copy(oBMPData.Scan0,
oPixels, 0, oPixels.Length)
b.UnlockBits(oBMPData)
' Start looping.
Do
' Find our frame time and add it to the total amount of time
' elapsed since our last FPS update (once per second).
fElapsed += oStopwatch.ElapsedMilliseconds / 1000.0F
oStopwatch.Reset() : oStopwatch.Start()
' Adjust the number of loops since the last whole second has elapsed
iLoops += 1
If fElapsed >= 1.0F Then
' Since we've now had a whole second elapse
' figure the Frames Per Second,
' measure our string,
' setup our backing rectangle for the FPS string
' (so it's clearly visible over the snow)
' reset our loop counter
' and our elapsed counter.
sFPS = (iLoops / fElapsed).ToString("0.0") & " FPS"
oFPSSize = g.MeasureString(sFPS, Font)
oFPSBG = New RectangleF(ClientSize.Width - cfPadding -
oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)
' We don't set this to 0 in case our frame time has gone
' a bit over 1 second since last update.
fElapsed -= 1.0F
iLoops = 0
End If
 
' Generate our snow.
' Allocate sufficient space for the pixel data and
For i As Integer = 0 To oPixels.GetUpperBound(0)
' flash copy it to our array.
oPixels(i) = oBlackWhite(r.Next(oBlackWhite.Length))
' We want an integer to hold the color for each pixel in the canvas.
Next
Array.Resize(oPixels, b.Width * b.Height)
Runtime.InteropServices.Marshal.Copy(oBMPData.Scan0, oPixels, 0, oPixels.Length)
b.UnlockBits(oBMPData)
 
' Prep the bitmap for an update.
' Start looping.
oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height),
Do
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
' Find our frame time and add it to the total amount of time
' Flash copy the new data into our bitmap.
' elapsed since our last FPS update (once per second).
Runtime.InteropServices.Marshal.Copy(oPixels, 0, oBMPData.Scan0,
fElapsed += oStopwatch.ElapsedMilliseconds / 1000.0F
oPixels.Length)
oStopwatch.Reset()
oStopwatch b.StartUnlockBits(oBMPData)
 
' Adjust the number of loops since' Draw the lastbacking wholefor secondour hasFPS elapseddisplay.
g.FillRectangle(Brushes.Black, oFPSBG)
iLoops += 1
' Draw our FPS.
g.DrawString(sFPS, Font, Brushes.Yellow, oFPSBG.Left, oFPSBG.Top)
 
' Update the form's background and draw.
If fElapsed >= 1.0F Then
' Since we've now hadBackgroundImage a= whole second elapseb
Invalidate(ClientRectangle)
' figure the Frames Per Second,
' measure our string,
' setup our backing rectangle for the FPS string (so it's clearly visible over the snow)
' reset our loop counter
' and our elapsed counter.
sFPS = (iLoops / fElapsed).ToString("0.0") & " FPS"
oFPSSize = g.MeasureString(sFPS, Font)
oFPSBG = New RectangleF(ClientSize.Width - cfPadding - oFPSSize.Width, cfPadding, oFPSSize.Width, oFPSSize.Height)
fElapsed -= 1.0F ' We don't set this to 0 incase our frame time has gone a bit over 1 second since last update.
iLoops = 0
End If
 
' GenerateLet windows handle some ourqueued snowevents.
For i As Integer = 0 To oPixelsApplication.GetUpperBoundDoEvents(0)
Loop While bRunning
oPixels(i) = oBlackWhite(r.Next(oBlackWhite.Length))
Next
 
End Sub
' Prep the bitmap for an update.
End Class
oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
</lang>
' Flash copy the new data into our bitmap.
Runtime.InteropServices.Marshal.Copy(oPixels, 0, oBMPData.Scan0, oPixels.Length)
b.UnlockBits(oBMPData)
 
' Draw the backing for our FPS display.
g.FillRectangle(Brushes.Black, oFPSBG)
' Draw our FPS.
g.DrawString(sFPS, Font, Brushes.Yellow, oFPSBG.Left, oFPSBG.Top)
 
' Update the form's background and draw.
BackgroundImage = b
Invalidate(ClientRectangle)
 
' Let windows handle some queued events.
Application.DoEvents()
Loop While bRunning
 
End Sub
End Class</lang>
Sample:<BR>
[[Image:SHSnowExercise.jpg]]