Create 1bpp mask from image

前端 未结 3 1371
灰色年华
灰色年华 2020-12-18 15:43

How do you create a 1 bit per pixel mask from an image using GDI in C#? The image I am trying to create the mask from is held in a System.Drawing.Graphics object.

I

相关标签:
3条回答
  • 2020-12-18 15:53

    In the Win32 C API the process to create a mono mask is simple.

    • Create an uninitialzied 1bpp bitmap as big as the source bitmap.
    • Select it into a DC.
    • Select the source bitmap into a DC.
    • SetBkColor on the destination DC to match the mask color of the source bitmap.
    • BitBlt the source onto the destination using SRC_COPY.

    For bonus points its then usually desirable to blit the mask back onto the source bitmap (using SRC_AND) to zero out the mask color there.

    0 讨论(0)
  • 2020-12-18 15:56

    Do you mean LockBits? Bob Powell has an overview of LockBits here; this should provide access to the RGB values, to do what you need. You might also want to look at ColorMatrix, like so.

    0 讨论(0)
  • 2020-12-18 16:03

    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);
          for (int y = 0; y < h; y++) {
            byte[] scan = new byte[(w + 7) / 8];
            for (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() is slow, you can speed it up with an unsafe byte*.

    0 讨论(0)
提交回复
热议问题