Bitmap C # usando código não seguro
Estou usando o seguinte código para criar máscaras de imagem em 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);
}
}
Mas é muito lento ... Qual é o equivalente inseguro para isso? Será mais rápido?
No final, faço um bmp.Save () no formato PN
ATUALIZAR
Depois de lerhttp: //www.bobpowell.net/lockingbits.ht conforme sugerido por MusiGenesis, eu fiz funcionar usando o seguinte código (para quem precisa):
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 sendo totalmente transparente, 255 sendo nenhuma transparência nesse pixe
Tenho certeza de que você pode modificar facilmente o loop para pintar um retângulo