首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ColorMatrix进行sRGB灰度转换

使用ColorMatrix进行sRGB灰度转换
EN

Stack Overflow用户
提问于 2018-01-30 09:07:01
回答 1查看 95关注 0票数 1

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

这种灰度转换能否在带有ColorMatrix的Format24bppRgb图像上进行(如果可以,如何进行)?或者这只能以像素为单位来完成?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-30 12:03:58

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48512401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档