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
| //mesimages est la chaîne qui contient le chemin vers ... Mes Images !
string mesimages = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
//ImagesOfTheCell contiendra tous les fichiers ".jpg" de Mes Images !
string[] ImagesOfTheCell = System.IO.Directory.GetFiles(mesimages, "*.jpg");
//cwidth contiendra la largeur totale de l'image
int cwidth = 0;
//cheight contiendra la plus grande hauteur de toutes les images donc la hauteur de l'image finale.
int cheight = 0;
//bmplist contiendra tous les bitmaps de Mes Images, comme ça on ne les recharge pas deux fois ^^
Bitmap[] bmplist = new Bitmap[ImagesOfTheCell.Length];
for(int i = 0; i < ImagesOfTheCell.Length; i++)
{
string str = ImagesOfTheCell[i];
//On charge le bitmap.
Bitmap bmp = new Bitmap(str);
bmplist[i] = bmp;
//On ajoute sa largeur à cwidth;
cwidth += bmp.Size.Width;
//Si la hauteur est plus grande alors on la redéfinit
cheight = Math.Max(cheight, bmp.Size.Height);
}
//Le bitmap final a donc une taille de cwidth, cheight.
Bitmap finalbitmap = new Bitmap(cwidth, cheight);
//On créé un nouveau Graphics à partir de finalbitmap.
Graphics g = Graphics.FromImage(finalbitmap);
//oldWidth contiendra la position en largeur de la prochaine image donc commence à 0.
int oldWidth = 0;
for(int i = 0; i < ImagesOfTheCell.Length; i++)
{
//On recharge nos bitmaps de tout à l'heure
Bitmap bmp = bmplist[i];
//Point en haut à droite du rectangle.
System.Drawing.Point p = new Point(oldWidth, 0);
//On définit le rectangle.
System.Drawing.Rectangle rect = new Rectangle(p, bmp.Size);
//On dessine l'image dans finalbitmap via Graphics.DrawImage(..., ...);
g.DrawImage(bmp, rect);
//On ajoute à oldWidth la largeur de l'image qu'on vient de dessiner
oldWidth += bmp.Size.Width;
}
//Enfin, pour voir le résultat on enregistre le tout dans concatenation.bmp dans Mes Images.
finalbitmap.Save(mesimages + "/concatenation.bmp"); |
Partager