1

I need a way to convert 1000+ 8bit bitmaps into eight 1bit bitmaps.

Currently I am running two loops which read each pixel from the main image and assign it a 1bpp image. It takes a very long time to accomplish this, anyway better to do it?

Here is an example of my code (separates only into two images):

Bitmap rawBMP = new Bitmap(path);
Bitmap supportRAW = new Bitmap(rawBMP.Width, rawBMP.Height);
Bitmap modelRAW = new Bitmap(rawBMP.Width, rawBMP.Height);

Color color = new Color();           

        for (int x = 0; x < rawBMP.Width; x++)
        {
            for (int y = 0; y < rawBMP.Height; y++)
            {
                color = rawBMP.GetPixel(x, y);

                if (color.R == 166) //model
                {
                    modelRAW.SetPixel(x, y, Color.White);
                }

                if (color.R == 249) //Support
                {
                    supportRAW.SetPixel(x, y, Color.White);
                }
            }
        }
        var supportBMP = supportRAW.Clone(new Rectangle(0, 0, rawBMP.Width, rawBMP.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
        var modelBMP = modelRAW.Clone(new Rectangle(0, 0, rawBMP.Width, rawBMP.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
Gal Begun
  • 11
  • 1
  • 1
    Do look up lockbits! – TaW Aug 09 '15 at 12:04
  • I'm voting to close this question as off-topic because it belongs on code review. – just.another.programmer Aug 09 '15 at 12:24
  • 2
    @just.another.programmer questions asking for performance improvements are on topic on both sites. Also please be aware that "because it belongs on site x" is *not a "valid" close reason*. Just because a question may be better suited to a different site, that doesn't mean it's off-topic here. – Vogel612 Aug 09 '15 at 13:01

1 Answers1

0

If you have to check every pixel then you are going to have to loop through them all at least once, however like TaW suggest there are more efficient ways to access pixels.

SetPixel and GetPixel are much slower then you accessing the data directly, Look at the use of unsafe to get direct access to the data, or marshaling to copy the data back and forth. ( see https://stackoverflow.com/a/1563170 for a more detailed written by notJim)

Community
  • 1
  • 1
Alexander
  • 1
  • 1