在c#中保存位图图像为灰度图像

本文关键字:图像 灰度图像 位图 保存 | 更新日期: 2023-09-27 18:14:07

这是我的位图代码

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

如何将其保存为灰度图像

我对保存部分特别感兴趣。如何保存为文件?

在c#中保存位图图像为灰度图像

我以前用过类似的方法

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

例句:

            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

基本上是对给定像素的RGB分量值求和,然后除以3。这需要使用指针操作所需的不安全关键字。您可以避免使用指针,只需像这样做:

        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

但是这个比较慢

我们有一个成像组件,它促进了许多"效果"的应用,包括简单的颜色操作-简单地应用颜色变换矩阵比手动逐像素移动要快得多,就像这样,例如…

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);
    }
}

这种方法和unsafe方法之间的速度几乎可以忽略不计,但可以有所不同;当它达到这一点时,测试用例是值得的——一个好处是不必用/unsafe编译。

我找到了一个函数如何在这个地址

如何将彩色图像转换为灰度

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;
}

google里有很多链接

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

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