繁体   English   中英

C#矩阵的颜色单元

[英]Color cells of a matrix with C#

我需要创建一个40x40的矩阵并为每个单元格手动上色,如下所示。

颜色矩阵

我认为我可以在表单应用程序上使用40 * 40 = 160标签并使其一个一个地着色,但这不是很有效。 最佳做法是什么? 也许是ColorMatrix?

这是一个完整但简单的Windows Forms应用程序。

这可能是最直接的方法。 考虑一下我没有从矩阵中获取颜色的事实,但是您明白了,它具有在代码中绘制它的方式。

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            for (int x = 0, y = 0; x + y != (this.Width + this.Height); x++)
            {
                var color = Color.Red;
                if (x % 2 == 0 && y % 2 != 0) { color = Color.Blue; }

                e.Graphics.FillRectangle(new SolidBrush(color), x, y, 1, 1);

                if (x == this.Width)
                {
                    x = -1;
                    y++;
                }
            }
        }
    }
}

拦截OnPaint事件的一种替代方法是例如通过System.Drawing.Bitmap类创建40x40像素的位图,并设置所有像素的颜色。

最后,根据您的UI技术,在PictureBox(Windows窗体)或Image(WPF)中显示它,并设置缩放值以填充整个控件的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM