C# 在 Windows 窗体上绘制单个像素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/761003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw a single pixel on Windows Forms
提问by Mark T
I'm stuck trying to turn on a single pixel on a Windows Form.
我被困在试图打开 Windows 窗体上的单个像素。
graphics.DrawLine(Pens.Black, 50, 50, 51, 50); // draws two pixels
graphics.DrawLine(Pens.Black, 50, 50, 50, 50); // draws no pixels
The API really should have a method to set the color of one pixel, but I don't see one.
API确实应该有一种方法来设置一个像素的颜色,但我没有看到。
I am using C#.
我正在使用 C#。
采纳答案by Henk Holterman
This will set a single pixel:
这将设置单个像素:
e.Graphics.FillRectangle(aBrush, x, y, 1, 1);
回答by Adam Robinson
The Graphics
object doesn't have this, since it's an abstraction and could be used to cover a vector graphics format. In that context, setting a single pixel wouldn't make sense. The Bitmap
image format does have GetPixel()
and SetPixel()
, but not a graphics object built on one. For your scenario, your option really seems like the only one because there's no one-size-fits-all way to set a single pixel for a general graphics object (and you don't know EXACTLY what it is, as your control/form could be double-buffered, etc.)
该Graphics
对象不具备这一点,因为它是一个抽象和可用于覆盖矢量图形格式。在这种情况下,设置单个像素没有意义。该Bitmap
图像格式确实有GetPixel()
和SetPixel()
,但没有一个图形对象建立在一个。对于您的场景,您的选择似乎真的是唯一的选择,因为没有一种万能的方法可以为通用图形对象设置单个像素(并且您不知道它是什么,因为您的控件/表单可以是双缓冲的,等等)
Why do you need to set a single pixel?
为什么需要设置单个像素?
回答by Rytmis
Apparently DrawLine draws a line that is one pixel short of the actual specified length. There doesn't seem to be a DrawPoint/DrawPixel/whatnot, but instead you can use DrawRectangle with width and height set to 1 to draw a single pixel.
显然,DrawLine 绘制了一条比实际指定长度短一个像素的线。似乎没有 DrawPoint/DrawPixel/whatnot,但是您可以使用宽度和高度设置为 1 的 DrawRectangle 来绘制单个像素。
回答by Will Dean
Where I'm drawing lots of single pixels (for various customised data displays), I tend to draw them to a bitmap and then blit that onto the screen.
在我绘制大量单个像素(用于各种自定义数据显示)的地方,我倾向于将它们绘制到位图,然后将其 blit 到屏幕上。
The Bitmap GetPixel and SetPixel operations are not particularly fast because they do an awful lot of boundschecking, but it's quite easy to make a 'fast bitmap' class which has quick access to a bitmap.
Bitmap GetPixel 和 SetPixel 操作并不是特别快,因为它们进行了大量的边界检查,但是制作一个可以快速访问位图的“快速位图”类非常容易。
回答by WoodyDRN
Just to show complete code for Henk Holterman answer:
只是为了显示 Henk Holterman 答案的完整代码:
Brush aBrush = (Brush)Brushes.Black;
Graphics g = this.CreateGraphics();
g.FillRectangle(aBrush, x, y, 1, 1);
回答by nor
I think this is what you are looking for. You will need to get the HDC and then use the GDI calls to use SetPixel. Note, that a COLORREF in GDI is a DWORD storing a BGR color. There is no alpha channel, and it is not RGB like the Color structure of GDI+.
我想这就是你要找的。您需要获取 HDC,然后使用 GDI 调用来使用 SetPixel。请注意,GDI 中的 COLORREF 是存储 BGR 颜色的 DWORD。没有 alpha 通道,它不像 GDI+ 的颜色结构那样是 RGB。
This is a small section of code that I wrote to accomplish the same task:
这是我为完成相同任务而编写的一小段代码:
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);
}
...
}
回答by Eddy
Drawing a Line 2px using a Pen with DashStyle.DashStyle.Dot draws a single Pixel.
使用带有 DashStyle.DashStyle.Dot 的 Pen 绘制一条 2px 的线会绘制一个像素。
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Brushes.Black))
{
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(p, 10, 10, 11, 10);
}
}