Generation d'un GIF en ASP.NET

Generation d'un GIF en ASP.NET - C#/.NET managed - Programmation

Marsh Posté le 03-05-2010 à 15:24:41    

Bonjour a tous.  :bounce:  
 
Dans le cadre de l'amelioration de l'ergo d'une appli web, je voudrais remplacer un champ texte 'heure' (en affichage) par un petit picto avec une horloge (gain de place). J'ai utilise la classe maison suivante :

Code :
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Drawing;
  14. using System.Drawing.Imaging;
  15. namespace Horloge
  16. {
  17.     public partial class Horloge : System.Web.UI.Page
  18.     {
  19.         const double LG_IMAGE = 19;
  20.         const double HT_IMAGE = 19;
  21.         const double LG_ARC_MINUTE = 7;
  22.         const double LG_ARC_HEURE = 5;
  23.         protected DateTime Tick
  24.         {
  25.             get
  26.             {
  27.                 return new DateTime(long.Parse(Request["ticks"]));
  28.             }
  29.         }
  30.         protected void Page_Load(object sender, EventArgs e)
  31.         {
  32.             this.Response.ClearContent();
  33.             this.Response.ContentType = "image/gif";
  34.             Bitmap lHorloge = new Bitmap((int)LG_IMAGE, (int)HT_IMAGE);
  35.             Pen lPen = new Pen(Color.Red, 1);
  36.             Graphics lGraphics = Graphics.FromImage(lHorloge);
  37.             Rectangle lImage = new Rectangle(1, 1, (int)LG_IMAGE - 2, (int)HT_IMAGE - 2);
  38.             lGraphics.DrawEllipse(lPen, lImage);
  39.             Point lCentre = new Point((int)LG_IMAGE / 2 + 1, (int)HT_IMAGE / 2 + 1);
  40.             lGraphics.DrawRectangle(lPen, new Rectangle((int)lCentre.X, (int)lCentre.Y, 1, 1));
  41.             // Dessin du cadran
  42.             Point[] lHeure = new Point[13];
  43.             Point[] lMinute = new Point[13];
  44.             int lAngleInitial = 270;
  45.             for (int i = 0; i < 13; i++)
  46.             {
  47.                 lMinute[i] = new Point(
  48.                     (int)Math.Floor((Math.Cos(Math.PI * (lAngleInitial) / 180) * LG_ARC_MINUTE + LG_IMAGE / 2 + 1)),
  49.                     (int)Math.Floor((Math.Sin(Math.PI * (lAngleInitial) / 180) * LG_ARC_MINUTE + HT_IMAGE / 2 + 1)));
  50.                 lHeure[i] = new Point(
  51.                     (int)Math.Floor((Math.Cos(Math.PI * (lAngleInitial) / 180) * LG_ARC_HEURE + LG_IMAGE / 2 + 1)),
  52.                     (int)Math.Floor((Math.Sin(Math.PI * (lAngleInitial) / 180) * LG_ARC_HEURE + HT_IMAGE / 2 + 1)));
  53.                 if ((i % 3) == 0)
  54.                 {
  55.                     lGraphics.DrawRectangle(lPen, new Rectangle((int)lMinute[i].X, (int)lMinute[i].Y, 1, 1));
  56.                 }
  57.                 lAngleInitial += 30;
  58.             }
  59.             // Dessin des aiguilles
  60.             int lMinutesParam = Tick.Minute;
  61.             int lHeuresParam = Tick.Hour;
  62.             lGraphics.DrawLine(new Pen(Color.Yellow), lCentre, lMinute[lMinutesParam / 5]);
  63.             lGraphics.DrawLine(new Pen(Color.Red), lCentre, lHeure[lHeuresParam % 12]);
  64.             lHorloge.Save(this.Response.OutputStream, ImageFormat.Gif);
  65.             this.Response.End();
  66.         }
  67.         protected override void Render(HtmlTextWriter writer)
  68.         {
  69.         }
  70.     }
  71. }


Je sais que ce n'est pas optimise (calculs maths alors que je n'ai pas besoin de tous les points) mais ce n'est pas le probleme (pour le moment, j'optimiserai si necessaire).  
Ce truc fonctionne a peu pres bien si ce n'est que je me retrouve avec un fond noir sur mon image alors que je n'ai specifie ca nulle part et que je n'arrive meme pas a peindre un rectangle blanc avant de dessiner mon horloge pour eviter ca.  :pt1cable:  
Qu'ai-je donc loupe pour ne pas pouvoir definir la couleur de fond (ou a defaut de la transparence) pour ce petit GIF tres primaire ?  :ange:  
 
Merci d'avance.
 
Fred.
 
edit : je precise l'appel : dans un aspx  

Code :
  1. <img src="horloge.aspx?DateTime.Now.Ticks=0" alt='Heure <%= DateTime.Now.ToString("HH:mm" ); %>" />


par ex.


Message édité par fred777888999 le 03-05-2010 à 15:30:03
Reply

Marsh Posté le 03-05-2010 à 15:24:41   

Reply

Marsh Posté le 03-05-2010 à 15:56:16    

Il te manque apres la création de ton graphic qqch du genre :
SolidBrush pinceauFond = new SolidBrush(Color.White);
graphic.FillRectangle(pinceauFond, 0, 0, largeur, longueur);


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
Reply

Marsh Posté le 03-05-2010 à 16:00:04    

