[C#] Franchement, vous savez quoi ? J'aime GDI+

Franchement, vous savez quoi ? J'aime GDI+ [C#] - C#/.NET managed - Programmation

Marsh Posté le 15-03-2006 à 15:41:23    

Après avoir rammé comme un galérien depuis hier sur la génération d'une étiquette "jolie" sous forme d'une image pendant une journée, je me rends compte que lorsque je met le résutat de mon contrôle (un objet Bitmap) dans un document d'impression, ça fait un truc tout moche en 72dpi.
 
Un peu écoeuré de ne pas trouver comment changer la résolution d'une image pour la refaire en plus grand, je tente "genre j'ai plus rien à perdre" de passer le "Graphics" de l'impression à ma fonction de rendu GDI+ plutôt que d'utiliser celui de mon objet créé à la main pour l'occasion.
 
Et proutch ! Mon bitmap tout pourrave 72dpi sort à la même taille mais en 600dpi maintenant :love:
 
Franchement, j'adore ce truc :D
Moi qui suis habitué à aller bidouiller des flux PCL en héxa, comment c'est cool de voir un truc qui comprends le langage humain :bounce:
 
Et ze summum, c'est que mes "drawstring()" son reconnus en tant que texte à l'impression. du coup, avec mes tests en sortie PDF (PDF995) je peux sélectionner le texte imprimé :)
C'est tout tip top. Je sus heureux :D
 
 
Bon, ok, jusque là c'est du pur blablatage, donc je vais coller un bout de mon code genre pour ceux que ça intéresserait ;)
 
La fonction qui crée une image 72dpi toute pourrie (mon test du débût)

Code :
  1. public Bitmap render()
  2.  {
  3.   // [...]
  4.   Bitmap canvas = new Bitmap(width, height);
  5.   Graphics g = Graphics.FromImage(canvas);
  6.   // [...]
  7.   // Display the family
  8.   Font ft = new Font("Arial", fontSize, FontStyle.Bold);
  9.   SizeF familySize = g.MeasureString(this.family, ft, width);
  10.   g.DrawString(this.family, ft, Brushes.Black, new RectangleF(new PointF((float)width - familySize.Width, 0f), new SizeF(familySize.Width, familySize.Height)));
  11.   // Display logo and photo
  12.   float logoBigScale = Math.Min(Math.Min((float)width / 2f - ((float)spacer * 2f), (float)this.logoBig.Width) / (float)this.logoBig.Width, Math.Min((float)width / 2f - (float)spacer * 2f, (float)this.logoBig.Height) / (float)this.logoBig.Height);
  13.   float photoScale = Math.Min(Math.Min((float)width / 2f - ((float)spacer * 2f), (float)this.photo.Width) / (float)this.photo.Width, Math.Min((float)width / 2f - (float)spacer * 2f, (float)this.photo.Height) / (float)this.photo.Height);
  14.   float logoBigX = Math.Max((float)spacer, (((float)width / 2f) - (float)this.logoBig.Width * logoBigScale) / 2f);
  15.   float logoBigY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)logoBig.Height * logoBigScale) / 2f;
  16.   float photoX = Math.Max(((float)width / 2f) + (float)spacer, ((float)width - (float)this.photo.Width * photoScale) / 2f);
  17.   float photoY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)photo.Height * photoScale) / 2f;
  18.   g.DrawImage(this.logoBig, new RectangleF(logoBigX, familySize.Height + (float)spacer + logoBigY, (float)this.logoBig.Width * logoBigScale, (float)this.logoBig.Height * logoBigScale));
  19.   g.DrawImage(this.photo, new RectangleF(photoX, familySize.Height + (float)spacer + photoY, (float)this.photo.Width * photoScale, (float)this.photo.Height * photoScale));
  20.   // [...]
  21.   // Return the result
  22.   return canvas;
  23.  }


 
Et la même en couleurs qui marche à l'impression :)
 

