Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从像素阵列绘制图像_C#_Arrays_Winforms_Drawing - Fatal编程技术网

C# 从像素阵列绘制图像

C# 从像素阵列绘制图像,c#,arrays,winforms,drawing,C#,Arrays,Winforms,Drawing,我试图编写一个代码,从指向另一个数组(基本上是调色板)颜色值的索引数组中绘制一组像素。我对使用图片框在屏幕上画图很陌生,所以我对这种东西没有什么经验。根据我的研究,这段代码应该可以工作,但是表单上没有任何内容。知道我做错了什么吗 public string[] colors = new string[] { "#FFFF0000", "#FF00FF00", "#FF0000FF", "#FFFFFF00", "#FFFF00FF", "#FF00FFFF" }; public byte[] p

我试图编写一个代码,从指向另一个数组(基本上是调色板)颜色值的索引数组中绘制一组像素。我对使用图片框在屏幕上画图很陌生,所以我对这种东西没有什么经验。根据我的研究,这段代码应该可以工作,但是表单上没有任何内容。知道我做错了什么吗

public string[] colors = new string[] { "#FFFF0000", "#FF00FF00", "#FF0000FF", "#FFFFFF00", "#FFFF00FF", "#FF00FFFF" };
public byte[] pixels = new byte[] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };

public byte scale = 2;

public void PaintPixels()
{
    Graphics g = CreateGraphics();

    int x = 0;
    int y = 0;

    for (int p = 0;  p < pixels.Length; p++)
    {
        using (var brush = new SolidBrush(ColorTranslator.FromHtml(colors[pixels[p]])))
        {
            g.FillRectangle(brush, x, y, scale, scale);
        }

        x += scale;

        if(x > 255)
        {
            y += scale;
            x = 0;
        }
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    Width = 256 * scale;
    Height = 240 * scale;

    PaintPixels();
}
public string[]colors=新字符串[]{“#FFFF0000”、“#FF00FF00”、“#FF0000FF”、“#FFFFFF00”、“#FF00FFFF”};
公共字节[]像素=新字节[]{0,0,1,1,2,2,3,3,4,4,5,5};
公共字节比例=2;
公共空间(像素)
{
Graphics g=CreateGraphics();
int x=0;
int y=0;
对于(int p=0;p255)
{
y+=刻度;
x=0;
}
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
宽度=256*刻度;
高度=240*刻度;
PaintPixels();
}

只需重写
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);
        }
    }
}
公共部分类表单1:表单
{
私有静态只读字符串[]颜色=
新字符串[]{“#ffffff0000”、“#FF00FF00”、“#FF0000FF”、“#FFFFFF00”、“#ff00ff”、“#FF00FFFF”};
专用静态只读字节[]像素=
新字节[]{0,0,1,1,2,2,3,3,4,4,5,5};
专用静态只读字节比例=10;
公共表格1()
{
初始化组件();
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
对于(int p=0,x=0,y=0;p255)
{
x=0;
y+=刻度;
}
使用(var brush=new SolidBrush(colorplator.FromHtml(colors[pixels[p]]))
e、 图形。填充矩形(画笔、x、y、比例、比例);
}
}
}
它给出:


摆脱CreateGraphics。总是错的。使用应该进行绘制的控件的绘制事件。我可以试试。上一次,由于某些原因,它不喜欢using语句,您必须显示该代码。您可以在应该显示调色板的控件的绘制事件中,将代码移动到
PaintPixels()
(从
int x=0;
)中。当然,
g
将变成
e.Graphics
正如评论中已经提到的,您应该使用
Paint
事件或
OnPaint()
方法来绘制图形,而不是
CreateGraphics()
。有关详细信息,请参阅标记的副本。表单不需要侦听自己的事件。您可以使用OnPaint覆盖。另外,不要处理那个图形对象——它不是你创建的。@LarsTech,说得好!谢谢