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 :

Copier une Bitmap vers une autre Form


Sujet :

C#

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut Copier une Bitmap vers une autre Form
    Bonjour à tous,

    Je me permets de venir sur le forum, car je cherche depuis pas mal de temps comment
    faire pour copier l'image d'une picturebox dans une form1 et la m'en servir dans une form2.

    En gros, je souhaite en fait créer un logiciel de traitement d'image et lorsque l'utilisateur va cliquez sur le bouton
    NDG, une form s'ouvre et il apparait un picturebox avec l'image d'origine et un picturebox2 avec l'image en ndg.

    J'ai deja fais les form1 et 2 mais impossible de "copier" mon image.

    J'ai regarder sur plusieurs forums et ils parlent tous de get { } et set {} mais pour des string, ...
    Mais je n'arrive pas à aller plus loin...

    Je vous remercie pour votre aide

  2. #2
    Membre chevronné
    Avatar de PixelJuice
    Homme Profil pro
    Ingénieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur .NET & Game Designer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 640
    Points : 2 149
    Points
    2 149
    Par défaut
    Bonjour,

    les get / set que tu as pu lire c'est pour faire le chemin inverse la plupart du temps (Form2 => Form1), dans ton cas il te suffit de changer le constructeur de la Form2 pour qu'il accepte un objet de type Bitmap ou Image. Quand tu construit ta Form, tu passe l'image de ta picturebox et le tour est joué. Puis comme ça tu pourras le modifier directement dans Form2.

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    Je vais essayer d'y regarder alors
    En fait, j'appelle mon Form2 avec en parametres, le picturebox1.Image ?

    Je reviens poster mon code si jamais je bug lol

    Merci beaucoup

  4. #4
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    Bon, j'ai avancé mon code,
    voici ce que j'ai mis dedans :

    Form F_main (correspond à ma form1 où j'ai chargé l'image) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     public Image img_picturebox()
            {
                Image img;
                img = pictureBox1.Image;
     
                return img;
            }
    Form F_NDG (correspond à ma form2 où je dois charger l'image de Form1 pour la traiter) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     public void Btn_Traiter_Click(Bitmap _img)
            {
                _pictureBox1.Image = _img;
                _pictureBox1.Show();
                Bitmap picture = (Bitmap)this._pictureBox1.Image;
                if (picture != null)
                {
                    this._pictureBox2.Image = MakeGrayscale(picture);
                }
                _pictureBox2.Show();
     
     
            }
    IL me reste à mettre en place la partie d'affichage de la fonction MakeGrayscale de traitement d'image . . .

    POuvez vous me dire s'il y a des erreurs deja ?

    Merci beaucoup

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    J'ai testé ce code, mais pas d'image qui apparaissent.
    Un peu deçu lol mais il ne me manque pas grand chose je pense...

    Si quelqu'un à trouvé une misère dans mon code

    MErci pour l'aide

  6. #6
    Membre chevronné
    Avatar de PixelJuice
    Homme Profil pro
    Ingénieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur .NET & Game Designer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 640
    Points : 2 149
    Points
    2 149
    Par défaut
    Citation Envoyé par julien43120 Voir le message
    J'ai testé ce code, mais pas d'image qui apparaissent.
    Un peu deçu lol mais il ne me manque pas grand chose je pense...

    Si quelqu'un à trouvé une misère dans mon code

    MErci pour l'aide
    C'est bien gentil mais le code que tu as montré n'indique pas vraiment ce que tu essayes de faire. Montre nous plutôt comment tu créer la 2e Form et comment tu passes l'image en paramètre.

    Il te suffit juste de faire ça :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
      // Code dans ta Form1          
                Form2 form = new Form2(pictureBox1.Image);
                form.Show();
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     // Constructeur de Form2        
             public Form2(Image image)
            {
                InitializeComponent();
                pictureBox1.Image = image;
            }

  7. #7
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    Le proto ressemble au mien
    JE posterais le code ce soir, car en déplacement ce matin et début d'après midi mais merci pour le retour

  8. #8
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    En fait, dans ma form1,
    j'ai créé une fonction public qui renvoie limage de la picturebox1;
    Dans le constructeur du form2, j'ai rajouté un argument (bitmap _img) qui correspond à mon image de form1.

    Et du coup, j'ai une fonction avec le bouton qui me permets d'afficher l'image et de la traiter...

    Je mets le code ce soir.


    Merci

  9. #9
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    51
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 51
    Points : 17
    Points
    17
    Par défaut
    Voici mon code de ma FOrm2, et je n'ai pas affiché d'image encore. je cherche encore si souci il y a :
    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
     
    using ....
     
     
    namespace WindowsFormsApplication1
    {
        public partial class F_NDG : Form
        {
            public Bitmap bmp;
            F_Main f1 = new F_Main();
     
            public F_NDG(Bitmap _img)
            {
                InitializeComponent();
                pictureBox1.Image = _img;
            }
     
     
            private void Btn_Quit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
     
            private void Btn_Sauv_Click(object sender, EventArgs e)
            {
                Bitmap bmp = new Bitmap(pictureBox2.ClientSize.Width, pictureBox2.ClientSize.Height);
                Graphics g = Graphics.FromImage(bmp);
                bmp.Save(@"c:/picturebox_NDG.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                MessageBox.Show("Image Enregistrée");
            }
     
     
            public static Bitmap MakeGrayscale(Bitmap original)
            {
                //make an empty bitmap the same size as original
     
                Bitmap gray_img = new Bitmap(original.Width, original.Height);
     
                for (int i = 0; i < original.Width; i++)
                {
                    for (int j = 0; j < original.Height; j++)
                    {
                        //get the pixel from the original image
                        Color originalColor = original.GetPixel(i, j);
     
                        //create the grayscale version of the pixel
                        int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                            + (originalColor.B * .11));
     
                        //create the color object
                        Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
     
                        //set the new image's pixel to the grayscale version
                        gray_img.SetPixel(i, j, newColor);
                    }
                }
     
                return gray_img;
            }
     
            public void Btn_Traiter_Click()
            {
                //pictureBox1.Image = _img;
                //pictureBox1.Show();
                Bitmap picture = (Bitmap)this.pictureBox1.Image;
                if (picture != null)
                {
                    this.pictureBox2.Image = MakeGrayscale(picture);
                }
                pictureBox2.Show();         
            }
        }
    }
    et ma form principale F_main :

    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
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class F_Main : Form
        {        
     
            public F_Main()
            {
                InitializeComponent();
            }
     
           /// Acces à l'image de pictureBox1 (F_Main)
           /// Pour passage vers F_NDG
     
            public Image img_picturebox()
            {
                Image img;
                img = pictureBox1.Image;
     
                return img;
            }
     
     
            /// END
     
     
     
     
     
            private void F_Main_Load(object sender, EventArgs e)
            {
     
            }
     
            private void label1_Click(object sender, EventArgs e)
            {
     
            }
     
            private void pictureBox1_Click(object sender, EventArgs e)
            {
            }
     
            private void pictureBox2_Click(object sender, EventArgs e)
            {
            }
     
            private void fermerToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
     
            void ouvrir_Click_1(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Image Files | *.png;*.jpg;*.bmp;*.jpeg;*.bmp";
                DialogResult result = ofd.ShowDialog();
     
                if (result == DialogResult.OK)
                {
                    string picture_path = ofd.FileName;
                    Bitmap picture = (Bitmap)Bitmap.FromFile(picture_path);
     
     
                    this.pictureBox1.Image = picture;
                    byte[] pixel_map = this.GetPixelMap(picture);
                    this.pictureBox2.Image = this.CreateHistogram(pixel_map);
     
                    this.comboBox1.Enabled = true;
                }
            }
     
            private byte[] GetPixelMap(Bitmap picture)
            {
                int size = picture.Width * picture.Height;
                int channel_index = this.comboBox1.SelectedIndex;
                int picture_width = picture.Width;
                int picture_height = picture.Height;
                byte[] pixels_map = new byte[size];
     
                for (int i = 0; i < picture_height; i++)
                {
                    for (int j = 0; j < picture_width; j++)
                    {
                        Color color = picture.GetPixel(j, i);
                        byte color_intensity = 0;
                        switch (channel_index)
                        {
                            case 0:
                                color_intensity = color.R;
                                break;
                            case 1:
                                color_intensity = color.G;
                                break;
                            case 2:
                                color_intensity = color.B;
                                break;
     
                        }
     
                        pixels_map[j + picture_width * i] = color_intensity;
                    }
                }
     
                return pixels_map;
            }
     
            private Bitmap CreateHistogram(byte[] pixel_map)
            {
                int size = pixel_map.Length;
                int index = this.comboBox1.SelectedIndex;
                int[] histogram = new int[256];
                int max = 0;
     
                for (int i = 0; i < size; i++)
                {
                    int pixel_value = pixel_map[i];
                    histogram[pixel_value]++;
                    if (histogram[pixel_value] > max)
                    {
                        max = histogram[pixel_value];
                    }
                }
     
                Point[] points = new Point[Byte.MaxValue + 3];
                points[0].X = 0;
                points[0].Y = 190;
     
                for (int i = 0; i < Byte.MaxValue + 1; i++)
                {
                    points[i + 1].X = i;
                    points[i + 1].Y = Convert.ToInt32(190 - Convert.ToDouble(histogram[i]) / max * 180);
                }
     
                points[Byte.MaxValue + 2].X = Byte.MaxValue;
                points[Byte.MaxValue + 2].Y = 190;
     
     
                Bitmap picture_histogram = new Bitmap(300, 300);
                Graphics graphic_histogram = Graphics.FromImage(picture_histogram);
                Pen pen = new Pen(Color.Black);
                pen.Brush = new SolidBrush(Color.Black);
                graphic_histogram.FillPolygon(pen.Brush, points);
                graphic_histogram.DrawLine(pen, new Point(0, 195), new Point(260, 195));
                graphic_histogram.DrawLine(pen, new Point(255, 197), new Point(260, 195));
                graphic_histogram.DrawLine(pen, new Point(255, 193), new Point(260, 195));
     
                return picture_histogram;
            }
     
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                Bitmap picture = (Bitmap)this.pictureBox1.Image;
                if (picture != null)
                {
                    byte[] pixel_map = this.GetPixelMap(picture);
                    this.pictureBox2.Image = this.CreateHistogram(pixel_map);
                }
            }
     
            private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
            {
     
            }
     
            private void Btn_Taille_Click(object sender, EventArgs e)
            {
                if (pictureBox1.Image != null)
                {
                    B_Affi_Taille1.Text = (pictureBox1.Image.Width.ToString());
                    B_Affi_Taille2.Text = (pictureBox1.Image.Height.ToString());
                    Total.Text = (pictureBox1.Image.PixelFormat.ToString());
                }
                else
                {
                    MessageBox.Show("Erreur de chargement");
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Image Files | *.png;*.jpg;*.bmp;*.jpeg;*.bmp";
                    DialogResult result = ofd.ShowDialog();
                }
            }
     
            private void sauverImageToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
                Graphics g = Graphics.FromImage(bmp);            
                bmp.Save(@"c:/picturebox.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                MessageBox.Show("Image Enregistrée");
            }
     
     
     
            private void niveauDeGrisToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
                Bitmap _img = new Bitmap(pictureBox1.Width,pictureBox1.Height);
                F_NDG Fndg = new F_NDG(_img);
                Fndg.Show();
            }
     
     
            private void egalisationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Bitmap _img2 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                F_Egal F2 = new F_Egal(_img2);
                F2.Show();
            }
     
            private void planCouleurToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Bitmap _img3 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                F_Plan F3 = new F_Plan(_img3);
                F3.Show();
            }
     
     
     
            private void Btn_Traiter_Click(object sender, EventArgs e)
            {
                Bitmap picture = (Bitmap)this.pictureBox1.Image;
                if (picture != null)
                {
                   // this.pictureBox4.Image = MakeGrayscale(picture);
                }
     
     
            }
     
        }
    }
    Merci pour l'aide apportée

Discussions similaires

  1. Réponses: 1
    Dernier message: 19/10/2011, 12h13
  2. Copier les enregistrements d'une table vers une table d'une autre DB
    Par karinette21 dans le forum Requêtes et SQL.
    Réponses: 4
    Dernier message: 18/11/2008, 21h50
  3. copier une partie d'une image vers une autre
    Par gregcat dans le forum Langage
    Réponses: 1
    Dernier message: 14/04/2006, 13h39
  4. Réponses: 12
    Dernier message: 22/02/2006, 15h28
  5. Copier un enregistrement d'une table vers une autre
    Par le lynx dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 04/01/2006, 09h56

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