Bonjour,

Je souhaite sauvegarder en bitmap un contexte graphics. Je suis tombé sur plusieurs posts expliquant la méthode à suivre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
Graphics graph = CreateGraphics();
graph.DrawRectangle(new Pen(Color.Black,8),5,5,100,50);
graph.FillRectangle(new SolidBrush(Color.Blue),new Rectangle(5,5,100,50));
Image image = new Bitmap(100,50,graph);
imageList1.Images.Add(image);
Mais le problème est que je ne sais pas où insérer cette ligne de code. J'ai pour projet de créer un sorte de Paint.
J'ai donc un fichier avec dedans une classe mère : Figure et 3 classe qui dérive : Cercle, Carré et Triangle.

D'un autre côté, j'ai une classe formulaire qui gère l'affichage et le dessin des figure sur un panel. Le problème est que je ne sais pas du tout où insérer l'image et faire le FromImage.

J'ai essayé dans le constructeur de mon formulaire mais ca ne marche pas.

Voici le code de mes figures :
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
240
241
242
243
244
245
public abstract class Figure
    {
        //Champs protégés (accessibles seulement par les classes dérivées)
        protected Point m_ptPosition = new Point(0, 0);
        protected Color m_colTrait = Color.Black;
        protected bool m_bRempli = false;
        protected int m_nEpaisseur = 1;
 
        //Constructeurs
        public Figure(Point pt, Color col, bool b, int Ep)
        {
            this.m_ptPosition = pt;
            this.m_colTrait = col;
            this.m_bRempli = b;
            this.m_nEpaisseur = Ep;
        }
 
        public Figure(Point pt)
        {
            this.m_ptPosition = pt;
        }
 
        //Propriétés
        public Point Position
        {
            get { return this.m_ptPosition; }
            set { this.m_ptPosition = value; }
        }
 
        public Color Couleur
        {
            get { return this.m_colTrait; }
            set { this.m_colTrait = value; }
        }
 
        public bool Rempli
        {
            get { return this.m_bRempli; }
            set { this.m_bRempli = value; }
        }
 
        public int Epaisseur
        {
            get { return this.m_nEpaisseur; }
            set { this.m_nEpaisseur = value; }
        }
 
        //méthodes abstraites qui doivent être surchargées
        public abstract void Dessine(Graphics g);
        public abstract bool Clic(Point pt);
    }
 
    class Triangle : Figure
    {
        private Point[] points;
 
        public Triangle(int positionX, int positionY, Color couleur, int epaisseur, bool rempli)
            : base (new Point(positionX, positionY),couleur, rempli, epaisseur)
        {
            points = new Point[3];
        }
 
        public Triangle(int positionX, int positionY)
            : base(new Point(positionX, positionY))
        {
            points = new Point[3];
            points[0] = new Point(positionX, positionY);
        }
 
        public Point this[int i]
        {
            set
            {
                this.points[i] = value;
            }
            get
            {
                return this.points[i];
            }
        }
 
        public override string ToString()
        {
            string ts = "PointA : {" + this.points[0].X + "," + this.points[0].Y + "} - PointB : {"
                            + this.points[1].X + "," + this.points[1].Y + "} - PointC : {"
                                + this.points[2].X + "," + this.points[2].Y + "} - Couleur : "
                                    + this.m_colTrait + " - Epaisseur : " + this.m_nEpaisseur;
            return ts;
        }
 
        public override void Dessine(Graphics g)
        {
            if (m_bRempli)
            {
                SolidBrush brosse = new SolidBrush(m_colTrait);
                g.FillPolygon(brosse, points);
            }
            else
            {
                Pen stylo = new Pen(m_colTrait, m_nEpaisseur);
                g.DrawPolygon(stylo, points);
            }
        }
 
        public override bool Clic(Point pt)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddPolygon(points);
            if (gp.IsVisible(pt))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
 
    class Carre : Figure
    {
        private int m_Longueur;
 
        public Carre(int positionX, int positionY, int longueur, Color couleur, int epaisseur, bool rempli)
            : base(new Point(positionX, positionY), couleur, rempli, epaisseur)
        {
            this.m_Longueur = longueur;
        }
 
        public Carre(int positionX, int positionY, int longueur)
            : base(new Point(positionX, positionY))
        {
            this.m_Longueur = longueur;
        }
 
        public override string ToString()
        {
            string ts = "Position : {" + this.m_ptPosition.X + "," + this.m_ptPosition.Y + "} - Longueur : "
                    + this.m_Longueur + " - Couleur : " + this.m_colTrait + " - Epaisseur : " + this.m_nEpaisseur;
            return ts;
        }
 
        public int Longueur
        {
            set
            {
                this.m_Longueur = value;
            }
 
            get
            {
                return this.m_Longueur;
            }
        }
 
        public override bool Clic(Point pt)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddRectangle(new Rectangle(this.m_ptPosition.X, this.m_ptPosition.Y, this.m_Longueur, this.m_Longueur));
            if (gp.IsVisible(pt))
                return true;
            else
                return false;
        }
 
        public override void Dessine(Graphics g)
        {
            Color couleur = base.Couleur;
            Pen stylo = new Pen(couleur, m_nEpaisseur);
            Point position = base.Position;
 
            if (base.Rempli)
            {
                SolidBrush brosse = new SolidBrush(couleur);
                g.FillRectangle(brosse, this.m_ptPosition.X, this.m_ptPosition.Y, this.m_Longueur, this.m_Longueur);
            }
            else
            {
                g.DrawRectangle(stylo, this.m_ptPosition.X, this.m_ptPosition.Y, this.m_Longueur, this.m_Longueur);
            }
        }
    }
 
    class Cercle : Figure
    {
        private int m_nRayon;
 
        public Cercle (int positionX, int positionY, int rayon, Color couleur, int epaisseur, bool rempli) 
            : base (new Point(positionX,positionY),couleur,rempli,epaisseur)
        {
            this.m_nRayon = rayon;
        }
 
        public Cercle(int positionX, int positionY, int rayon)
            : base(new Point(positionX, positionY))
        {
            this.m_nRayon = rayon;
        }
 
        public override string ToString()
        {
            string ts = "Position : {" + this.m_ptPosition.X + "," + this.m_ptPosition.Y + "} - Rayon : "
                + this.m_nRayon + " - Couleur : " + this.m_colTrait + " - Epaisseur : " + this.m_nEpaisseur;
            return ts;
        }
 
        public int Rayon
        {
            get
            {
                return this.m_nRayon;
            }
            set
            {
                this.m_nRayon = value;
            }
        }
 
        public override void Dessine(Graphics g)
        {
            Color couleur = base.Couleur;
            Pen stylo = new Pen(couleur, m_nEpaisseur);
            Point position = base.Position;
 
            if (base.Rempli)
            {
                SolidBrush brosse = new SolidBrush(couleur);
                g.FillEllipse(brosse, ((position.X) - m_nRayon), ((position.Y) - m_nRayon), 2*m_nRayon, 2*m_nRayon);
            }
            else
            {
                g.DrawEllipse(stylo, ((position.X) - m_nRayon), ((position.Y) - m_nRayon), 2*m_nRayon, 2*m_nRayon);
            }
        }
 
        public override bool Clic(Point pt)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(this.m_ptPosition.X, this.m_ptPosition.Y, 2 * this.m_nRayon, 2 * this.m_nRayon);
            if (gp.IsVisible(pt))
                return true;
            else
                return false;
        }
    }
