VB/VB.Net problème fonction pixel search

A

Anonymous

Invité
#1
bonjour, je commence la programmation en vb.net
j'ai voulu créer une fonction pixelsearch qui liste tout les pixel d'une couleur donné dans un tableau

mais j'ai une erreur et ne comprend pas la quelle...

mon code :
Code:
Function PixelSearch(ByVal Couleur As Color, ByVal Bitmap As Bitmap) As Point()
        Dim Img As Bitmap
        Img = New Bitmap(Bitmap)
        Dim Coul As Color
        Coul = Couleur
        Dim pos(-1) As Point
        Dim pos_actuel As Point
        Dim pos_actuel_color As Color
        Dim pos_max_x As Integer
        Dim pos_max_y As Integer
        Dim compteur As Integer
        pos_actuel.X = 0
        pos_actuel.Y = 0
        pos_max_x = Img.Width
        pos_max_y = Img.Height
        compteur = 0

        Do
            pos_actuel_color = img.GetPixel(pos_actuel.X, pos_actuel.Y)
            If pos_actuel_color = Coul Then
                pos(compteur) = pos_actuel
                compteur = compteur + 1
            End If
            If pos_actuel.X = pos_max_x Then
                pos_actuel.Y = pos_actuel.Y + 1
                pos_actuel.X = 0
            Else
                pos_actuel.X = pos_actuel.X + 1
            End If
        Loop Until pos_actuel.X = pos_max_x And pos_actuel.Y = pos_max_y

        If compteur = 0 Then
            pos(0) = New Point(-1, -1)
        End If

        Return pos
    End Function
l'erreur : ( fenêtre d’exécution )
Code:
Une exception de première chance de type 'System.ArgumentOutOfRangeException' s'est produite dans System.Drawing.dll
l'erreur : ( dans la petite fenêtre )
Code:
Le paramètre doit être positif et inférieur à la largeur.
Nom du paramètre : x
je vous remercie d'avance d'une futur réponse de votre par .
Steven
 
A

Anonymous

Invité
#2
problème résolut.

code de la fonction ( sa peut peu-être servir :p )
Code:
Function PixelSearch(ByVal Couleur As Color, ByVal Bitmap As Bitmap) As Point()
        Dim Img As Bitmap
        Img = New Bitmap(Bitmap)
        Dim Coul As Color
        Coul = Couleur
        Dim pos(Img.Width * Img.Height) As Point
        Dim pos_actuel As Point
        Dim pos_actuel_color As Color
        Dim pos_max_x As Integer
        Dim pos_max_y As Integer
        Dim compteur As Integer
        pos_actuel.X = 0
        pos_actuel.Y = 0
        pos_max_x = Img.Width
        pos_max_y = Img.Height
        compteur = 0

        While 1
            pos_actuel_color = Img.GetPixel(pos_actuel.X, pos_actuel.Y)
            If pos_actuel_color = Coul Then
                pos(compteur) = pos_actuel
                compteur = compteur + 1
            End If
            If pos_actuel.X = pos_max_x - 1 And pos_actuel.Y = pos_max_y - 1 Then
                If compteur = 0 Then
                    pos(0) = New Point(-1, -1)
                End If

                Return pos
            End If
            If pos_actuel.X = pos_max_x - 1 Then
                pos_actuel.Y = pos_actuel.Y + 1
                pos_actuel.X = 0
            Else
                pos_actuel.X = pos_actuel.X + 1
            End If
        End While
    End Function
 

Geraff

Administrateur
Membre du personnel
Inscrit
13 Mars 2008
Messages
564
Reactions
0
#3
merci du partage !
 

ToOnS

Membre Actif
Inscrit
8 Avril 2009
Messages
974
Reactions
0
#4
Salut , peu etre plus simple avec :
Code:
Function PixelSearch(ByVal Couleur As Color, ByVal Img As Bitmap) As Point()
for x =0 to Img.Width -1
        for y =0 to Img.Height -1
            if Img.GetPixel(X,Y)=Couleur then return new point(x,y)
       next
next
 
Haut Bas