Code :
  1. public void print(Graphics g)
  2.  {
  3.   const int width = 500;
  4.   const int height = 900;
  5.   const int spacer = 10;
  6.   const float fontSize = 10f;
  7.   const int tableSpacer = 3;
  8.   const int tableBorder = 1;
  9.   const int tablePadding = 2;
  10.   // Ensure all pictures are present
  11.   if (this.logoBig == null)
  12.   {
  13.    this.logoBig = new Bitmap(1, 1);
  14.   }
  15.   if (this.logoSmall == null)
  16.   {
  17.    this.logoSmall = new Bitmap(1, 1);
  18.   }
  19.   if (this.photo == null)
  20.   {
  21.    this.photo = new Bitmap(1, 1);
  22.    this.txtPhoto = "";
  23.   }
  24. //   Bitmap canvas = new Bitmap(width, height);
  25. //   Graphics g = Graphics.FromImage(canvas);
  26.   //   g.Clear(Color.Red);
  27.   // Display the family
  28.   Font ft = new Font("Arial", fontSize, FontStyle.Bold);
  29.   SizeF familySize = g.MeasureString(this.family, ft, width);
  30.   g.DrawString(this.family, ft, Brushes.Black, new RectangleF(new PointF((float)width - familySize.Width, 0f), new SizeF(familySize.Width, familySize.Height)));
  31.   // Display logo and photo
  32.   float logoBigScale = Math.Min(Math.Min((float)width / 2f - ((float)spacer * 2f), (float)this.logoBig.Width) / (float)this.logoBig.Width, Math.Min((float)width / 2f - (float)spacer * 2f, (float)this.logoBig.Height) / (float)this.logoBig.Height);
  33.   float photoScale = Math.Min(Math.Min((float)width / 2f - ((float)spacer * 2f), (float)this.photo.Width) / (float)this.photo.Width, Math.Min((float)width / 2f - (float)spacer * 2f, (float)this.photo.Height) / (float)this.photo.Height);
  34.   float logoBigX = Math.Max((float)spacer, (((float)width / 2f) - (float)this.logoBig.Width * logoBigScale) / 2f);
  35.   float logoBigY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)logoBig.Height * logoBigScale) / 2f;
  36.   float photoX = Math.Max(((float)width / 2f) + (float)spacer, ((float)width - (float)this.photo.Width * photoScale) / 2f);
  37.   float photoY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)photo.Height * photoScale) / 2f;
  38.   g.DrawImage(this.logoBig, new RectangleF(logoBigX, familySize.Height + (float)spacer + logoBigY, (float)this.logoBig.Width * logoBigScale, (float)this.logoBig.Height * logoBigScale));
  39.   g.DrawImage(this.photo, new RectangleF(photoX, familySize.Height + (float)spacer + photoY, (float)this.photo.Width * photoScale, (float)this.photo.Height * photoScale));
  40.   // Display url text
  41.   //ft = new Font("Arial", fontSize, FontStyle.Bold);
  42.   SizeF urlSize = g.MeasureString(this.url, ft, width / 2 - spacer * 2);
  43.   g.DrawString(this.url, ft, Brushes.Black, new RectangleF(new PointF(((float)width / 2f - (float)spacer * 2f - urlSize.Width) / 2f, familySize.Height + logoBigY + (float)logoBig.Height * logoBigScale + (float)spacer), new SizeF(urlSize.Width, urlSize.Height)));
  44.   // Display photo text
  45.   ft = new Font("Arial", fontSize * .7f, FontStyle.Bold | FontStyle.Italic);
  46.   SizeF txtPhotoSize = g.MeasureString(this.txtPhoto, ft, width / 2 - spacer * 2);
  47.   g.DrawString(this.txtPhoto, ft, Brushes.Black, new RectangleF(new PointF((float)width / 2f + ((float)width / 2f - (float)spacer * 2f - txtPhotoSize.Width) / 2f, familySize.Height + photoY + (float)photo.Height * photoScale + (float)spacer), new SizeF(txtPhotoSize.Width, txtPhotoSize.Height)));
  48.   // Display "model" sentence then the model text
  49.   ft = new Font("Arial", fontSize, FontStyle.Bold);
  50.   SizeF txtModelTitleSize = g.MeasureString("Modèle : ", ft, width);
  51.   g.DrawString("Modèle : ", ft, Brushes.Black, new RectangleF(new PointF(0f, familySize.Height + Math.Max(logoBigY + urlSize.Height + (float)logoBig.Height * logoBigScale, photoY + txtPhotoSize.Height + (float)photo.Height * photoScale) + (float)spacer * 2f), new SizeF(txtModelTitleSize.Width, txtModelTitleSize.Height)));
  52.   SizeF txtModelTextSize = g.MeasureString(this.model, ft, width - (int)txtModelTitleSize.Height);
  53.   g.DrawString(this.model, ft, Brushes.DarkRed, new RectangleF(new PointF(txtModelTitleSize.Width, familySize.Height + Math.Max(logoBigY + urlSize.Height + (float)logoBig.Height * logoBigScale, photoY + txtPhotoSize.Height + (float)photo.Height * photoScale) + (float)spacer * 2f), new SizeF(txtModelTextSize.Width, txtModelTextSize.Height)));
  54.   // Display the table titles
  55.   // Compute columns width
  56.   SizeF txtLongTitle = g.MeasureString("Long.", ft, width);
  57.   SizeF txtLargTitle = g.MeasureString("Larg.", ft, width);
  58.   SizeF txtHautTitle = g.MeasureString("Haut.", ft, width);
  59.   int colLongWidth = (int)txtLongTitle.Width;
  60.   int colLargWidth = (int)txtLargTitle.Width;
  61.   int colHautWidth = (int)txtHautTitle.Width;
  62.   ft = new Font("Arial", fontSize, FontStyle.Regular);
  63.   foreach (ProductDetail pd in this.products)
  64.   {
  65.    colLongWidth = Math.Max(colLongWidth, (int)g.MeasureString(pd.length, ft).Width);
  66.    colLargWidth = Math.Max(colLargWidth, (int)g.MeasureString(pd.width, ft).Width);
  67.    colHautWidth = Math.Max(colHautWidth, (int)g.MeasureString(pd.height, ft).Width);
  68.   }
  69.   // Titles
  70.   ft = new Font("Arial", fontSize, FontStyle.Bold);
  71.   SizeF txtDesignationTitle = g.MeasureString("Désignation", ft, width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8));
  72.   float tableTop = txtModelTitleSize.Height + familySize.Height + Math.Max(logoBigY + urlSize.Height + (float)logoBig.Height * logoBigScale, photoY + txtPhotoSize.Height + (float)photo.Height * photoScale) + (float)spacer * 3f;
  73.   g.FillRectangle(Brushes.Black, 0f, tableTop, (float)width - (float)(colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 6 + tableBorder * 6), txtDesignationTitle.Height + (float)tablePadding * 2f + (float)tableBorder * 2f);
  74.   g.FillRectangle(Brushes.Brown, (float)tableBorder, tableTop + (float)tableBorder, (float)width - (float)(colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 6 + tableBorder * 6) - (float)tableBorder * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f);
  75.   g.DrawString("Désignation", ft, Brushes.White, new RectangleF((float)tableBorder + (float)tablePadding, (float)tableTop + (float)tableBorder + (float)tablePadding, txtDesignationTitle.Width, txtDesignationTitle.Height));
  76.   g.FillRectangle(Brushes.Black, (float)width - (float)(colLongWidth + colLargWidth + colHautWidth + tableSpacer * 2 + tablePadding * 6 + tableBorder * 6), tableTop, (float)colLongWidth + (float)tablePadding * 2f + (float)tableBorder * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f + (float)tableBorder * 2f);
  77.   g.FillRectangle(Brushes.Brown, (float)width - (float)(colLongWidth + colLargWidth + colHautWidth + tableSpacer * 2 + tablePadding * 6 + tableBorder * 5), tableTop + (float)tableBorder, (float)colLongWidth + (float)tablePadding * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f);
  78.   g.DrawString("Long.", ft, Brushes.White, new RectangleF((float)width - (float)(colLongWidth + colLargWidth + colHautWidth + tableSpacer * 2 + tablePadding * 5 + tableBorder * 5), tableTop + (float)tableBorder + (float)tablePadding, (float)colLongWidth, txtDesignationTitle.Height));
  79.   g.FillRectangle(Brushes.Black, (float)width - (float)(colLargWidth + colHautWidth + tableSpacer + tablePadding * 4 + tableBorder * 4), tableTop, (float)colLargWidth + (float)tablePadding * 2f + (float)tableBorder * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f + (float)tableBorder * 2f);
  80.   g.FillRectangle(Brushes.Brown, (float)width - (float)(colLargWidth + colHautWidth + tableSpacer + tablePadding * 4 + tableBorder * 3), tableTop + (float)tableBorder, (float)colLargWidth + (float)tablePadding * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f);
  81.   g.DrawString("Larg.", ft, Brushes.White, new RectangleF((float)width - (float)(colLargWidth + colHautWidth + tableSpacer + tablePadding * 3 + tableBorder * 3), tableTop + (float)tableBorder + (float)tablePadding, (float)colLargWidth, txtDesignationTitle.Height));
  82.   g.FillRectangle(Brushes.Black, (float)width - (float)(colHautWidth + tablePadding * 2 + tableBorder * 2), tableTop, (float)colHautWidth + (float)tablePadding * 2f + (float)tableBorder * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f + (float)tableBorder * 2f);
  83.   g.FillRectangle(Brushes.Brown, (float)width - (float)(colHautWidth + tablePadding * 2 + tableBorder * 1), tableTop + (float)tableBorder, (float)colHautWidth + (float)tablePadding * 2f, txtDesignationTitle.Height + (float)tablePadding * 2f);
  84.   g.DrawString("Haut.", ft, Brushes.White, new RectangleF((float)width - (float)(colHautWidth + tablePadding + tableBorder), tableTop + (float)tableBorder + (float)tablePadding, (float)colHautWidth, txtDesignationTitle.Height));
  85.   tableTop += txtDesignationTitle.Height + (float)tableBorder * 2f + (float)tablePadding * 2f + (float)tableSpacer;
  86.   foreach (ProductDetail fd in this.products)
  87.   {
  88.    ft = new Font("Arial", fontSize, FontStyle.Bold);
  89.    SizeF txtDesignationText = g.MeasureString(fd.designation, ft, width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8));
  90.    g.DrawString(fd.designation, ft, Brushes.Black, new RectangleF((float)tableBorder + (float)tablePadding, tableTop, (float)(width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8)), txtDesignationText.Height));
  91.    ft = new Font("Arial", fontSize, FontStyle.Regular);
  92.    g.DrawString(fd.length, ft, Brushes.Black, new PointF((float)tableBorder * 3f + (float)tablePadding * 3f + (float)tableSpacer + (float)(width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8)), tableTop));
  93.    g.DrawString(fd.width, ft, Brushes.Black, new PointF((float)tableBorder * 5f + (float)tablePadding * 5f + (float)tableSpacer * 2f + (float)(width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8)) + (float)colLongWidth, tableTop));
  94.    g.DrawString(fd.height, ft, Brushes.Black, new PointF((float)tableBorder * 7f + (float)tablePadding * 7f + (float)tableSpacer * 3f + (float)(width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8)) + (float)colLongWidth + (float)colLargWidth, tableTop));
  95.    tableTop += (float)tableSpacer + txtDesignationText.Height;
  96.   }
  97.   // Description
  98.   tableTop += (float)spacer;
  99.   ft = new Font("Arial", fontSize, FontStyle.Italic);
  100.   SizeF descriptionSize = g.MeasureString(this.description, ft, width);
  101.   g.DrawString(this.description, ft, Brushes.Black, new RectangleF(0f, tableTop, descriptionSize.Width, descriptionSize.Height));
  102.   // Price
  103.   tableTop += (float)spacer + descriptionSize.Height;
  104.   ft = new Font("Arial", fontSize * 1.2f, FontStyle.Bold);
  105.   SizeF priceLabel = g.MeasureString("Prix :", ft, width);
  106.   g.DrawString("Prix :", ft, Brushes.Black, new RectangleF(0f, tableTop, priceLabel.Width, priceLabel.Height));
  107.   tableTop += priceLabel.Height;
  108.   ft = new Font("Arial", fontSize * 3f, FontStyle.Bold);
  109.   g.DrawString(this.price, ft, Brushes.Blue, new PointF((float)spacer * 4f, tableTop));
  110.   // Small logo
  111.   g.DrawImage(this.logoSmall, ((float)width - this.logoSmall.Width) / 2f, (float)height - this.logoSmall.Height);
  112.  }
  113. }


 
