Arjuna Aircraft Ident.: F-MBSD | 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
Franchement, j'adore ce truc
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
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
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 :
- public Bitmap render()
- {
- // [...]
- Bitmap canvas = new Bitmap(width, height);
- Graphics g = Graphics.FromImage(canvas);
- // [...]
- // Display the family
- Font ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF familySize = g.MeasureString(this.family, ft, width);
- g.DrawString(this.family, ft, Brushes.Black, new RectangleF(new PointF((float)width - familySize.Width, 0f), new SizeF(familySize.Width, familySize.Height)));
- // Display logo and photo
- 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);
- 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);
- float logoBigX = Math.Max((float)spacer, (((float)width / 2f) - (float)this.logoBig.Width * logoBigScale) / 2f);
- float logoBigY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)logoBig.Height * logoBigScale) / 2f;
- float photoX = Math.Max(((float)width / 2f) + (float)spacer, ((float)width - (float)this.photo.Width * photoScale) / 2f);
- float photoY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)photo.Height * photoScale) / 2f;
- g.DrawImage(this.logoBig, new RectangleF(logoBigX, familySize.Height + (float)spacer + logoBigY, (float)this.logoBig.Width * logoBigScale, (float)this.logoBig.Height * logoBigScale));
- g.DrawImage(this.photo, new RectangleF(photoX, familySize.Height + (float)spacer + photoY, (float)this.photo.Width * photoScale, (float)this.photo.Height * photoScale));
- // [...]
- // Return the result
- return canvas;
- }
|
Et la même en couleurs qui marche à l'impression
Code :
- public void print(Graphics g)
- {
- const int width = 500;
- const int height = 900;
- const int spacer = 10;
- const float fontSize = 10f;
- const int tableSpacer = 3;
- const int tableBorder = 1;
- const int tablePadding = 2;
- // Ensure all pictures are present
- if (this.logoBig == null)
- {
- this.logoBig = new Bitmap(1, 1);
- }
- if (this.logoSmall == null)
- {
- this.logoSmall = new Bitmap(1, 1);
- }
- if (this.photo == null)
- {
- this.photo = new Bitmap(1, 1);
- this.txtPhoto = "";
- }
- // Bitmap canvas = new Bitmap(width, height);
- // Graphics g = Graphics.FromImage(canvas);
- // g.Clear(Color.Red);
- // Display the family
- Font ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF familySize = g.MeasureString(this.family, ft, width);
- g.DrawString(this.family, ft, Brushes.Black, new RectangleF(new PointF((float)width - familySize.Width, 0f), new SizeF(familySize.Width, familySize.Height)));
- // Display logo and photo
- 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);
- 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);
- float logoBigX = Math.Max((float)spacer, (((float)width / 2f) - (float)this.logoBig.Width * logoBigScale) / 2f);
- float logoBigY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)logoBig.Height * logoBigScale) / 2f;
- float photoX = Math.Max(((float)width / 2f) + (float)spacer, ((float)width - (float)this.photo.Width * photoScale) / 2f);
- float photoY = (Math.Max((float)this.logoBig.Height * logoBigScale, (float)this.photo.Height * photoScale) - (float)photo.Height * photoScale) / 2f;
- g.DrawImage(this.logoBig, new RectangleF(logoBigX, familySize.Height + (float)spacer + logoBigY, (float)this.logoBig.Width * logoBigScale, (float)this.logoBig.Height * logoBigScale));
- g.DrawImage(this.photo, new RectangleF(photoX, familySize.Height + (float)spacer + photoY, (float)this.photo.Width * photoScale, (float)this.photo.Height * photoScale));
- // Display url text
- //ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF urlSize = g.MeasureString(this.url, ft, width / 2 - spacer * 2);
- 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)));
- // Display photo text
- ft = new Font("Arial", fontSize * .7f, FontStyle.Bold | FontStyle.Italic);
- SizeF txtPhotoSize = g.MeasureString(this.txtPhoto, ft, width / 2 - spacer * 2);
- 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)));
- // Display "model" sentence then the model text
- ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF txtModelTitleSize = g.MeasureString("Modèle : ", ft, width);
- 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)));
- SizeF txtModelTextSize = g.MeasureString(this.model, ft, width - (int)txtModelTitleSize.Height);
- 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)));
- // Display the table titles
- // Compute columns width
- SizeF txtLongTitle = g.MeasureString("Long.", ft, width);
- SizeF txtLargTitle = g.MeasureString("Larg.", ft, width);
- SizeF txtHautTitle = g.MeasureString("Haut.", ft, width);
- int colLongWidth = (int)txtLongTitle.Width;
- int colLargWidth = (int)txtLargTitle.Width;
- int colHautWidth = (int)txtHautTitle.Width;
- ft = new Font("Arial", fontSize, FontStyle.Regular);
- foreach (ProductDetail pd in this.products)
- {
- colLongWidth = Math.Max(colLongWidth, (int)g.MeasureString(pd.length, ft).Width);
- colLargWidth = Math.Max(colLargWidth, (int)g.MeasureString(pd.width, ft).Width);
- colHautWidth = Math.Max(colHautWidth, (int)g.MeasureString(pd.height, ft).Width);
- }
- // Titles
- ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF txtDesignationTitle = g.MeasureString("Désignation", ft, width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8));
- 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;
- 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);
- 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);
- g.DrawString("Désignation", ft, Brushes.White, new RectangleF((float)tableBorder + (float)tablePadding, (float)tableTop + (float)tableBorder + (float)tablePadding, txtDesignationTitle.Width, txtDesignationTitle.Height));
- 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);
- 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);
- 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));
- 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);
- 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);
- 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));
- 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);
- 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);
- g.DrawString("Haut.", ft, Brushes.White, new RectangleF((float)width - (float)(colHautWidth + tablePadding + tableBorder), tableTop + (float)tableBorder + (float)tablePadding, (float)colHautWidth, txtDesignationTitle.Height));
- tableTop += txtDesignationTitle.Height + (float)tableBorder * 2f + (float)tablePadding * 2f + (float)tableSpacer;
- foreach (ProductDetail fd in this.products)
- {
- ft = new Font("Arial", fontSize, FontStyle.Bold);
- SizeF txtDesignationText = g.MeasureString(fd.designation, ft, width - (colLongWidth + colLargWidth + colHautWidth + tableSpacer * 3 + tablePadding * 8 + tableBorder * 8));
- 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));
- ft = new Font("Arial", fontSize, FontStyle.Regular);
- 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));
- 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));
- 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));
- tableTop += (float)tableSpacer + txtDesignationText.Height;
- }
- // Description
- tableTop += (float)spacer;
- ft = new Font("Arial", fontSize, FontStyle.Italic);
- SizeF descriptionSize = g.MeasureString(this.description, ft, width);
- g.DrawString(this.description, ft, Brushes.Black, new RectangleF(0f, tableTop, descriptionSize.Width, descriptionSize.Height));
- // Price
- tableTop += (float)spacer + descriptionSize.Height;
- ft = new Font("Arial", fontSize * 1.2f, FontStyle.Bold);
- SizeF priceLabel = g.MeasureString("Prix :", ft, width);
- g.DrawString("Prix :", ft, Brushes.Black, new RectangleF(0f, tableTop, priceLabel.Width, priceLabel.Height));
- tableTop += priceLabel.Height;
- ft = new Font("Arial", fontSize * 3f, FontStyle.Bold);
- g.DrawString(this.price, ft, Brushes.Blue, new PointF((float)spacer * 4f, tableTop));
- // Small logo
- g.DrawImage(this.logoSmall, ((float)width - this.logoSmall.Width) / 2f, (float)height - this.logoSmall.Height);
- }
- }
|
(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 :
- private System.Windows.Forms.PictureBox pictureBox1;
- ProductLabel.ProductLabel tt = new ProductLabel.ProductLabel();
- private void Form1_Load(object sender, System.EventArgs e)
- {
- pictureBox1.Image = tt.render();
- PrintDocument pd = new PrintDocument();
- pd.PrintPage += new PrintPageEventHandler(this.printpage);
- PrintDialog printDialog1 = new PrintDialog();
- printDialog1.Document = pd;
- DialogResult result = printDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- pd.Print();
- }
- }
- //The function to handle the printpage event
- private void printpage(Object o, PrintPageEventArgs e)
- {
- PrintDialog pd = new PrintDialog();
- tt.print(e.Graphics);
- }
|
Et hop !
Ca donne ça
(bon, en fait vous verrez pas, multimania déconne j'arrive pas à uploader mon PDF ) |