Image noise: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
(perfomance improvement too! from 111 fps to 130 fps.) |
||
Line 7: | Line 7: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<lang csharp>using System; |
<lang csharp>using System; |
||
using System. |
using System.Collections.Generic; |
||
using System.ComponentModel; |
|||
using System.Drawing; |
using System.Drawing; |
||
using System.Drawing.Imaging; |
using System.Drawing.Imaging; |
||
using System.Runtime.InteropServices; |
|||
using System.Linq; |
using System.Linq; |
||
using System. |
using System.Runtime.InteropServices; |
||
using System. |
using System.Windows.Forms; |
||
class Program |
class Program |
||
{ |
{ |
||
static |
static byte[] colors = { 0, 255 }; |
||
static Size size = new Size(320, 240); |
static Size size = new Size(320, 240); |
||
static Rectangle rectsize = new Rectangle(new Point(0, 0), size); |
static Rectangle rectsize = new Rectangle(new Point(0, 0), size); |
||
static int |
static int numpixels = size.Width * size.Height; |
||
static int numbytes = numpixels * 3; |
|||
static PictureBox pb; |
static PictureBox pb; |
||
Line 29: | Line 30: | ||
static Random rand = new Random(); |
static Random rand = new Random(); |
||
static IEnumerable<byte> YieldVodoo( |
static IEnumerable<byte> YieldVodoo() |
||
{ |
{ |
||
// Yield 3 times same number (i.e 255 255 255) for numpixels |
// Yield 3 times same number (i.e 255 255 255) for numpixels times. |
||
// Numpixels is number of pixels, it's divided by 3 because it's a RGB image. |
|||
for (int i = 0; i < numpixels |
for (int i = 0; i < numpixels; i++) |
||
{ |
{ |
||
var tmp = colors[rand.Next( |
var tmp = colors[rand.Next(2)]; |
||
for (int j = 0; j < 3; j++) |
for (int j = 0; j < 3; j++) |
||
{ |
{ |
||
yield return |
yield return tmp; |
||
} |
} |
||
} |
} |
||
Line 47: | Line 47: | ||
static Image Randimg() |
static Image Randimg() |
||
{ |
{ |
||
// Low-level bitmaps |
// Low-level bitmaps |
||
⚫ | |||
⚫ | |||
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); |
var data = bitmap.LockBits(rectsize, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); |
||
var yielder = YieldVodoo(numbytes); |
|||
Marshal.Copy( |
Marshal.Copy( |
||
YieldVodoo().ToArray<byte>(),// source |
|||
0, // start |
0, // start |
||
data.Scan0, // scan0 is a pointer to low-level bitmap data |
data.Scan0, // scan0 is a pointer to low-level bitmap data |
||
numbytes / |
numbytes); // number of bytes in source |
||
bitmap.UnlockBits(data); |
bitmap.UnlockBits(data); |
||
Line 65: | Line 63: | ||
static void Main() |
static void Main() |
||
{ |
{ |
||
// TODO: Fix form size. |
|||
var form = new Form(); |
var form = new Form(); |
||
form.AutoSize = true; |
form.AutoSize = true; |
||
form.Size = new Size(0, 0); |
|||
form.Text = "Test"; |
form.Text = "Test"; |
||
form.FormClosed += delegate |
form.FormClosed += delegate |
||
{ |
{ |
||
Line 76: | Line 75: | ||
worker = new BackgroundWorker(); |
worker = new BackgroundWorker(); |
||
worker.DoWork += delegate |
worker.DoWork += delegate |
||
{ |
{ |
||
Line 98: | Line 98: | ||
} |
} |
||
catch |
catch |
||
{ |
{ |
||
// Just pass. |
// Just pass. |
||
} |
} |
||
} |
} |
||
}; |
}; |
||
worker.RunWorkerAsync(); |
worker.RunWorkerAsync(); |
||
FlowLayoutPanel flp = new FlowLayoutPanel(); |
|||
form.Controls.Add(flp); |
|||
pb = new PictureBox(); |
pb = new PictureBox(); |
||
pb.Size = size; |
pb.Size = size; |
||
//pb.Dock = DockStyle.Bottom; |
|||
flp.AutoSize = true; |
|||
flp.Controls.Add(pb); |
|||
form.Show(); |
|||
Application.Run(); |
Application.Run(); |
||
} |
} |