我设置了一个代码来随机覆盖 2 种不同颜色的位图,10 次中有 7 次是蓝色,10 次中有 3 次是绿色。然而,当它完成后,它看起来非常不随机,就像它决定放 7 个蓝色像素几次,然后放 3 个绿色像素几次等等。
示例:
我的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FourEx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(canvas.Image);
System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, 800, 600), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
int tempy = 0;
while (tempy < 600)
{
byte* row = (byte*)bmpdata.Scan0 + (tempy * bmpdata.Stride);
for (int x = 0; x <= 800; x++)
{
Random rand = new Random();
if (rand.Next(1,10) <= 7)
{
row[x * 4] = 255;
}
else
{
row[(x * 4) + 1] = 255;
}
}
tempy++;
}
}
bmp.UnlockBits(bmpdata);
canvas.Image = bmp;
}
}
}
如果您需要其他信息,请告诉我。