Pourquoi le GDI+ est si lent

NICKO02 Messages postés 6 Date d'inscription jeudi 5 décembre 2002 Statut Membre Dernière intervention 27 mars 2003 - 27 mars 2003 à 08:49
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 - 5 juin 2013 à 14:37
Voila, j'ai commencé il y a peu a creer des graphiques à l'aide du GDI+ de .net.

J'utilise principalement les methodes DrawString() et DrawLine().

C'est apropos de cette derniere : quant les lignes sont horizontales ou verticales, il n'a aucun probleme mais pour une ligne ambiée : l'ordi met 2 secondes a me l'afficher.

Si qqn connait la cause de ce probleme et, eventuellement la solution

Merci

4 réponses

SharpMao Messages postés 1024 Date d'inscription mardi 4 février 2003 Statut Membre Dernière intervention 7 juin 2010 69
27 mars 2003 à 10:09
Pourrais-tu montrer une partie de ton code, j'ai déja utilisé les DrawLine() avec des lignes ni horizontales ni verticales et je n'ai pas eu de problèmes.
Amicalement SharpMao
0
NICKO02 Messages postés 6 Date d'inscription jeudi 5 décembre 2002 Statut Membre Dernière intervention 27 mars 2003
27 mars 2003 à 12:36
Voici la classe en entier : tu devra la lancer avec une ligne :

graphique grf = new graphique("chaine", 2.0, 2.0); à partir du programme principal.

Il sagit d'y tracer une droite qui serait là de y=2x+2

Il n'y pas trops de commentaires mais tu t'y retrouvera :

Le probleme se situe dans dessin_graph()

Voila 205 lignes :

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

class graphique : System.Windows.Forms.Form
{
private string chaine; // equation de type Ax+b pour affichage
private double ax; //valeur de x
private double b; //valeur de b

//controles de la fenetre
private TextBox source;
private Label lblsrc;
private Label graduate;
private TextBox grnum;
private TextBox pas;
private Button ok;

//coordonnée de la plache graphique
Graphics g;
private int x = 6;
private int y = 20;
private int rectgd = 460;//de gauche a droite
private int recthb = 400;//de haut en bas
private int gradu = 5;
private int ipas = 10;

public graphique(string equation, double ax, double b)
{
chaine = equation;
this.ax = ax;
this.b = b;
this.Size = new Size(500, 500);
source = new TextBox();
lblsrc = new Label();
graduate = new Label();
grnum = new TextBox();
pas = new TextBox();
ok = new Button();
g = this.CreateGraphics();
controles();
}

private void controles()
{
source.Location = new Point(15, 35);
source.Size = new Size(200, 20);
lblsrc.Location = new Point(15, 15);
lblsrc.Size = new Size(200, 20);
lblsrc.Text = "Equation en cours :";
graduate.Location = new Point(230, 15);
graduate.Size = new Size(200, 20);
graduate.Text = "Nombre de graduations/taille";
grnum.Location = new Point(430, 15);
grnum.Size = new Size(40, 20);
grnum.Text = Convert.ToString(gradu);
pas.Location = new Point(472, 15);
pas.Size = new Size(40, 20);
pas.Text = Convert.ToString(ipas);
ok.Location = new Point(430, 35);
ok.Size = new Size(82, 20);
ok.Text = "OK";
ok.Click += new System.EventHandler(this.ok_onclick);

this.Controls.Add(lblsrc);
this.Controls.Add(source);
this.Controls.Add(graduate);
this.Controls.Add(grnum);
this.Controls.Add(pas);
this.Controls.Add(this.ok);
//mettre les bonnes mesures
try
{
gradu = 2*Convert.ToInt16(grnum.Text);
ipas = (Convert.ToInt16(pas.Text))/2;
}
catch(Exception x)
{
string erreur = Convert.ToString(x);
}
dessin_graph();
this.Show();

}

private void dessin_graph()
{

Point centre = new Point(rectgd/2+6, recthb/2+20);

//zone de graphique
Graphics g = this.CreateGraphics();
g.PageUnit = GraphicsUnit.Millimeter;

Font fonte = new Font("Arial", 12);
Font gradufonte = new Font("Arial", 8);
SolidBrush stylo = new SolidBrush(Color.Black);

g.FillRectangle(new SolidBrush(Color.LightBlue), new Rectangle(x, y, recthb, rectgd));
//ligne verticale
g.DrawLine(new Pen(Color.Black, 0),centre.X, y,centre.X, y + recthb);
//ligne horizontale
g.DrawLine(new Pen(Color.Black, 0), x, centre.Y, x+rectgd, centre.Y);

//graduer l'axe des y
g.DrawString("y", fonte, stylo, new Point(centre.X, y));
g.DrawString("x", fonte, stylo, new Point((x+rectgd)-4, centre.Y));
for(int i centre.Y, j 0; i >= 0; i -= ipas, ++j)
{
if(j <= gradu)
{
g.DrawLine(new Pen(Color.Green, 0), centre.X, i, centre.X+2, i);
if(j % 2 == 0)
{
g.DrawString(Convert.ToString(j/2), gradufonte, stylo, new Point(centre.X+4, i-2));
g.DrawLine(new Pen(Color.Red, 0), centre.X, i, centre.X+3, i);
}
}
}
///////////////////for(int i centre.Y, j 0; i <= recthb+20; i += ipas, --j)
{
if((j*-1) <= gradu)
{
g.DrawLine(new Pen(Color.Green, 0), centre.X, i, centre.X+2, i);
if((j % 2 == 0) && (j!= 0))
{
g.DrawString(Convert.ToString(j/2), gradufonte, stylo, new Point(centre.X+4, i-2));
g.DrawLine(new Pen(Color.Red, 0), centre.X, i, centre.X+3, i);
}
}
}

///////////////////for(int i centre.X, j 0; i <= rectgd+6; i += ipas, ++j)
{
if((j) <= gradu)
{
g.DrawLine(new Pen(Color.Green, 0), i, centre.Y, i, centre.Y+2);
if((j % 2 == 0) && (j != 0))
{
g.DrawString(Convert.ToString(j/2), gradufonte, stylo, new Point(i-2, centre.Y+4));
g.DrawLine(new Pen(Color.Red, 0), i, centre.Y, i, centre.Y+3);
}
}
}

///////////////////for(int i centre.X, j 0; i <= rectgd+6; i -= ipas, --j)
{
if((j*-1) <= gradu)
{
g.DrawLine(new Pen(Color.Green, 0), i, centre.Y, i, centre.Y+2);
if((j % 2 == 0) && (j != 0))
{
g.DrawString(Convert.ToString(j/2), gradufonte, stylo, new Point(i-2, centre.Y+4));
g.DrawLine(new Pen(Color.Red, 0), i, centre.Y, i, centre.Y+3);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Maintenant, tracer la droite

float fb = (float)b;

g.DrawLine(new Pen(Color.Violet, 0), centre.X-gradu*ipas, centre.Y-fb*ipas, centre.X+gradu*ipas, centre.Y+fb*ipas);
source.Text = Convert.ToString(fb);
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
recthb = this.Height/5+20;
if(recthb % 2 == 0)//si nombre pair
rectgd = recthb;
else
rectgd = ++recthb;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
dessin_graph();
}
private void ok_onclick(object sender, System.EventArgs e)
{
try
{
gradu = 2*Convert.ToInt16(grnum.Text);
ipas = (Convert.ToInt16(pas.Text))/2;
}
catch(Exception x)
{
string erreur = Convert.ToString(x);
}
dessin_graph();
}
}
0
cs_Scooper Messages postés 71 Date d'inscription jeudi 2 octobre 2003 Statut Membre Dernière intervention 12 septembre 2013
4 juin 2013 à 23:43
il me semble (si je ne dis pas de bétises) que dans le onpaint il faut appeller d'abord base.onpaint(e)
puis dessiner mais en passant à la fonction l'objet e.graphics

cela éviter de recréer un objet à chaque appel de fonction

de plus les objets Pen, Brush ... doivent être supprimés explicitement à la fin (Dispose()) ou bien les utiliser comme suit :

using(Pen p = new Pen ...)
{
//dessins ici
}
comme cela ils sont automatiquement détruits
0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
5 juin 2013 à 14:37
bonjour scooper, j'espère pour lui qu'il a trouvé depuis 10 ans...


Whismeril
0
Rejoignez-nous