从像素阵列绘制图像

时间:2018-12-28 21:54:55

标签: c# arrays winforms drawing

我正在尝试编写一个代码,该代码从索引数组中绘制一组像素,这些像素指向另一个数组(基本上是调色板)中的颜色值。我是刚开始使用图片框在屏幕上绘制图像的新手,所以我对此类东西没有适当的经验。根据我的研究,此代码应该可以工作,但是表单上没有任何内容。有任何想法我在这里做错了吗?

static propTypes = {
  title: PropTypes.string,
  variant: PropTypes.string,
  size: PropTypes.string,
  drop: PropTypes.string,
  customToggle: PropTypes.func
};

1 个答案:

答案 0 :(得分:1)

像这样覆盖Form.OnPaint(PaintEventArgs e)

public partial class Form1 : Form
{
    private static readonly string[] colors =
        new string[] { "#FFFF0000", "#FF00FF00", "#FF0000FF", "#FFFFFF00", "#FFFF00FF", "#FF00FFFF" };
    private static readonly byte[] pixels =
        new byte[] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
    private static readonly byte scale = 10;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        for (int p = 0, x = 0, y = 0; p < pixels.Length; p++, x += scale)
        {
            if (x > 255)
            {
                x = 0;
                y += scale;
            }

            using (var brush = new SolidBrush(ColorTranslator.FromHtml(colors[pixels[p]])))
                e.Graphics.FillRectangle(brush, x, y, scale, scale);
        }
    }
}

它给出:

enter image description here