访问在C#中的单个像素 [英] Access to a single pixel in C#
问题描述
我工作的一所学校的项目现在。它的意思是3D编辑软件,(像玛雅的一个非常最小化版本)。我将它写在C#中,使用Windows应用程序项目。
我打算实施自己的所有的3D的东西,从预测到阴影。
我的问题是,我怎样才能在C#Windows应用程序直接访问单个像素?
我知道我将不得不在窗口中的主视图端口。但我还没有决定将如何尚未作出。是否有一个内置的对象,我可以使用,这将让我定义视图港口的边界,然后逐个绘制每个像素?
(我只是在黑暗中拍摄在这里,我不知道很多关于C#,我用C主要程序)
I'm working on a school project right now. It's meant to be a 3D editing software, (like a very minimized version of Maya). I have to write it in C#, using the 'Windows Application Project'. I intend to implement all the 3D stuff myself, from projections to shading. My question is, how can I get direct access to a single pixel in the C# windows application? I know I'm going to have a main view port in the window. But I haven't decided yet how it will be made. Is there a built in object that I can use, that will let me define the boundaries of the view port, and then paint each pixel individually? (I'm just shooting in the dark here, I don't know much about C#. I mostly program in C)
谢谢,$ B $ ; b马勒基
Thanks, Malki.
推荐答案
使用的createGraphics()方法,这应该工作:
Using the CreateGraphics() method, this should work:
Bitmap b = new Bitmap(Width, Height, this.CreateGraphics());
//pixel is:
Color c = b.GetPixel(x, y);
要设置一个像素到一个特定的颜色,用它来代替颜色为c = b.GetPixel(X,Y)
:
To set a pixel to a specific colour, use this instead of Color c = b.GetPixel(x,y)
:
b.SetPixel(x, y, c); // where c is a Color
如果你想有一个视口中,将面板或一个图片(也许与码头:填充),然后使用:
If you want a viewport, place a panel or a PictureBox (maybe with Dock: Fill), then use:
Bitmap b = new Bitmap(viewport.Width, viewport.Height, viewport.CreateGraphics());
代替以前使用的第一行。
instead of the first line used previously.
但是,从你想做的事,我认为这将是更好地使用OnPaint事件:
But from what you want to do, I think it would be better to use the OnPaint event:
void pnlViewport_Paint(object sender, PaintEventArgs e) {
if ( e.ClipRectange.Width < 1 || e.ClipRectangle.Height < 1 ) return;
Bitmap b = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height, e.Graphics);
// ...
}
这事件触发每次控制需要涂漆。第一行检查正在绘制的区域是否为空 - 这将不只是节省CPU时间,但您的应用程序可能会崩溃 - 你得到它做一个为0x0位图
This event fires every time the control needs painted. The first line checks whether the area being drawn is empty - this will not just save CPU time, but your application may crash - you are getting it to make a 0x0 bitmap.
修改:是的,这是可调整大小,如果码头= DockStyle.Fill;当您调整窗口大小,控制扩展,以填补空间。然后将其粉刷 - 射击事件。
EDIT: Yes, this is resizable, if Dock = DockStyle.Fill; As your window resizes, the control expands to fill the space. It is then repainted - firing the event.
编辑2 :正如有人指出的那样,这仍然缓慢。这听起来像是一个要求做3D绘图自己,所以也许 SDL.NET (我认为可以使用硬件加速)是要走的路。它甚至有一个(慢)SurfaceControl使用。
EDIT 2: As pointed out by others, this is still slow. It does sound like it is a requirement to do the 3D drawing yourself, so maybe SDL.NET (which I think can use hardware acceleration) is the way to go. It even has a (slow) SurfaceControl to use.
这篇关于访问在C#中的单个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!