VB.NET将字节数转换为8位(256色)的RGB值



我阅读了这个链接将8位颜色转换为RGB值

然后我尝试了如下VB.NET代码:

Private Sub picturebox1_MouseDown(ByVal sender As Object, _
                          ByVal e As System.Windows.Forms.MouseEventArgs) _
                          Handles picturebox1.MouseDown
    Dim bm As New Bitmap(picturebox1.Image)
    Dim Red As Byte = bm.GetPixel(e.X, e.Y).R
    Dim Green As Byte = bm.GetPixel(e.X, e.Y).G
    Dim Blue As Byte = bm.GetPixel(e.X, e.Y).B
    Dim ColorNumber As Byte = ((Red / 32) << 5) + ((Green / 32) << 2) + (Blue / 64)
    ' Show Byte Number of color
    MsgBox(ColorNumber)
    MsgBox(Red & ":" & Green & ":" & Blue)
    Red = (ColorNumber >> 5) * 32
    Green = ((ColorNumber >> 2) << 3) * 32
    Blue = (ColorNumber << 6) * 64
    MsgBox(Red & ":" & Green & ":" & Blue)

End Sub

但当选择一个像素时,会出现错误:

算术运算导致溢出。

如何获得256色(8位)图像的字节值,然后将(转换)得到的字节值恢复为RGB值。

感谢:)

您的ColorNumber已声明为Byte,它只能存储0到255之间的值…将代码更改为:

Dim ColorNumber As Int32 = ((Red / 32) << 5) + ((Green / 32) << 2) + (Blue / 64)

此外,由于您使用.Net,您可以通过以下功能获取颜色:

Dim color As Color = Color.FromRgb(Red, Green, Blue)

相关内容

  • 没有找到相关文章

最新更新