C # Mapa de bits con código inseguro
Estoy usando el siguiente código para crear máscaras de imágenes en C #:
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
bmp.SetPixel(x,y,Color.White);
}
}
for(int x = left; x < width; x++)
{
for(int y = top; y < height; y++)
{
bmp.SetPixel(x,y,Color.Transparent);
}
}
Pero es MUY lento ... ¿Cuál es el equivalente inseguro de esto? ¿Será mucho más rápido?
Al final hago un bmp.Save () en formato PNG.
ACTUALIZAR
Después de leer a través dehttp: //www.bobpowell.net/lockingbits.ht según lo sugerido por MusiGenesis, lo hice funcionar usando el siguiente código (para cualquier persona que lo necesite):
Bitmap bmp = new Bitmap(1000,1000,PixelFormat.Format32bppArgb);
BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width,bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
int PixelSize=4;
unsafe
{
for(int y=0; y<bmd.Height; y++)
{
byte* row=(byte *)bmd.Scan0+(y*bmd.Stride);
for(int x=0; x<bmd.Width; x++)
{
row[x*PixelSize] = 0; //Blue 0-255
row[x*PixelSize + 1] = 255; //Green 0-255
row[x*PixelSize + 2] = 0; //Red 0-255
row[x*PixelSize + 3] = 50; //Alpha 0-255
}
}
}
bmp.UnlockBits(bmd);
bmp.Save("test.png",ImageFormat.Png);
Alpha channel: 0 es totalmente transparente, 255 no es transparente en ese píxel.
Estoy seguro de que puede modificar fácilmente el bucle para pintar un rectángulo:)