c# - Create 1bpp mask from image -


how create 1 bit per pixel mask image using gdi in c#? image trying create mask held in system.drawing.graphics object.

i have seen examples use get/setpixel in loop, slow. method interests me 1 uses bitblits, this. can't work in c#, appreciated.

try this:

using system.drawing; using system.drawing.imaging; using system.runtime.interopservices; 

...

   public static bitmap bitmapto1bpp(bitmap img) {       int w = img.width;       int h = img.height;       bitmap bmp = new bitmap(w, h, pixelformat.format1bppindexed);       bitmapdata data = bmp.lockbits(new rectangle(0, 0, w, h), imagelockmode.readwrite, pixelformat.format1bppindexed);       (int y = 0; y < h; y++) {         byte[] scan = new byte[(w + 7) / 8];         (int x = 0; x < w; x++) {           color c = img.getpixel(x, y);           if (c.getbrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));         }         marshal.copy(scan, 0, (intptr)((int)data.scan0 + data.stride * y), scan.length);       }       bmp.unlockbits(data);       return bmp;     } 

getpixel() slow, can speed unsafe byte*.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -