0

我读了这个链接Converting 8 bit color into RGB value

然后我尝试了如下的 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

But when one pixel is selected, an error occurs:

算术运算导致溢出。

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

谢谢 :)

4

1 回答 1

1

您的 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)
于 2013-08-08T13:50:27.283 回答