Errechnet aus verschiedenen Farbpunkten eines Bildes die Durchschnittliche Farbe. Mit Quality kann man die Anzahl der Punkte bestimmen die horizontal und vertikal abgerufen werden.
z.B.:
PictureBox1.BackColor = GetAverageColor(picturebox1.Image)
Private Function GetAverageColor(ByVal img As Bitmap, _
Optional ByVal Quality As Integer = 10) As Color
Dim cDots As New ArrayList
Dim Red, Blue, Green As Integer
Dim HoriPoints As Integer = Math.Floor(img.Width / Quality)
Dim VertiPoints As Integer = Math.Floor(img.Height / Quality)
Dim result As Color = Color.White
Try
For i = 1 To Quality
Dim x = HoriPoints * i - 1
For ypos = 1 To Quality
Dim y = VertiPoints * ypos - 1
cDots.Add(img.GetPixel(x, y))
Next
Next
For Each c As Color In cDots
Red += c.R
Blue += c.B
Green += c.G
Next
Dim aColor As Color = Color.FromArgb(Red / cDots.Count, _
Green / cDots.Count, _
Blue / cDots.Count)
result = aColor
Catch ex As Exception
Return result
End Try
Return result
End Function
5 Kommentare zum Snippet