Annexes - RFIm : Robot Formateur Image
Transcription
Annexes - RFIm : Robot Formateur Image
Annexes /* * * * * * * * Institut Supérieur Industriel de Bruxelles Année académique 2010-2011 Bureau d'étude Robot Formateur D'image Application permettant la conversion d'une image à un Legos Mosaïques Hatim Mohammed Amine Boukobba Jaafar */ using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Drawing.Imaging; namespace WindowsFormsApplication9 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // LA fonction pixelate permet la pixellisation de l'image private static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize) { Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height); // Faire une copie du BITMAP using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated)) graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // parcourir chaque pixel dans le rectangle pour etre sur quand on est dans les limites de l'image for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize) { Int32 offsetX = pixelateSize / 2; Int32 offsetY = pixelateSize / 2; // etre sur que les Offset appartiennent à l'image while (xx + offsetX >= image.Width) offsetX--; while (yy + offsetY >= image.Height) offsetY--; // Avoir la couleur du pixel au centre du rectangle (la zone qui sera pixélliser) Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY); // mettre la couleur du pixel au centre dans chaque pixel dans la zone pexilisé for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++) for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++) pixelated.SetPixel(x, y, pixel); } } return pixelated; } // Pixelate qui permet la pixellisation d'une partie de l'image private Bitmap Pixelate(Bitmap image, Int32 blurSize) { return Pixelate(image, new Rectangle(0, 0, image.Width, image.Height), blurSize); } //LA fonction Changement couleur en couleur LEGO public static Bitmap ChangeColorToLego(Bitmap image) { Bitmap ColorChanger = new System.Drawing.Bitmap(image.Width, image.Height); Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); // Faire une copie du BITMAP using (Graphics graphics = System.Drawing.Graphics.FromImage(ColorChanger)) graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); int i, j; Color legocolorRed = Color.FromArgb(196, 40, 27); Color legocolorGreen = Color.FromArgb(40, 127, 70); Color legocolorYellow = Color.FromArgb(245, 205, 47); Color legocolorBlack = Color.FromArgb(5, 5, 15); Color legocolorBleu = Color.FromArgb(110, 153, 201); for (i = 0; i < image.Width; i++) { for (j = 0; j < image.Height; j++) { Color pixelColor = image.GetPixel(i, j); if (pixelColor.R > 40 image.SetPixel(i, if (pixelColor.R < 80 image.SetPixel(i, && j, && j, pixelColor.B < 20 && pixelColor.G < 50) legocolorRed); pixelColor.B < 10 && pixelColor.G > 40) legocolorGreen); if (pixelColor.R > 150 && pixelColor.B < 50 && pixelColor.G > 150) image.SetPixel(i, j, legocolorYellow); if (pixelColor.R < 10 && pixelColor.B < 10 && pixelColor.G < 10) image.SetPixel(i, j, legocolorBlack); if (pixelColor.R > 60 && pixelColor.B > 200 && pixelColor.G > 190) image.SetPixel(i, j, legocolorBleu); } } return ColorChanger; } //Bouton pixelliser afin de montrer la phase pixellisation private void button1_Click(object sender, EventArgs e) { Bitmap img = new Bitmap(pictureBox1.Image); Rectangle rect = new Rectangle(0, 0, img.Width, img.Height); pictureBox1.Image = Pixelate(img, rect, 5); } //bouton changement Couleur en couleur LEGO private void button2_Click(object sender, EventArgs e) { Bitmap pic = new Bitmap(pictureBox1.Image); Rectangle rect = new Rectangle(0, 0, pic.Width, pic.Height); pictureBox1.Image = ChangeColorToLego(pic); pictureBox1.Image = Pixelate(pic, rect, 5); } } }