Comment récupérer la valeur d'un pixel d'une picturebox

Fermé
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 - 26 juin 2014 à 08:07
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 - 27 juin 2014 à 12:12
Bonjour, à toutes et tous.
Je débute en Visual Basic 2010 et je voudrais savoir comment je dois m'y prendre
pour avoir la valeur de la couleur d'un pixel aux coordonnées X,Y d'une PictureBox.
Merci déjà de votre aide.




A voir également:

13 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
26 juin 2014 à 11:09
Bonjour,

En me servant du code donné hier, j'ai fait une recherche de pixel à partir du Form. Je m'explique j'affiche l'image contenu dans la pictureBox dans la BackgroundImage du Form.
Voici le code:

Option Strict On
Public Class Form1
    Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" _
 (ByVal hdc As Int32, ByVal x As Int32, ByVal y As Int32) As Int32
    Dim pic As Image
    Dim x As New DataObject
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ofd As New OpenFileDialog
        With ofd
            .Title = "Choisissez une image"
            .Filter = "jpeg (*.jpg)|*.jpg|gif (*.gif)|*.gif|tiff (*.tif)|*.tif|bmp (*.bmp)|*.bmp|png (*.png)|*.png"
            .FilterIndex = 1
            .RestoreDirectory = False
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                pic = New Bitmap(.FileName)
                x.SetData(DataFormats.Bitmap, False, pic)
                System.Windows.Forms.Clipboard.SetDataObject(x)
            Else
                MessageBox.Show("Opération annulée par l'utilisateur!", "Ouverture image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            .Dispose()
        End With
        Button1.Text = "Afficher image"
        Button2.Text = "Valeur pixel"
        Button3.Text = "Rétablir"
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        x = CType(System.Windows.Forms.Clipboard.GetDataObject, DataObject)
        pic = CType(x.GetData(DataFormats.Bitmap), Image)
        PictureBox1.Image = pic
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.BackgroundImage = PictureBox1.Image
        Me.BackgroundImageLayout = ImageLayout.Zoom
        PictureBox1.Visible = False
    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        PictureBox1.Visible = True
        Me.BackgroundImage = Nothing
    End Sub
   Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dim pixelColor As Color
       Using gr As Graphics = Graphics.FromHwnd(Me.Handle)
            pixelColor = ColorTranslator.FromWin32(GetPixel(CInt(gr.GetHdc()), CInt(e.X), CInt(e.Y)))
        End Using
        Dim r As Byte = (pixelColor.R)
        Dim g As Byte = (pixelColor.G)
        Dim b As Byte = (pixelColor.B)
        Dim text As String = _
    String.Format("red: {0}, green: {1}, blue: {2}", New Object() {r, g, b})
        MsgBox(text) 'code RGB
    End Sub
  
End Class


Vois si cela te convient?
1
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
26 juin 2014 à 11:50
Tu as aussi cela, a toi de voir:

  Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        Dim MyColor As Color
        MyColor = TempBitmap.GetPixel(e.X, e.Y)
        MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

1
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
26 juin 2014 à 14:28
Un Tout Grand Merci à toi pour ton aide.
J'essaie cela .
Bonne journée.
@+Michel.
0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
Modifié par papanou1965 le 26/06/2014 à 16:20
Excuse moi encore dit , mais je me suis mal expliqué.
Le code que tu me donne fonctionne bien , ce n'est pas le soucis.
Mais ce que je désirerai faire , c'est simplement de tester un pixel
au coordonnées fixes X/Y et de mettre tout sa dans une où des variables sans devoir cliquer.
J'ai bien tenté de bricoler sur le deuxième code , mais je n'arrive qu'à avoir
des Erreurs par brouettes entières. Lol
Au secours !
Merci d'avance.
Michel.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
26 juin 2014 à 16:54
J'ai pas trop de temps maintenant. Je te donne une ébauche. Sinon on verra demain.
A mettre dans un button:

 Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        Dim MyColor As Color
        Dim x As Integer = 100
        Dim y As Integer = 100
        MyColor = TempBitmap.GetPixel(x, y)
        'MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ' Label1.Text = MyColor.ToString
        Dim aString As String = MyColor.ToString
        Dim rString As String
        rString = Mid(aString, 14)
        rString = Replace(rString, "]", "")
        MsgBox(rString)
        Dim bString As String
        bString = rString.Substring(9, 3) ' couleur Vert tu fais la suite pour les autres couleurs: 9 début de chaine 3 longueur de chaine à mettre dans une variable
        MsgBox(bString)
    End Sub

0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
26 juin 2014 à 18:31
Merci , je teste cela et je te dit si je m'en sort avec sa.
Bonne soirée @ demain .
0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
26 juin 2014 à 19:50
Bon voilà , je n'ai pas de problème pour avoir les 3 valeurs RVB
dans des variables (integer) .
J'ai retourner le code dans tout les sens , mais je n'arrive à rien.
J'en suis toujours réduit à devoir cliquer.
Désolé de te faire avoir des cheveux blancs .
Tu ne saurais pas m'aider S T P .
Merci à demain.
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
26 juin 2014 à 20:44
Tu le mets dans l'évènement:

Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

Comme cela à l'entrée de la souris dans la PictureBox cela s'affichera.
Mais tu peux choisir un autre évènement détectable avec la souris si tu ne veux pas cliquer !
0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
27 juin 2014 à 06:53
Vraiment désolé sait-tu , mais j'ai copier-coller la sub partout.
Pour être sur de ne rien avoir oublié de tester .
Mais cela ne donne rien .
Si tu savais encore m'aider se serais gentil , je me rend bien compte que
j'abuse un peut non?
@ tantôt.
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
27 juin 2014 à 08:33
Toujours avec le même code, je t'ai mis la sub recupixel dans le bouton afficher avec des MsgBox pour vérifier:

Option Strict On
Public Class Form1
    Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" _
 (ByVal hdc As Int32, ByVal x As Int32, ByVal y As Int32) As Int32
    Dim pic As Image
    Dim x As New DataObject
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ofd As New OpenFileDialog
        With ofd
            .Title = "Choisissez une image"
            .Filter = "jpeg (*.jpg)|*.jpg|gif (*.gif)|*.gif|tiff (*.tif)|*.tif|bmp (*.bmp)|*.bmp|png (*.png)|*.png"
            .FilterIndex = 1
            .RestoreDirectory = False
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                pic = New Bitmap(.FileName)
                x.SetData(DataFormats.Bitmap, False, pic)
                System.Windows.Forms.Clipboard.SetDataObject(x)
            Else
                MessageBox.Show("Opération annulée par l'utilisateur!", "Ouverture image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            .Dispose()
        End With
        Button1.Text = "Afficher image"
        Button2.Text = "Valeur pixel"
        Button3.Text = "Rétablir"
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        x = CType(System.Windows.Forms.Clipboard.GetDataObject, DataObject)
        pic = CType(x.GetData(DataFormats.Bitmap), Image)
        PictureBox1.Image = pic
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        recupixel()
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.BackgroundImage = PictureBox1.Image
        Me.BackgroundImageLayout = ImageLayout.Zoom
        PictureBox1.Visible = False
    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        PictureBox1.Visible = True
        Me.BackgroundImage = Nothing
    End Sub
   Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dim pixelColor As Color
       Using gr As Graphics = Graphics.FromHwnd(Me.Handle)
            pixelColor = ColorTranslator.FromWin32(GetPixel(CInt(gr.GetHdc()), CInt(e.X), CInt(e.Y)))
        End Using
        Dim r As Byte = (pixelColor.R)
        Dim g As Byte = (pixelColor.G)
        Dim b As Byte = (pixelColor.B)
        Dim text As String = _
    String.Format("red: {0}, green: {1}, blue: {2}", New Object() {r, g, b})
        MsgBox(text) 'code RGB
    End Sub
  Private Sub PictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        Dim MyColor As Color
        MyColor = TempBitmap.GetPixel(e.X, e.Y)
        'MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Label1.Text = MyColor.ToString
        Dim aString As String = MyColor.ToString
      Dim rString As String
        rString = Mid(aString, 14)
        ' rString = Replace(rString, ",", "  ")
        rString = Replace(rString, "]", "")
        MsgBox(rString)
        Dim bString As String
        bString = rString.Substring(9, 3) ' bString = "String"
        MsgBox(bString)
    End Sub
    Private Sub recupixel()
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        Dim MyColor As Color
        Dim x As Integer = 100
        Dim y As Integer = 100
        Dim vert As String
        Dim rouge As String
        Dim bleu As String
        MyColor = TempBitmap.GetPixel(x, y)
        'MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Dim aString As String = MyColor.ToString
        Dim rString As String
        rString = Mid(aString, 14)
        rString = Replace(rString, "]", "")
        rString = Replace(rString, ",", " ")
        MsgBox(rString)
        rouge = rString.Substring(3, 3) ' couleur rouge
        MsgBox(rouge)
        vert = rString.Substring(9, 3) ' couleur Vert 
        MsgBox(vert)
        bleu = rString.Substring(16, 3) ' couleur bleu 
        MsgBox(bleu)
    End Sub
End Class



0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
27 juin 2014 à 09:41
Super , Un grand merci à toi.
J'ai assemblé des bouts de tes codes pour n'en faire qu'un seul.
Et sa tourne , maintenant je vais pouvoir voler de mes propres ailes
pour le reste du projet .
Encore merci à toi et bonne continuation.
Michel
P.S.
Pour les débutants en VB 2010 Express , voici un site bien fait.

http://fr.openclassrooms.com/informatique/cours/apprenez-a-programmer-en-vb-net/historique-et-visual-basic-express-2010

Désolé je n'ai pas trouvé les quote(s) pour les URL il faudra copier-coller.

Je te le met toujours , sa pourrais servir à des newbee(s) comme moi.
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
27 juin 2014 à 10:53
Pour aider les prochains demandeurs cliques sur Résolu.

Bonne programmation
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
27 juin 2014 à 12:12
Je me suis aperçu que si le rouge n'avait que 2 chiffres et le vert idem cela générait une erreur .
Voici le code rectifié:

Private Sub recupixel()
        Dim TempBitmap As New Bitmap(PictureBox1.Image)
        Dim MyColor As Color
        Dim x As Integer = 100
        Dim y As Integer = 100
        MyColor = TempBitmap.GetPixel(x, y)
        'MessageBox.Show("Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, "Color", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Dim aString As String = MyColor.ToString
        Dim rString As String
        rString = Mid(aString, 14)
        rString = Replace(rString, "]", " ")
        MsgBox(rString)
        Dim s As String = rString
        Dim separateur As Char = CChar(",")
        Dim nom() As String
        nom = s.Split(separateur)
        nom(0) = Replace(nom(0), "R=", "")
        rouge = nom(0)
        MsgBox(rouge)
        nom(1) = Replace(nom(1), "G=", "")
        vert = nom(1)
        MsgBox(vert)
        nom(2) = Replace(nom(2), "B=", "")
        bleu = nom(2)
        MsgBox(bleu)
    End Sub


Les variables:

  Dim vert As String
    Dim rouge As String
    Dim bleu As String


sont a déclarer au début du projets


0