Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Actualización: noviembre 2007
En este ejemplo, se crea y se rellena un objeto Bitmap y, a continuación, se muestra en un control PictureBox. Para ejecutar este ejemplo, cree un proyecto de aplicación de Windows Forms y arrastre un control PictureBox del Cuadro de herramientas al formulario. El tamaño del cuadro de imagen no es importante, ya que cambiará automáticamente para ajustarse al mapa de bits. Pegue el método CreateBitmap en la clase Form1 y llámelo desde el método de control de eventos Form1_Load.
Ejemplo
void CreateBitmap()
{
const int colWidth = 10;
const int rowHeight = 10;
System.Drawing.Bitmap checks = new System.Drawing.Bitmap(
colWidth * 10, rowHeight * 10);
// The checkerboard consists of 10 rows and 10 columns.
// Each square in the checkerboard is 10 x 10 pixels.
// The nested for loops are used to calculate the position
// of each square on the bitmap surface, and to set the
// pixels to black or white.
// The two outer loops iterate through
// each square in the bitmap surface.
for (int columns = 0; columns < 10; columns++)
{
for (int rows = 0; rows < 10; rows++)
{
// Determine whether the current sqaure
// should be black or white.
Color color;
if (columns % 2 == 0)
color = rows % 2 == 0 ? Color.Black : Color.White;
else
color = rows % 2 == 0 ? Color.White : Color.Black;
// The two inner loops iterate through
// each pixel in an individual square.
for (int j = columns * colWidth; j < (columns * colWidth) + colWidth; j++)
{
for (int k = rows * rowHeight; k < (rows * rowHeight) + rowHeight; k++)
{
// Set the pixel to the correct color.
checks.SetPixel(j, k, color);
}
}
}
}
}
Compilar el código
Para este ejemplo se necesita:
- Una referencia al espacio de nombres System.
Programación eficaz
Las condiciones siguientes pueden producir una excepción:
- Intentar establecer un píxel fuera de los límites del mapa de bits.
Vea también
Conceptos
Diseñar una interfaz de usuario en Visual C#