Voici le code de mon formulaire :
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
public partial class FormMere : Form
    {
        private List<Figure> m_FigList;
        private Etats m_Etat = Etats.Start;
 
        private Point p;
        private Cercle cercle;
        private Carre carre;
        private Triangle triangle;
 
        private enum Etats { Start, CercleCreation, CercleCentre, CarreCreation, CarreCentre, TriangleCreation, TrianglePoint1, TrianglePoint2, TrianglePoint3, TriangleCreationPoint1, TriangleCreationPoint2, TriangleCreationPoint3 }
 
        public FormMere()
        {
            InitializeComponent();
            m_FigList = new List<Figure>();
        }
 
        private void NewCercle_Click(object sender, EventArgs e)
        {
            m_Etat = Etats.CercleCreation;
            NewCercle.Checked = true;
        }
 
        private void NewCarre_Click(object sender, EventArgs e)
        {
            m_Etat = Etats.CarreCreation;
            NewCarre.Checked = true;
        }
 
        private void NewTriangle_Click(object sender, EventArgs e)
        {
            m_Etat = Etats.TriangleCreation;
            NewTriangle.Checked = true;
        }
 
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            foreach (Figure fig in m_FigList)
            {
                fig.Dessine(e.Graphics);
            }
 
        }
 
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_Etat == Etats.CercleCentre)
            {
                m_Etat = Etats.Start;
                Invalidate();
                NewCercle.Checked = false;
            }
            if (m_Etat == Etats.CarreCentre)
            {
                m_Etat = Etats.Start;
                Invalidate();
                NewCarre.Checked = false;
            }
            if (m_Etat == Etats.TrianglePoint1)
            {
                Point p = new Point(e.X, e.Y);
                triangle = new Triangle(e.X, e.Y);
                triangle[0] = p;
 
                m_Etat = Etats.TriangleCreationPoint2;
            }
            if (m_Etat == Etats.TrianglePoint2)
            {
                Point p = new Point(e.X, e.Y);
                triangle[1] = p;
 
                m_Etat = Etats.TriangleCreationPoint3;
            }
            if (m_Etat == Etats.TrianglePoint3)
            {
                Point p = new Point(e.X, e.Y);
                triangle[2] = p;
 
                m_FigList.Add(triangle);
                Invalidate();
                NewTriangle.Checked = false;
                m_Etat = Etats.Start;
            }
        }
 
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_Etat == Etats.CercleCentre)
            {
                double rayon = Math.Sqrt((e.X - p.X) * (e.X - p.X) + (e.Y - p.Y) * (e.Y - p.Y));
                cercle.Rayon = (int)rayon;
                Invalidate();
            }
            if (m_Etat == Etats.CarreCentre)
            {
                int longueur = e.X - p.X;
                carre.Longueur = longueur;
                Invalidate();
            }
        }
 
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (m_Etat == Etats.CercleCreation)
            {
                p = new Point(e.X, e.Y);
                cercle = new Cercle(p.X, p.Y, 0);
                m_FigList.Add(cercle);
                Invalidate();
                m_Etat = Etats.CercleCentre;
            }
            if (m_Etat == Etats.CarreCreation)
            {
                p = new Point(e.X, e.Y);
                carre = new Carre(p.X, p.Y, 0);
                m_FigList.Add(carre);
                Invalidate();
                m_Etat = Etats.CarreCentre;
            }
            if (m_Etat == Etats.TriangleCreation)
            {
                m_Etat = Etats.TrianglePoint1;
            }
            if (m_Etat == Etats.TriangleCreationPoint2)
            {
                m_Etat = Etats.TrianglePoint2;
            }
            if (m_Etat == Etats.TriangleCreationPoint3)
            {
                m_Etat = Etats.TrianglePoint3;
            }
        }
 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            panel1.Invalidate();
            Refresh();
        }
 
        public override void Refresh()
        {
            Remplir();
        }
 
        public void Remplir()
        {
            {
                calques.Items.Clear();
                string[] calque = new string[2];
 
                foreach (Figure f in m_FigList)
                {
                    calque[0] = "Figure";
 
                    if (f is Triangle)
                        calque[0] = "Triangle";
                    if (f is Carre)
                        calque[0] = "Carré";
                    if (f is Cercle)
                        calque[0] = "Cercle";
 
                    calque[1] = f.ToString();
 
                    ListViewItem item = new ListViewItem(calque);
                    item.Tag = Figure;
                    calques.Items.Add(item);
                }
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            supprimerToolStripMenuItem.Enabled = false;
            changerLaCouleurToolStripMenuItem.Enabled = false;
            remplirToolStripMenuItem.Enabled = false;
            mToolStripMenuItem.Enabled = false;
            modifierLeRayonToolStripMenuItem.Enabled = false;
            modifierLaLongueurToolStripMenuItem.Enabled = false;
            remplirToolStripMenuItem.Checked = false;
        }
 
        private void calques_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (calques.SelectedIndices.Count > 0)
            {
                supprimerToolStripMenuItem.Enabled = true;
                changerLaCouleurToolStripMenuItem.Enabled = true;
                remplirToolStripMenuItem.Enabled = true;
                mToolStripMenuItem.Enabled = true;
                remplirToolStripMenuItem.Checked = false;
 
                if (calques.SelectedIndices.Count < 2)
                {
                    Figure f = m_FigList[calques.SelectedIndices[0]];
                    if (f is Carre)
                    {
                        modifierLaLongueurToolStripMenuItem.Enabled = true;
                        modifierLeRayonToolStripMenuItem.Enabled = false;
                    }
                    else if (f is Cercle)
                    {
                        modifierLeRayonToolStripMenuItem.Enabled = true;
                        modifierLaLongueurToolStripMenuItem.Enabled = false;
                    }
                    else
                    {
                        modifierLeRayonToolStripMenuItem.Enabled = false;
                        modifierLaLongueurToolStripMenuItem.Enabled = false;
                    }
 
                    if (f.Rempli)
                    {
                        remplirToolStripMenuItem.Checked = true;
                    }
                    else
                    {
                        remplirToolStripMenuItem.Checked = false;
                    }
                }
            }
            else
            {
                supprimerToolStripMenuItem.Enabled = false;
                changerLaCouleurToolStripMenuItem.Enabled = false;
                remplirToolStripMenuItem.Enabled = false;
                mToolStripMenuItem.Enabled = false;
                modifierLeRayonToolStripMenuItem.Enabled = false;
                modifierLaLongueurToolStripMenuItem.Enabled = false;
            }
        }
 
        private void supprimerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Voulez vous vraiment supprimer cette/ces figure(s) ?", "Attention", MessageBoxButtons.OKCancel);
            int decalage = 0;
            switch (d)
            {
                case DialogResult.OK:
                    foreach (int n in calques.SelectedIndices)
                    {
                        m_FigList.RemoveAt(n - decalage);
                        decalage++;
                    }
                    Invalidate();
 
                    break;
 
                case DialogResult.Cancel:
                    break;
            }
            supprimerToolStripMenuItem.Enabled = false;
            changerLaCouleurToolStripMenuItem.Enabled = false;
            remplirToolStripMenuItem.Enabled = false;
            mToolStripMenuItem.Enabled = false;
            modifierLeRayonToolStripMenuItem.Enabled = false;
            modifierLaLongueurToolStripMenuItem.Enabled = false;
        }
 
        private void changerLaCouleurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            Color couleur = cd.Color;
 
            foreach (int n in calques.SelectedIndices)
            {
                Figure f = m_FigList[n];
                f.Couleur = couleur;
            }
            Invalidate();
 
            supprimerToolStripMenuItem.Enabled = false;
            changerLaCouleurToolStripMenuItem.Enabled = false;
            remplirToolStripMenuItem.Enabled = false;
            mToolStripMenuItem.Enabled = false;
            modifierLeRayonToolStripMenuItem.Enabled = false;
            modifierLaLongueurToolStripMenuItem.Enabled = false;
        }
 
        private void remplirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (int n in calques.SelectedIndices)
            {
                Figure f = m_FigList[n];
                if (f.Rempli)
                {
                    f.Rempli = false;
                    remplirToolStripMenuItem.Checked = false;
                }
                else
                {
                    f.Rempli = true;
                    remplirToolStripMenuItem.Checked = true;
                }
            }
            Invalidate();
 
            supprimerToolStripMenuItem.Enabled = false;
            changerLaCouleurToolStripMenuItem.Enabled = false;
            remplirToolStripMenuItem.Enabled = false;
            mToolStripMenuItem.Enabled = false;
            modifierLeRayonToolStripMenuItem.Enabled = false;
            modifierLaLongueurToolStripMenuItem.Enabled = false;
        }
 
        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void enregistrerToolStripMenuItem_Click(object sender, EventArgs e)
        {
 
        }
 
        private void modifierLeRayonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormRayon formRayon = new FormRayon();
            formRayon.ShowDialog();
 
            int ry = 40;
 
            switch (formRayon.DialogResult)
            {
                case DialogResult.OK:
                    ry = formRayon.Rayon;
                    break;
 
                case DialogResult.Cancel:
                    break;
            }
 
            foreach (int n in calques.SelectedIndices)
            {
                Figure f = m_FigList[n];
                if (f is Cercle)
                {
                    Cercle cer = f as Cercle;
                    cer.Rayon = ry;
                }
            }
            Invalidate();
        }
 
        private void modifierLaLongueurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLongueur formLongueur = new FormLongueur();
            formLongueur.ShowDialog();
 
            int longueur = 100;
 
            switch (formLongueur.DialogResult)
            {
                case DialogResult.OK:
                    longueur = formLongueur.Longueur;
                    break;
 
                case DialogResult.Cancel:
                    break;
            }
 
            foreach (int n in calques.SelectedIndices)
            {
                Figure f = m_FigList[n];
                if (f is Carre)
                {
                    Carre car = f as Carre;
                    car.Longueur = longueur;
                }
            }
            Invalidate();
        }
 
        private void mToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormEpaisseur formEpaisseur = new FormEpaisseur();
            formEpaisseur.ShowDialog();
 
            int epaisseur = 1;
 
            switch (formEpaisseur.DialogResult)
            {
                case DialogResult.OK:
                    epaisseur = formEpaisseur.Epaisseur;
                    break;
 
                case DialogResult.Cancel:
                    break;
            }
 
            foreach (int n in calques.SelectedIndices)
            {
                Figure f = m_FigList[n];
                f.Epaisseur = epaisseur;
            }
            Invalidate();
 
        }
    }
Pouvez-vous m'aider ?
Merci par avance.

carnibal