使用 Color Matrix 进行 sRGB 灰度转换

Using ColorMatrix for sRGB greyscale conversion

灰度转换的 sRGB 因子为 B、G、R = 0.0722、0.7152、0.2126。 https://en.wikipedia.org/wiki/Grayscale

能否在具有 ColorMatrix 的 Format24bppRgb 图像上完成此灰度转换(如果可以,如何)? 或者这只能以像素方式完成吗?

我还不太确定,但也许这就是答案:

Public Function ConvertToGrayscale(ByVal image As Bitmap) As Bitmap

    Dim grayscaleImage As Image = New Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb)
    Dim attributes As ImageAttributes = New System.Drawing.Imaging.ImageAttributes()

    Dim d1 As Double = 0.0722
    Dim d2 As Double = 0.7152
    Dim d3 As Double = 0.2126

    Dim grayscaleMatrix As New ColorMatrix(New Single()() {New Single() {d1, d1, d1, 0, 0}, New Single() {d2, d2, d2, 0, 0}, New Single() {d3, d3, d3, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}})
    attributes.SetColorMatrix(grayscaleMatrix)
    Using g As Graphics = Graphics.FromImage(grayscaleImage)
        g.DrawImage(image, New Rectangle(0, 0, grayscaleImage.Width, grayscaleImage.Height), 0, 0, grayscaleImage.Width, grayscaleImage.Height, GraphicsUnit.Pixel, attributes)
    End Using

    Return grayscaleImage

End Function