4

This is my bitmap code

Bitmap b = new Bitmap(columns, rows, PixelFormat.Format24bppRgb);
BitmapData bmd = b.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadWrite, b.PixelFormat); 

How can i save this as a grayscale image ?

Well iam specifically interested in the saving part. How do i save it as a file ?

user574183
  • 710
  • 1
  • 10
  • 22
  • You mentioned `BitmapData`. Do you mean to construct grayscale image in memory? If so, you need `PixelFormat.Format8bppIndexed` and [attach the grayscale palette](https://stackoverflow.com/questions/8603596/how-can-i-define-a-8-bit-grayscale-image-directly) before saving your bitmap. – tsul Apr 20 '19 at 21:10

4 Answers4

1

I've used a similar method to this before

http://www.codeproject.com/KB/graphics/quickgrayscale.aspx

e.g:

            for (int Y = 0; Y < Size.Y; Y++)
            {
                PixelData_s* PPixel =
                    PixelAt(0, Y, ImageWidth, PBase);

                for (int X = 0; X < Size.X; X++)
                {
                    byte Value = (byte)((PPixel->Red + PPixel->Green + PPixel->Blue) / 3);
                    PPixel->Red = Value;
                    PPixel->Green = Value;
                    PPixel->Blue = Value;
                    PPixel++;
                } // End for
            } // End for

Basically sum the RGB component values for the given pixel and divide by 3. This requires unsafe keyword which is required for operations using pointers. You could avoid using pointers and simply do something like this:

        for (int X = 0; X < Size.X; X++)
        {
            for (int Y = 0; Y < Size.Y; Y++)
            {
                Color C = WinBitmap.GetPixel(X, Y);
                int Value = (C.R + C.G + C.B) / 3;
                WinBitmap.SetPixel(X, Y, Color.FromArgb(Value, Value, Value));
            } // End for
        } // End for

but this is rather slow compared.

Jeb
  • 3,689
  • 5
  • 28
  • 45
1

I found a function how do this in this address

How to convert a colour image to grayscale

public Bitmap ConvertToGrayscale(Bitmap source)
{
  Bitmap bm = new Bitmap(source.Width,source.Height);
  for(int y=0;y<bm.Height;y++)
  {
    for(int x=0;x<bm.Width;x++)
    {
      Color c=source.GetPixel(x,y);
      int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));
    }
  }
  return bm;
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
DeveloperX
  • 4,633
  • 17
  • 22
1

We have an imaging component which facilitates application of numerous "effects", including simple color manipulations - it is considerably quicker to simply apply a color transformation matrix than manually walking pixel-by-pixel, like so, for example...

private static ColorMatrix GrayscaleMatrix = new ColorMatrix(
    new float[][]
    {
        new float[] {0.30f, 0.30f, 0.30f, 0, 0},
        new float[] {0.59f, 0.59f, 0.59f, 0, 0},
        new float[] {0.11f, 0.11f, 0.11f, 0, 0},
        new float[] {0, 0, 0, 1, 0},
        new float[] {0, 0, 0, 0, 1}
    }
);

public static void ApplyGrayscaleTransformation(string inputPath, string outputPath)
{
    using (var image = Bitmap.FromFile(inputPath))
    {
        using (var graphics = Graphics.FromImage(image))
        {
            using (var attributes = new ImageAttributes())
            {
                attributes.SetColorMatrix(GrayscaleMatrix);
                graphics.DrawImage(image, 
                    new Rectangle(0,0,image.Width, image.Height), 
                    0, 0, image.Width, image.Height, GraphicsUnit.Pixel, 
                    attributes);
            }
        }
        image.Save(outputPath);
    }
}

The speed between this and unsafe methods are mostly negligible but can vary; it's worth testing cases when it gets to that point - one benefit is not having to compile with /unsafe.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
-3

There are many links in google

like

https://web.archive.org/web/20110219113117/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale (WebArchive for broken link)

or

https://web.archive.org/web/20120707003826/http://www.dreamincode.net/code/snippet2570.htm (WebArchive for broken link)

Chad
  • 1,531
  • 3
  • 20
  • 46
Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
  • 4
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Grant Thomas Nov 10 '11 at 10:55
  • Link only answers with no context or explanation are pretty fragile. Links rot leaving other users with no idea what was at the other end. – Kev Nov 10 '11 at 11:15
  • links not available - it would be nice to enter main parts of the code here – shahar eldad Jul 11 '16 at 12:31