0

这是我第一次在这里提问,所以请善待。

这是我在 Visual Basic 2010 中制作的一个小游戏。我有一个图像,当单击它时,它会变暗。我需要它稍后被代码识别为与原始图像相同。

这是使图像变暗的代码:

Sub WaterDarken(ByVal picTarget As PictureBox)
    Dim pic As New Bitmap(picTarget.Image)
    Dim intRed As Integer
    Dim intGreen As Integer
    Dim intBlue As Integer
    For x As Integer = 0 To picTarget.Image.Width - 1
        For y As Integer = 0 To picTarget.Image.Height - 1
            Dim intDarkenedColor As Color = pic.GetPixel(x, y)
            If intDarkenedColor.R - 50 <= 255 And intDarkenedColor.R - 50 >= 0 Then
                intRed = intDarkenedColor.R - 50
            Else
                intRed = 0
            End If
            If intDarkenedColor.G - 50 <= 255 And intDarkenedColor.G - 50 >= 0 Then
                intGreen = intDarkenedColor.G - 50
            Else
                intGreen = 0
            End If
            If intDarkenedColor.B - 50 <= 255 And intDarkenedColor.B - 50 >= 0 Then
                intBlue = intDarkenedColor.B - 50
            Else
                intBlue = 0
            End If
            intDarkenedColor = Color.FromArgb(225, intRed, intGreen, intBlue)
            pic.SetPixel(x, y, intDarkenedColor)
        Next
    Next
    picTarget.Image = pic
End Sub

这是将原始图像与变暗图像进行比较的代码:

  Function IsDarkened(ByRef source As Image, ByRef darkenedSource As Image) As Boolean
    Dim pic As New Bitmap(source)
    Dim pic2 As New Bitmap(darkenedSource)
    Dim intRed As Integer
    Dim intGreen As Integer
    Dim intBlue As Integer
    For x As Integer = 0 To pic.Width - 1
        For y As Integer = 0 To pic.Height - 1
            Dim darkened As Color = pic.GetPixel(x, y)
            If darkened.R - 50 <= 255 And darkened.R - 50 >= 0 Then
                intRed = darkened.R - 50
            Else
                intRed = 0
            End If
            If darkened.G - 50 <= 255 And darkened.G - 50 >= 0 Then
                intGreen = darkened.R - 50
            Else
                intGreen = 0
            End If
            If darkened.B - 50 <= 255 And darkened.B - 50 >= 0 Then
                intBlue = darkened.B - 50
            Else
                intBlue = 0
            End If
            darkened = Color.FromArgb(255, intRed, intGreen, intBlue)
            pic.SetPixel(x, y, darkened)
        Next
    Next
    For x = 0 To pic.Width - 1
        For y = 0 To pic.Height - 1
            If pic.GetPixel(x, y) <> pic2.GetPixel(x, y) Then
                Return False
            End If
        Next
    Next
    Return True
End Function

但是,有些东西不起作用,并且图像没有被注意到是相同的。这一直困扰着我一段时间。如果你们中的任何人可以提供帮助,请这样做!谢谢。

4

1 回答 1

1

当您使用 Color.FromArgb() 时,您将像素设置为两个不同的 alpha 值:

intDarkenedColor = Color.FromArgb(225,intRed,intGreen,intBlue)

对比

变暗 = Color.FromArgb(255,intRed,intGreen,intBlue)

具有相同的 RGB 但不同的不透明度将始终导致不同的图像。

于 2012-05-14T18:46:16.363 回答