根据C#中的注册将像素放入区域

时间:2018-01-11 00:56:36

标签: c# winforms pixels

我需要创建一个图形(正方形),对于系统中注册的每个客户端,我在客户端的广场的选定位置放置一个像素。我有这个例子。我需要在C#中提示如何开始。这只是开始的例子。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RandomPixelImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int width = 640, height = 320;
            //bitmap
            Bitmap bmp = new Bitmap(width, height);
            //random number
            Random rand = new Random();
            //create random pixels
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    //generate random ARGB value
                    int a = rand.Next(256);
                    int r = rand.Next(256);
                    int g = rand.Next(256);
                    int b = rand.Next(256);
                    //set ARGB value
                    bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
                }
            }
            //load bmp in picturebox1
            pictureBox1.Image = bmp;
            //save (write) random pixel image
            bmp.Save("D:\\Image\\RandomImage.png");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于您已经创建了一个Bitmap,所以您需要做的就是将其分配给PictureBox控件:

首先将PictureBox控件添加到表单中,然后添加此代码以将图像放到其上。

pictureBox.Image = bmp;

如您的示例所示,您可以使用Bitmap.SetPixel()来更改单个像素。