在容器中快速绘制WinForm中的像素

时间:2014-07-23 22:23:21

标签: c# pixel system.drawing

我有一个相当大的形式,并且绘制像素的位置移动,我有一个面板,它是所需的图像尺寸400,200 - 如何更改此面板上的单个像素?我也需要尽可能快的改变。

1 个答案:

答案 0 :(得分:1)

GDI +没有设置单个像素的方法,但是您可以设置1x1矩形以在OnPaint事件中实现相同的效果。感觉有点像黑客,但没有标准的方法来做到这一点。

SolidBrush brush = new SolidBrush(Color.White);

private void panel1_Paint(object sender, PaintEventArgs e)
{
    SetPixel(brush, e.Graphics, 10, 10, Color.Red);
}

public void SetPixel(SolidBrush brush, Graphics graphics, int x, int y, Color color)
{
    brush.Color = color;
    graphics.FillRectangle(brush, x, y, 1, 1);
}

您也可以直接访问位图并使用它的GetPixelSetPixel方法,但如果您的像素需要快速更新,它们通常会很慢。