IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

determiner la couleur d un pixel d une photo avec Visual c++


Sujet :

C++

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut determiner la couleur d un pixel d une photo avec Visual c++
    Bonjour,

    Je souhaiterais determiner la couleur d un pixel d une photo avec Visual studio c++ 2005. J ai reussi a importer la photo .bmp.
    Apres de recherches sur internet et dans les aides, jai trouve ce bout de code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    typedef DWORD COLORREF;
    COLORREF GetPixel (
    HDC hdc,
    int x,
    int y
    );
    COLORREF color;
    color=GetPixel (hdc,x,y);
    Le souci c est que vu mes faibles notions en c++ et decouvrant visaul c++, je ne sais meme pas ou je dois rajouter ce bout de code
    Je lai rajoute sans le ressource.h par hasard au build il y a 1 succeed mais rien n apparait

    Si quelqun peut meclaircir et m aider,
    Merci beaucoup

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Ceci n'est pas UN code, mais un mélange.

    Déjà, tu dois avoir le Platform SDK installé. Ensuite, tu devras mettre #include <windows.h> dans ton code et la fonctio GetPixel() sera déclarée. Tu n'auras donc pas besoin des 6 premières lignes que tu as postées.

    Les deux dernières lignes doivent être mises à un moment de ton code où tu possède déjà la photo chargée dans un HBITMAP, et ce HBITMAP "sélectionné" dans un HDC. Elle te permettent d'obtenir la valeur RVB du pixel (tu peux utiliser ensuite GetRValue(), GetGValue(), GetBValue() dessus).
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Merci beaucoup pour tout ces renseignements.
    Mais comment je fais pour savoir si j ai la platform sdk instalee? Et dans le cas echeant, comment je fais pour l installer? Et qu est ce que c est que cette platform sdk? Quel type de projet dois je ouvrir?
    Derniere question: j ai insere l image en faisant add..new ressource.. bitmap,
    est ce le bon moyen? car je ne vois pas dans quel code je dois ecrire un .h, .cpp?
    Desole pour toutes ces questions mais je debute vraiment en visual c ++ 2005.
    Je vous remercie par avance!!

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Le Platform SDK est un ensemble de fichiers d'en-tête et bibliothèques statiques.
    Il est livré avec Visual, SAUF pour la version Express (si tu as la version Express, tu dois le télécharger séparément).

    Si le Platform SDK est installé, un programme contenant la ligne #include <windows.h> compilera correctement.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Je lai installe dans le doute car je le trouvais pas avec mon 2005, et suivi la manip comme pour visual express. Jai ensuite ouvert un projet win32 importer la photo comme detaille precedemment. Est ce bien comme ca qu on importe une bitmap? Ou dois je ecrire les commandes du code?

    Est ce quelqu un en saurait plus pour pouvoir m aider? car la jai essayer des codes de laide du logiciel et je suis vraiment perdue..
    merci beaucoup par avance

  6. #6
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Poste-donc ton code actuel.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  7. #7
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Alors voici le code que j'entre dans la fenetre form.h, apres avoir creer une picturebox, un label, et un button comme conseille dans l'aide msdn :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
     
    #pragma once
     
    namespace test4 {
     
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
     
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
     
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::PictureBox^  pictureBox1;
    	private: System::Windows::Forms::Label^  label1;
    	private: System::Windows::Forms::Button^  button1;
     
    	protected: 
     
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
     
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
    			this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
    			this->label1 = (gcnew System::Windows::Forms::Label());
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
    			this->SuspendLayout();
    			// 
    			// pictureBox1
    			// 
    			this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.Image")));
    			this->pictureBox1->Location = System::Drawing::Point(189, 12);
    			this->pictureBox1->Name = L"pictureBox1";
    			this->pictureBox1->Size = System::Drawing::Size(706, 578);
    			this->pictureBox1->TabIndex = 0;
    			this->pictureBox1->TabStop = false;
    			// 
    			// label1
    			// 
    			this->label1->AutoSize = true;
    			this->label1->Location = System::Drawing::Point(951, 226);
    			this->label1->Name = L"label1";
    			this->label1->Size = System::Drawing::Size(35, 13);
    			this->label1->TabIndex = 1;
    			this->label1->Text = L"label1";
    			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(929, 185);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(75, 23);
    			this->button1->TabIndex = 2;
    			this->button1->Text = L"button1";
    			this->button1->UseVisualStyleBackColor = true;
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(1032, 612);
    			this->Controls->Add(this->button1);
    			this->Controls->Add(this->label1);
    			this->Controls->Add(this->pictureBox1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
    			this->ResumeLayout(false);
    			this->PerformLayout();
     
    		}
    #pragma endregion
    	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    			 }
     
     
    private:
       Bitmap^ image1;
       void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
       {
          try
          {
     
             // Retrieve the image.
             image1 = gcnew Bitmap( "D:\k15-06.bmp",true );
             int x;
             int y;
     
             // Loop through the images pixels to reset color.
             for ( x = 0; x < image1->Width; x++ )
             {
                for ( y = 0; y < image1->Height; y++ )
                {
                   Color pixelColor = image1->GetPixel( x, y );
                   Color newColor = Color::FromArgb( pixelColor.R, 0, 0 );
                   image1->SetPixel( x, y, newColor );
     
                }
     
             }
     
             // Set the PictureBox to display the image.
             pictureBox1->Image = image1;
     
             // Display the pixel format in Label1.
             label1->Text = String::Format( "Pixel format: {0}", image1->PixelFormat );
          }
          catch ( ArgumentException^ ) 
          {
             MessageBox::Show( "There was an error."
             "Check the path to the image file." );
          }
     
       }
     
    	private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
    			 }
    };
    }
    Quand je debug, il n'y a pas de probleme apparent, mais quand je compile le programme il ne se passe rien..

  8. #8
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Enfin, la partie que j'ai moi-meme rajoute, c'est la suivante seulement, le reste etait de base:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    private:
    Bitmap^ image1;
    void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
    {
    try
    {
     
    // Retrieve the image.
    image1 = gcnew Bitmap( "D:\k15-06.bmp",true );
    int x;
    int y;
     
    // Loop through the images pixels to reset color.
    for ( x = 0; x < image1->Width; x++ )
    {
    for ( y = 0; y < image1->Height; y++ )
    {
    Color pixelColor = image1->GetPixel( x, y );
    Color newColor = Color::FromArgb( pixelColor.R, 0, 0 );
    image1->SetPixel( x, y, newColor );
     
    }
     
    }
     
    // Set the PictureBox to display the image.
    pictureBox1->Image = image1;
     
    // Display the pixel format in Label1.
    label1->Text = String::Format( "Pixel format: {0}", image1->PixelFormat );
    }
    catch ( ArgumentException^ ) 
    {
    MessageBox::Show( "There was an error."
    "Check the path to the image file." );
    }
     
    }
     
    private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
    }

  9. #9
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Houlà! Tu ne nous avais pas dit que tu étais en .Net! (ce code est du C++/CLI)

    Il manque une ligne pour que ta fonction button1_Click() soit effectivement appelée lors d'un clic sur le bouton.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    Rajoute ça dans InitializeComponent(), juste avant les commentaires pour //Form1.
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  10. #10
    Nouveau Candidat au Club
    Inscrit en
    Juin 2008
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 6
    Points : 1
    Points
    1
    Par défaut
    Merci pour cette ligne de code.
    Je viens de construire la solution et de compiler sans problemes apparant.
    Mais malheureusement, rien ne se passe, alors peut-etre que le programme que j'ai rajoute n'est pas suffisant?
    Qu'en penses-tu?
    En tout cas merci pour ton aide et ta patience!

    Non en fait, a present le bouton est bien actif, mais la lecture du pixel par GetPixel presente un probleme. J'obtiens ca : Format8bppIndexed

  11. #11
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Format8BppIndexed, c'est le PixelFormat. Ce n'est pas lié au GetPixel()...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Petite idée :

    Si tu change cette ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    image1 = gcnew Bitmap( "D:\k15-06.bmp",true );
    Par celle ci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    image1 = gcnew Bitmap( "D:\\k15-06.bmp",true );
    Car dans les chaines de caractères, \ est un caractère spéciaux (ex \n )
    Ça m'étonne d'ailleurs que le compilo ne dise rien. ( a moins que le \k soit voulu, mais j'en doute, le nom de l'image est bien : k15-06.bmp? )

    Et autre vérification, l'image est bien à l'emplacement D:\ ?

    Voila, j'espère que ça a aider.

    Clem

Discussions similaires

  1. Couleur d'un pixel sur une texture2D
    Par Tuizi dans le forum XNA/Monogame
    Réponses: 1
    Dernier message: 03/06/2008, 23h44
  2. retrouver la couleur d'un pixel dans une image
    Par Dennis Nedry dans le forum VBA Access
    Réponses: 1
    Dernier message: 19/12/2007, 14h00
  3. Valeur de la couleur d'un pixel d'une image
    Par PiPo123 dans le forum Windows
    Réponses: 4
    Dernier message: 10/03/2007, 21h42
  4. Obtenir la couleur d'un pixel d'une PictureBox
    Par avigeilpro dans le forum VB 6 et antérieur
    Réponses: 10
    Dernier message: 30/06/2006, 22h17
  5. Réponses: 1
    Dernier message: 26/03/2005, 12h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo