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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WinGraphe
{
public partial class FormGraphe : Form
{
private Bitmap img = null;
// la liste qui contiendra les intensités
private List<int> _intensities = new List<int>();
public FormGraphe()
{
InitializeComponent();
this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void FormGraphe_Load(object sender, EventArgs e)
{
img = WinGraphe.Properties.Resources.Jellyfish;
img = new Bitmap(img, this.pictureBox1.Width-5,this.pictureBox1.Height-5);//ajustement au picbox
ComputeIntentensity(img);
ReDraw();
}
private void btnIntensisty_Click(object sender, EventArgs e)
{
ReDraw();
}
private void ReDraw()
{
if (img == null) return;
using (Graphics gr = Graphics.FromHwnd (this.pictureBox1.Handle))
{
// on dessine l'image
gr.Clear(Color.White);
Matrix m = new Matrix();
m = gr.Transform;
m.Scale(sc/100.0f,sc/100.0f);
gr.Transform = m;
gr.DrawImage(img, 0, 0);
// dessine les Axes
DrawAxis(gr);
//desine le graphe
using (Pen p = new Pen(Color.WhiteSmoke, 2))
{
// on parcours la liste des intensités
for (int i = 0; i < _intensities.Count - 1; i++)
{
// on dessine un segment entre chaque point, en inversant les ordonnées pour que les hautes intensités soient en haut
gr.DrawLine(p, i, img.Height - _intensities[i], i + 1, img.Height - _intensities[i + 1]);
}
}
}
}
// procedure de calcul de dessin deporte des Axes
private void DrawAxis(Graphics grfx)
{
using(Pen axisPen = new Pen(Brushes.Red ,2.0f))
{
axisPen.DashStyle = DashStyle.DashDot;
grfx.DrawLine(axisPen,new Point(5,img.Height-5),new Point(img.Width-5,img.Height-5)); //axe vertical OX
grfx.DrawLine(axisPen, new Point(5, 5), new Point(5, img.Height - 5)); //axe vertical OY
}
}
// procedure de calcul de jopomk
private void ComputeIntentensity(Bitmap bmp)
{
// on efface les éventuelles valeurs déjà insérées dans la liste
_intensities.Clear();
// pour chaque colonne
for (int col = 0; col < bmp.Width; col++)
{
double total = 0;
// pour chaque ligne
for (int lin = 0; lin < bmp.Height; lin++)
{
// on fait la somme des composantes RGB
Color clr = bmp.GetPixel(col, lin);
total += clr.R + clr.G + clr.B;
}
// on normalise pour obtenir des valeurs comprises entre 0 et img.Height
total /= (3 * 255);
// on ajoute la valeur convertie en int à notre liste
_intensities.Add(Convert.ToInt32(total));
}
}
private float sc = 100.0f;
private float inc = 10.0f;
private void button1_Click(object sender, EventArgs e)
{
ZoomIn(); // permet de zoomer
ReDraw();
}
public void ZoomIn()
{
sc += inc;
sc = (float)Math.Min(sc, 200.0);
}
private void button2_Click(object sender, EventArgs e)
{
ZoomOut(); // permet de dézoomer
ReDraw();
}
public void ZoomOut()
{
sc -= inc;
sc = (float)Math.Max(sc, 25.0);
}
}
} |
Partager