Image noise: Difference between revisions

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