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
|
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string Fichier = openFileDialog1.FileName;
pictureBox2.Image = Image.FromFile(Fichier);
// lecture fichier image
Bitmap Bmp = new Bitmap(Fichier);
// taille du fichier en pixel
int largeur = Bmp.Width;
int Hauteur = Bmp.Height;
// Traitement 1 : negatif
for ( int y = 0; y < Hauteur; y++)
{
for (int x = 0; x < largeur;x++)
{
// pixel valeur
Color pixl=Bmp.GetPixel(x, y);
//extraction valer ARGB du pixel
int A = pixl.A;
int R = pixl.R;
int G = pixl.G;
int B = pixl.B;
// mettre en negatif
R = 255 - R;
G=255-G;
B=255-B;
// Modifier le pixel
Bmp.SetPixel(x,y,Color.FromArgb(A,R,G,B));
}
}
// Traitement 2 : niveau de gris
for (int y = 0; y < Hauteur; y++)
{
for (int x = 0; x < largeur; x++)
{
// pixel valeur
Color pixl = Bmp.GetPixel(x, y);
//extraction valeur ARGB du pixel
int A = pixl.A;
int R = pixl.R;
int G = pixl.G;
int B = pixl.B;
// calcul en niveau de gris
int gris =Convert.ToInt32(( 0.299 * R) + (0.587 * G )+ (0.114 * B));
// mettre en gris
R = gris ;
G = gris ;
B = gris;
// Modifier le pixel
Bmp.SetPixel(x, y, Color.FromArgb(A, R, G, B));
}
}
// Traitement : suppression du fond
for (int y = 0; y < Hauteur; y++)
{
for (int x = 0; x < largeur; x++)
{
// pixel valeur
Color pixl = Bmp.GetPixel(x, y);
//extraction valer ARGB du pixel
int A = pixl.A;
int R = pixl.R;
int G = pixl.G;
int B = pixl.B;
// mise en blanc le fond
if (B > 222)
{
R = 255;
G = 255;
B= 255;
}
else
{
R = 0;
G = 0;
B =0;
}
//enregistrement de la cartographie
DataRow DR = Dt1.NewRow();
DR["X"] = x;
DR["Y"] = y;
DR["A"] = A;
DR["R"] = R;
DR["G"] = G;
DR["B"] = B;
Dt1.Rows.Add(DR);
// Modifier le pixel
Bmp.SetPixel(x, y, Color.FromArgb(A, R, G, B));
}
}
dataGridView1.DataSource = Dt1;
pictureBox3.Image = Bmp;
FileInfo Fic = new FileInfo(Fichier);
String Repertoire = Fic.Directory.FullName;
string FichierSauvegarde = Repertoire + @"\Negatif.png";
Bmp.Save(FichierSauvegarde);
} |
Partager