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
|
using Object_XML_Serializer;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using XML_données;
namespace XML_image4
{
public partial class Form1 : Form
{
#region declaration
//clsResize _form_resize;
//variable pour la création des images
private int NImage = 0;
private int x = 0;
PictureBox CustomerPictureBox;
//variable pour la création des fleches sur images
private bool isMoving = false;
private Point mouseDownPosition = Point.Empty;
private Point mouseMovePosition = Point.Empty;
private List<Tuple<Point, Point>> lines;
private List<Tuple<Point, Point>> lines1;
private List<string> liste_noms_Images = new List<string>();
private List<Image> liste_Images = new List<Image>();
private List<List<Tuple<Point, Point>>> list_lines = new List<List<Tuple<Point, Point>>>();
private Dictionary<string,List<Tuple<Point, Point>>> list_lines_test=new Dictionary<string, List<Tuple<Point, Point>>>();
// nom du fichier de sauvegarde xml
private const string XML_FILE_NAME = "DonnéesObject.xml";
#endregion
public Form1()
{
InitializeComponent();
}
private void Load_images_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.RestoreDirectory = true;
openFileDialog.Filter = "All picture files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog.FileNames)
try
{
Creation_picturebox(sender, e);
CustomerPictureBox.Image = Image.FromFile(file);
CustomerPictureBox.Name = Path.GetFileName(file);
CustomerPictureBox.Tag = CustomerPictureBox.Name;
CustomerPictureBox.Image.Tag= CustomerPictureBox.Name;
liste_noms_Images.Add(CustomerPictureBox.Name);
liste_Images.Add(CustomerPictureBox.Image);
lines = new List<Tuple<Point, Point>>();
lines1 = new List<Tuple<Point, Point>>();
list_lines.Add(lines);
mouseDownPosition = Point.Empty;
mouseMovePosition = Point.Empty;
lines1.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
list_lines_test.Add(CustomerPictureBox.Name, lines1);
}
catch (Exception ex)
{
MessageBox.Show("Il y a un problème... " + file.Substring(file.LastIndexOf('\\'))
+ " ...L'utilisateur ne dispose pas des autorisations appropriées pour lire les fichiers, les chemins d'accès, etc. "
+ ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
throw;
}
}
else
{
MessageBox.Show("Selectionner au moins une image", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Creation_picturebox(object sender, EventArgs e)
{
CustomerPictureBox = new PictureBox();
if (CustomerPictureBox!=null)
{
try
{
NImage++;
x = (215 * NImage) + 3 * (NImage - 1);
CustomerPictureBox.BorderStyle = BorderStyle.None;
CustomerPictureBox.Location = new Point(x, 3);
CustomerPictureBox.ContextMenuStrip = contextMenuStrip1;
activerToolStripMenuItem.Visible = false;
supprimerToolStripMenuItem.Visible = false;
flecheToolStripMenuItem1.Visible = false;
CustomerPictureBox.Size = new Size(215, 190);
CustomerPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
CustomerPictureBox.TabIndex = 1;
CustomerPictureBox.TabStop = false;
CustomerPictureBox.Visible = true;
CustomerPictureBox.Click += new EventHandler(picture_Click);
conteneur_images.Controls.Add(CustomerPictureBox);
}
catch (Exception)
{
MessageBox.Show("Il y a un problème " + e + "'!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
throw;
}
}
}
private Donnees CreateFichier()
{
Donnees new_donnees = new Donnees();
{
new_donnees.DateTimeValue = DateTime.Now;
// index est à -1 car les listes commence à 0, il permettra de recuperer la valeur de l'index de liste
int index = -1;
for (int i = 0; i < liste_Images.Count; i++)
{
Donnees.ToutesLesImages Images0 = new Donnees.ToutesLesImages();
index++;
//ElementAt retourne l'element d'un index specifique, ici on verifie que l'index existe
if (liste_Images.ElementAt(index) != null)
{
//ElementAt retourne l'element d'un index specifique, ici l'index de index++
Images0.NameImage = liste_noms_Images.ElementAt(index);
if (liste_Images.ElementAt(index)!=null)
{
Images0.Picture = (Bitmap)liste_Images.ElementAt(index);
}
//verification que la liste de tupples (de point) est au mini egale à 1
if (lines.Count>=1)
{
Images0.Pointxml = "P1X1=" + lines.ElementAt(index).Item1.X.ToString() + "," + "P1Y1=" + lines.ElementAt(index).Item1.Y.ToString() + ";"
+ "P2X2=" + lines.ElementAt(index).Item2.X.ToString() + "," + "P2Y2=" + lines.ElementAt(index).Item2.Y.ToString() + "|";
}
}
else
{
return null;
}
new_donnees.LesImages.Add(Images0);
}
}
return new_donnees;
}
private void picture_Click(object sender, EventArgs e)
{
CustomerPictureBox.Invalidate();
CustomerPictureBox.BackColor = SystemColors.Control;
PictureBox info_picture = sender as PictureBox;
CustomerPictureBox = info_picture;
CustomerPictureBox.BackColor = Color.Red;
CustomerPictureBox.Padding = new Padding(5);
activerToolStripMenuItem_Click(sender, e);
CustomerPictureBox.Focus();
}
private void activerToolStripMenuItem_Click(object sender, EventArgs e)
{
activerToolStripMenuItem.Enabled = false;
supprimerToolStripMenuItem.Visible = true;
flecheToolStripMenuItem1.Visible = true;
}
private void flecheToolStripMenuItem1_Click(object sender, EventArgs e)
{
while (CustomerPictureBox.Focused)
{
try
{
activerToolStripMenuItem.Visible = false;
supprimerToolStripMenuItem.Visible = false;
flecheToolStripMenuItem1.Visible = false;
CustomerPictureBox.Paint += new PaintEventHandler(CustomerPictureBox_Paint);
CustomerPictureBox.MouseDown += new MouseEventHandler(CustomerPictureBoxMouseDown);
CustomerPictureBox.MouseMove += new MouseEventHandler(CustomerPictureBoxMouseMove);
CustomerPictureBox.MouseUp += new MouseEventHandler(CustomerPictureBoxMouseUp);
}
catch (Exception)
{
throw;
}
break;
}
}
private void CustomerPictureBoxMouseMove(object sender, MouseEventArgs e)
{
if (isMoving)
{
while (CustomerPictureBox.Focused)
{
try
{
mouseMovePosition = e.Location;
CustomerPictureBox.Invalidate();
}
catch (Exception)
{
throw;
}
break;
}
}
}
private void CustomerPictureBoxMouseDown(object sender, MouseEventArgs e)
{
isMoving = true;
mouseDownPosition = e.Location;
}
private void CustomerPictureBoxMouseUp(object sender, MouseEventArgs e)
{
if (isMoving)
{
while (CustomerPictureBox.Focused)
{
try
{
if (list_lines_test.ContainsKey(CustomerPictureBox.Name))
{
lines = new List<Tuple<Point, Point>>(list_lines_test[CustomerPictureBox.Name]);
lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
list_lines_test[CustomerPictureBox.Name] = lines;
}
}
catch (Exception)
{
throw;
}
break;
}
}
isMoving = false;
}
private void CustomerPictureBox_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
Pen p = new Pen(Color.Red, 10);
p.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g.DrawLine(p, mouseDownPosition, mouseMovePosition);
while (CustomerPictureBox.Focused)
{
foreach (KeyValuePair<string, List<Tuple<Point, Point>>> key_list in list_lines_test)
{
if (key_list.Key == CustomerPictureBox.Name)
{
try
{
foreach (var line in key_list.Value)
{
p.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g.DrawLine(p, line.Item1, line.Item2);
}
}
catch (Exception)
{
throw;
}
}
}
break;
}
}
} |
Partager