Using the CreateGraphics() method, this should work:
Bitmap b = new Bitmap(Width, Height, this.CreateGraphics());
//pixel is:
Color 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.
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);
// ...
}
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.
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.
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.