我一直试图在Windows窗体上打开单个像素。
graphics.DrawLine(Pens.Black, 50, 50, 51, 50); // draws two pixels
graphics.DrawLine(Pens.Black, 50, 50, 50, 50); // draws no pixels
API确实应该有一种方法来设置一个像素的颜色,但我看不到。
我正在使用C#。
我一直试图在Windows窗体上打开单个像素。
graphics.DrawLine(Pens.Black, 50, 50, 51, 50); // draws two pixels
graphics.DrawLine(Pens.Black, 50, 50, 50, 50); // draws no pixels
API确实应该有一种方法来设置一个像素的颜色,但我看不到。
我正在使用C#。
Answers:
这将设置一个像素:
e.Graphics.FillRectangle(aBrush, x, y, 1, 1);
    DrawRectangle()。如果有Pen.Alignment = PenAlignment.Outset,它将Pen.Width在给定的矩形周围绘制像素粗轮廓。但是,如果指定Pen.Alignment = PenAlignment.Inset,它将在给定的矩形内绘制Pen.Width像素厚的“轮廓”。
                    我认为这就是您想要的。您将需要获取HDC,然后使用GDI调用来使用SetPixel。注意,GDI中的COLORREF是存储BGR颜色的DWORD。没有Alpha通道,也没有像GDI +的颜色结构那样的RGB。
这是我为完成同一任务而编写的一小段代码:
public class GDI
{
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    internal static extern bool SetPixel(IntPtr hdc, int X, int Y, uint crColor);
}
{
    ...
    private void OnPanel_Paint(object sender, PaintEventArgs e)
    {
        int renderWidth = GetRenderWidth();
        int renderHeight = GetRenderHeight();
        IntPtr hdc = e.Graphics.GetHdc();
        for (int y = 0; y < renderHeight; y++)
        {
            for (int x = 0; x < renderWidth; x++)
            {
                Color pixelColor = GetPixelColor(x, y);
                // NOTE: GDI colors are BGR, not ARGB.
                uint colorRef = (uint)((pixelColor.B << 16) | (pixelColor.G << 8) | (pixelColor.R));
                GDI.SetPixel(hdc, x, y, colorRef);
            }
        }
        e.Graphics.ReleaseHdc(hdc);
    }
    ...
}