J’utilise le code suivant pour créer des masques d’image 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); } }
Mais c’est beaucoup trop lent … Quel est l’équivalent dangereux à cela? Sera-ce allouer plus vite?
À la fin je fais un bmp.Save () au format PNG.
METTRE À JOUR:
Après avoir lu http://www.bobpowell.net/lockingbits.htm comme suggéré par MusiGenesis, je l’ai fait en utilisant le code suivant (pour tous ceux qui en ont besoin):
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);
Canal Alpha: 0 étant totalement transparent, 255 n’étant pas transparent sur ce pixel.
Je suis sûr que vous pouvez facilement modifier la boucle pour peindre un rectangle 🙂
Découvrez ce tutoriel sur l’utilisation de LockBits
:
http://www.BobPowell.net/lockingbits.htm
Ce sera des ordres de grandeur plus rapides qu’avec SetPixel
.