Image noise: Difference between revisions
Content added Content deleted
m (→{{header|Visual Basic .NET}}: reduced long widths of code and comment lines, removed overwide warning.) |
|||
Line 2,634: | Line 2,634: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{lines too long|Visual Basic .NET}} |
|||
Windows Forms Application. |
Windows Forms Application. |
||
Line 2,640: | Line 2,639: | ||
Public Class frmSnowExercise |
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() |
|||
' Prevent the user from resizing the window. Our draw code is not |
|||
' 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() |
|||
' The window size and the client rectangle 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 |
|||
' |
' Pop the window, bring it to the front and give windows time to |
||
' 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 |
|||
Activate() |
|||
Application.DoEvents() |
|||
Width = 320 + Size.Width - ClientSize.Width |
|||
Height = 240 + Size.Height - ClientSize.Height |
|||
' Hit the loop and keep going until we receive a close request. |
|||
RenderLoop() |
|||
Show() |
|||
Activate() |
|||
Application.DoEvents() |
|||
' We're done. Exit the application. |
|||
' Hit the loop and keep going until we receive a close request. |
|||
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 |
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As _ |
||
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) |
|||
' Prep our bitmap for a read. |
|||
oBMPData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), |
|||
g.Clear(Color.Black) |
|||
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb) |
|||
' |
' Allocate sufficient space for the pixel data and |
||
' 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() |
|||
b.UnlockBits(oBMPData) |
|||
' Draw the backing for our FPS display. |
|||
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 |
|||
BackgroundImage = b |
|||
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 |
|||
' |
' Let windows handle some queued events. |
||
Application.DoEvents() |
|||
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> |
Sample:<BR> |
||
[[Image:SHSnowExercise.jpg]] |
[[Image:SHSnowExercise.jpg]] |