(genre c'est la même chose, mais sans la création des objets "canvas" et "g", mais un Graphics passé en paramètre)
 
Appel pour imprimer :
 

Code :
  1. private System.Windows.Forms.PictureBox pictureBox1;
  2.  ProductLabel.ProductLabel tt = new ProductLabel.ProductLabel();
  3.  private void Form1_Load(object sender, System.EventArgs e)
  4.  {
  5.   pictureBox1.Image = tt.render();
  6.   PrintDocument pd = new PrintDocument();
  7.   pd.PrintPage += new PrintPageEventHandler(this.printpage);
  8.   PrintDialog printDialog1 = new PrintDialog();
  9.   printDialog1.Document = pd;
  10.   DialogResult result = printDialog1.ShowDialog();
  11.   if (result == DialogResult.OK)
  12.   {
  13.    pd.Print();
  14.   }
  15.  }
  16.  //The function to handle the printpage event
  17.  private void printpage(Object o, PrintPageEventArgs e)
  18.  {
  19.   PrintDialog pd = new PrintDialog();
  20.   tt.print(e.Graphics);
  21.  }


 
Et hop !
 
Ca donne ça :love:
 
(bon, en fait vous verrez pas, multimania déconne j'arrive pas à uploader mon PDF :o)

Reply

Marsh Posté le 15-03-2006 à 15:41:23   

Reply

Marsh Posté le 15-03-2006 à 15:43:36    

comme vous pouvez le voir, j'aime bien faire des calculs identiques 25 fois de suite et tout sur une même ligne :o ça fait tout péter le forum :D
en tout cas, j'en ai bien chier pour représenter des données sous forme d'un tableau dont les colonnes n'ont pas de taille fixe :o


Message édité par Arjuna le 15-03-2006 à 15:46:45
Reply

Sujets relatifs:

Leave a Replay

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