J'ai essaye ces lignes de code avant de poster, j'avais un joli gif tout noir :( C'etait le sens de ma remarque 'je n'arrive meme pas a peindre un rectangle blanc...' :(

Reply

Marsh Posté le 03-05-2010 à 16:05:28    

comment ça ? :heink:
Je t'assure que le code extrêmement simple que je t'ai donné fonctionne.  
Essaie de décomposer tout appli si tu ne t'en sors pas, là le code est un peu entremêlé.
- Construction du bmp
- Creation du fond
- Creation du cadran
- Aiguilles
(c'est un exemple)
Et tu peux faire des Save de ton bitmap dans un fichier différent entre chaque étape pour voir l'évolution. Et ne passe pas forcément par ton navigateur, fais des vrais fichiers.


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
Reply

Marsh Posté le 03-05-2010 à 16:23:15    

Ben oui, quand je mets le code  

Code :
  1. Graphics lGraphics = Graphics.FromImage(lHorloge);
  2.   Rectangle lImage = new Rectangle(1, 1, (int)LG_IMAGE - 2, (int)HT_IMAGE - 2);
  3.   SolidBrush lBrushWhite = new SolidBrush(Color.White);
  4.   Pen lPenWhite = new Pen(lBrushWhite);
  5.   lGraphics.DrawRectangle(lPenWhite, lImage);
  6.   lGraphics.DrawEllipse(lPen, lImage);
  7.   Point lCentre = new Point((int)LG_IMAGE / 2 + 1, (int)HT_IMAGE / 2 + 1);


J'ai tj mon image sur fond noir. Ce qui doit chier (je pense) est l'utilisation du pen pour l'elipse qui doit avoir une couleur de fond (noir) et qui du coup me dessine mon cercle sur fond noir lors de l'instruction :

Code :
  1. lGraphics.DrawEllipse(lPen, lImage);


mais je n'ai pas trouve de signature au constructeur du crayon qui me permet de definir une couleur de fond :(

Reply

Marsh Posté le 03-05-2010 à 16:31:44    

Je suis un boulet... Pas drawRectangle (qui ne sert a rien) mais fillRectangle bien sur. Le reste etait Ok, c'est la dessus que j'avais zappe depuis le debut.
Merci.

Reply

Marsh Posté le 03-05-2010 à 17:16:34    

Pour clore definitivement le sujet, la version a peu pres optimisee :

Code :
  1. using System;
  2. using System.Web;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Web.UI;
  6. public partial class UI_Horloge : System.Web.UI.Page
  7. {
  8.  const double LG_IMAGE = 19;
  9.  const double HT_IMAGE = 19;
  10.  const double LG_ARC_MINUTE = 8;
  11.  const double LG_ARC_HEURE = 5;
  12.  protected DateTime _Tick = null;
  13.  protected DateTime Tick
  14.  {
  15.   get
  16.   {
  17.    if (_Tick == null)
  18.    {
  19.     _Tick = new DateTime(long.Parse(Request["ticks"]));
  20.    }
  21.    return _Tick;
  22.   }
  23.  }
  24.  protected void Page_Load(object sender, EventArgs e)
  25.  {
  26.   this.Response.ClearContent();
  27.   this.Response.ContentType = "image/gif";
  28.   Bitmap lHorloge = new Bitmap((int)LG_IMAGE+1, (int)HT_IMAGE+1);
  29.   Pen lPen = new Pen(Color.Black, 1);
  30.   Graphics lGraphics = Graphics.FromImage(lHorloge);
  31.   Rectangle lImage = new Rectangle(0, 0, (int)LG_IMAGE, (int)HT_IMAGE);
  32.   SolidBrush lBrushWhite = new SolidBrush(Color.White);
  33.   lGraphics.FillRectangle(lBrushWhite, new Rectangle(0,0,(int)LG_IMAGE+1, (int)HT_IMAGE+1));
  34.   lGraphics.DrawEllipse(lPen, lImage);
  35.   Point lCentre = new Point((int)LG_IMAGE / 2, (int)HT_IMAGE / 2);
  36.   lGraphics.DrawRectangle(lPen, new Rectangle((int)lCentre.X, (int)lCentre.Y, 1, 1));
  37.   // Dessin du cadran  
  38.   for (int i = 0; i < 13; i += 3)
  39.   {
  40.    lGraphics.DrawRectangle(lPen,
  41.      (int)Math.Floor((Math.Cos(Math.PI * (270 + i * 30) / 180) * LG_ARC_MINUTE + LG_IMAGE / 2)),
  42.      (int)Math.Floor((Math.Sin(Math.PI * (270 + i * 30) / 180) * LG_ARC_MINUTE + HT_IMAGE / 2)),
  43.      1,
  44.      1
  45.    );
  46.   }
  47.   // Dessin des aiguilles  
  48.   Point lMinute =
  49.   new Point(
  50.    (int)Math.Floor((Math.Cos(Math.PI * (270 + Tick.Minute * 6) / 180) * LG_ARC_MINUTE + LG_IMAGE / 2)),
  51.    (int)Math.Floor((Math.Sin(Math.PI * (270 + Tick.Minute * 6) / 180) * LG_ARC_MINUTE + HT_IMAGE / 2)));
  52.   Point lHeure = new Point(
  53.    (int)Math.Floor((Math.Cos(Math.PI * (270 + Tick.Hour * 30) / 180) * LG_ARC_HEURE + LG_IMAGE / 2)),
  54.    (int)Math.Floor((Math.Sin(Math.PI * (270 + Tick.Hour * 30) / 180) * LG_ARC_HEURE + HT_IMAGE / 2)));
  55.   lGraphics.DrawLine(new Pen(Color.Red), lCentre, lMinute);
  56.   lGraphics.DrawLine(new Pen(Color.Blue), lCentre, lHeure);
  57.   lHorloge.Save(this.Response.OutputStream, ImageFormat.Gif);
  58.   this.Response.End();
  59.  }
  60.  protected override void Render(HtmlTextWriter writer)
  61.  {
  62.  }
  63. }

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed