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

しかし、1 つのピクセルを選択すると、エラーが発生します。

算術演算でオーバーフローが発生しました。

256 色 (8 ビット) のイメージのバイト値を取得し、(変換) 結果のバイト値を RGB 値に復元するにはどうすればよいですか。

ありがとう :)

4

1 に答える 1

1

ColorNumber は、0 から 255 までの値のみを格納できる Byte として宣言されています... コードを次のように変更します。

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 に答える